Sign in to follow this  
KodeNerd

Sin and Cosine Rounding Error?

Recommended Posts

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:


#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.850904
0.525322



On gcc (forgot what version) in debug mode.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this