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.

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 on other sites
Show us an example.

Share on other sites
There are various sin()'s, use sinf() for floats or else

sin(float(x));

Share on other sites
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.8509040.525322

On gcc (forgot what version) in debug mode.

Share on other sites
Quote:
 Original post by KodeNerdI 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 on other sites
Wow, I always thought that those two functions took degrees. That fixed everything.

Rating++

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 on other sites
Quote:
 Original post by alvaroAnd 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.

• 27
• 15
• 23
• 10
• 19