Jump to content
  • Advertisement
Sign in to follow this  
webwraith

sine, cosine, and the GCC

This topic is 3499 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

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 this post


Link to post
Share on other sites
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).

Share this post


Link to post
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 this post


Link to post
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 this post


Link to 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

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!