Archived

This topic is now archived and is closed to further replies.

PHRICTION

cos/sin acting strange?

Recommended Posts

PHRICTION    122
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?

Share this post


Link to post
Share on other sites
ZealousElixir    256
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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
<a href="http://docs.sun.com/source/806-3568/ncg_goldberg.html">What Every Computer Scientist Should Know About Floating-Point Arithmetic</a>

Share this post


Link to post
Share on other sites