Sin and Cosine Rounding Error?

Started by
6 comments, last by KodeNerd 15 years, 8 months ago
Are the Sin and Cosine functions in C++ inaccurate or is it just the fact that I am using floats instead of doubles?
------------George Gough
Advertisement
Show us an example.
There are various sin()'s, use sinf() for floats or else

sin(float(x));
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.
------------George Gough
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 :)
Wow, I always thought that those two functions took degrees. That fixed everything.

Rating++
------------George Gough
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).
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.
------------George Gough

This topic is closed to new replies.

Advertisement