sine, cosine, and the GCC

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

Recommended Posts

I am using simple functions to transform radians into degrees from the sin() cos() and tan() functions in <cmath>. When I ask for the value for COS(90) (my cos() function), I get 1.61554e-15. Now, I know this is very small, but it should be 0. Is this a rounding error, a granularity error (I'm using doubles), a problem with the accuracy of my PI value (3.14159265358979), or do I just need to create a special case for when cos angles reach 90 degrees?

Share on other sites
It's just precision error, for the most part you can safely ignore it (as long as you aren't checking for equality against 0 -- make sure to use epsilons).

Share on other sites
cos takes radians, so try cos(90/180*PI)

Edit: I missed the part where you said COS -> your cos function. Note that it's wise to work in radians all over the place; forget degrees.

Share on other sites
I'd love to, but I'm using the results to go into matrices for OpenGL, which uses degrees rather than radians.Still, if it's only precision errors, then I'll just have to keep an eye on it, and not worry too much. Thanks for the help.

Share on other sites
Ah, I see... Why the OpenGL API uses degrees is beyond me. Here's a gamedev topic about it: click

(double post)

Share on other sites
Quote:
 I'd love to, but I'm using the results to go into matrices for OpenGL, which uses degrees rather than radians.
This doesn't quite make sense, given what you've posted.

The only parts of the OpenGL API that use degrees (that I'm aware of, at least) are the glRotate*() functions. Are you using one of these functions? And if so, how would (e.g.) cos() or sin() come into play anyway?

In other words, I'm not quite clear on why using OpenGL would force you to use degrees in the way you've shown.

Also, the rounding errors you've described are to be expected, and in this context at least can almost certainly be safely ignored.

Share on other sites
So if I was plugging degrees into a matrix that I intend to use with glLoadMatrix(), I would need the results in radians anyway? I was under the impression that degrees would be used throughout OpenGL, rather than just in the glRotateX() family of functions

Share on other sites
I think there are some confusion here. Either on our side for not understanding your application, of on your side for not understanding how angles are used in OpenGL.

The only place in OpenGL where you work with angles is in the parameters to glRotate. Note that; the parameters to glRotate. Once the matrix is created, there are no angles anymore; there is only a matrix. glLoadMatrix accepts a matrix and there are no angles in that matrix either. Whether you construct the matrix from radians or angles is completely irrelevant to the content of the matrix. A rotation of one quarter of a turn, let it be 90 degrees or pi/2 radians, is always a rotation of one quarter.

Share on other sites
Sorry, I meant that I was using degrees with cos() and sin() to generate the contents of the matrix. I wasn't actually plugging degree values directly into matrices, that was merely poor wording on my behalf, and I apologise for it.

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 12
• 14
• 9
• Forum Statistics

• Total Topics
633351
• Total Posts
3011476
• Who's Online (See full list)

There are no registered users currently online

×