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

This topic is 3003 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

It's returning a weird number that's not zero. What's wrong?

Share on other sites
Is the number practically zero? Maybe you need to read this

Share on other sites
M_PI is not exactly pi, because floating-point values cannot represent pi exactly. Therefore, the sine of M_PI is not 0.

Share on other sites
Quote:
 Original post by WindScarIt'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 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 on other sites
The e-17' part means times 10^(-17)', in other words, the number is 0.0000000000000000612303.

Share on other sites
Quote:
 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.
It's scientific notation.

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 on other sites
Quote:
 Original post by WindScarIt 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.

• 10
• 13
• 14
• 18
• 15