sine, cosine, and the GCC

Started by
11 comments, last by webwraith 15 years, 5 months ago
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?
Advertisement
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).
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.
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.
Ah, I see... Why the OpenGL API uses degrees is beyond me. Here's a gamedev topic about it: click
(double post)
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.
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
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.
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.

This topic is closed to new replies.

Advertisement