Sin and Cosine Rounding Error?
Are the Sin and Cosine functions in C++ inaccurate or is it just the fact that I am using floats instead of doubles?
I checked and I was actually using a double and this still happened.
In this sample program I was running I used the following snippet and got the shown output:
And got
On gcc (forgot what version) in debug mode.
In this sample program I was running I used the following snippet and got the shown output:
#include <cmath>#include <iostream>int main(void) { float value = 45; std::cout << 1.0 * sin(45) << "\n" << 1.0 * cos(45); //The 1.0 multiplication is just reproducing something from my source code}
And got
0.8509040.525322
On gcc (forgot what version) in debug mode.
Quote:Original post by KodeNerd
I checked and I was actually using a double and this still happened.
In this sample program I was running I used the following snippet and got the shown output:
*** Source Snippet Removed ***
And got
*** Source Snippet Removed ***
On gcc (forgot what version) in debug mode.
sin and cos takes radians, not degrees :)
And you thought it was a problem of accuracy? :)
In these cases I usually make a plot (using gnuplot, or even OpenOffice), which makes things much more clear. Alternatively, you can look it up on a reference.
You should get used to using radians. The only place where I have seen degrees being used in the last 15 years is in OpenGL (and I have no idea why they chose to use them).
In these cases I usually make a plot (using gnuplot, or even OpenOffice), which makes things much more clear. Alternatively, you can look it up on a reference.
You should get used to using radians. The only place where I have seen degrees being used in the last 15 years is in OpenGL (and I have no idea why they chose to use them).
Quote:Original post by alvaro
And you thought it was a problem of accuracy? :)
I saw that they were only a couple of hundredths off so I guessed that it might just be a rounding error.
Quote:
In these cases I usually make a plot (using gnuplot, or even OpenOffice), which makes things much more clear. Alternatively, you can look it up on a reference.
I don't actually have a reference around anymore, I gave it away...I guess I should get another.
Quote:
You should get used to using radians. The only place where I have seen degrees being used in the last 15 years is in OpenGL (and I have no idea why they chose to use them).
I am using OpenGL so that is why I (stupidly) assumed that the sine and cosine functions also took degrees.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement