cos/sin acting strange?

Started by
2 comments, last by PHRICTION 20 years, 9 months ago
This is probably something really obvious I''m overlooking, but I don''t see it. Is there some attribute of the c/c++ standard library trig functions? Heres the problem. Why does this: cosf(acosf(0)) Give me this: -4.37114e-08 cosf gives me that value when I feed in a right angle. Am I stupid or is cos(PI/2) not 0? I''ve tried this in linux and VCPP. Also if I do this: float HALFPI = M_PI / 2; float value = sinf( HALFPI - acosf(0) ); value will be 0. But if I do this: float value - sinf( (M_PI/2) - acosf(0) ); value will again be -4.37114e-08 Does this make any sense to anyone?
Advertisement
It's caused by floating point inaccuracy. For more information, read up on the IEEE 754 standard floating-point format.

Don't worry though; for all intents and purposes, that number IS zero, except for the fact that its binary representation is not literally zero, so you'd have to check its absolute value against a suitable epsilon to see if it was small enough to be considered zero.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links


[edited by - zealouselixir on July 8, 2003 12:21:24 AM]

[twitter]warrenm[/twitter]

Why use float? Why not use double and use the sin and cos functions?
And yes, -4.37114e-08 is a very small number, close enough to zero.
<a href="http://docs.sun.com/source/806-3568/ncg_goldberg.html">What Every Computer Scientist Should Know About Floating-Point Arithmetic</a>

This topic is closed to new replies.

Advertisement