• Advertisement
Sign in to follow this  

[C++] math.h sin(M_PI) problem.

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

Advertisement
M_PI is not exactly pi, because floating-point values cannot represent pi exactly. Therefore, the sine of M_PI is not 0.

Share this post


Link to post
Share on other sites
Quote:
Original post by WindScar
It's returning a weird number that's not zero. What's wrong?
Probably just a precision issue. Can you post the number that you're getting?

[Too slow...]

Share this post


Link to post
Share on other sites
Link favorited for future reading. The number I'm getting is 6.12303e-17. I suppose it's very close to 0 but I don't know how to read it.

Share this post


Link to post
Share on other sites
It seems a bit weird to have such a small number in my console instead of 0, but I suppose that's too small to be a problem. Thank you.

Share this post


Link to post
Share on other sites
Quote:
Original post by WindScar
It seems a bit weird to have such a small number in my console instead of 0, but I suppose that's too small to be a problem. Thank you.


Well, it's too small to be a problem unless you're doing things like testing thingThatShouldBeZero == 0 and getting strange bugs as a result (in case it's not clear, you should test for thingThatShouldBeZero < epsilon). Floating point computation has lots of gotchas that are well worth learning about. If you're just worried about pretty-printing, use setprecision to cut it off at a sensible point.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement