Jump to content
  • Advertisement
Sign in to follow this  
KodeNerd

Sin and Cosine Rounding Error?

This topic is 3589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Are the Sin and Cosine functions in C++ inaccurate or is it just the fact that I am using floats instead of doubles?

Share this post


Link to post
Share on other sites
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!