visual studio math.h inconsistencies?

Why is sin(pi) not exactly 0? I have written a text parsing calculator for Windows which interprets command lines similar to how a TI-85 can. When I print answers in standard 'f' notation, sin(M_PI), M_PI as defined by math.h, prints 0.00000000... However when the answer is printed using the scientific notation 'g' flag in printf/sprintf using 12-digit precision, it appears as 1.22460635382e-16

Using <cmath> so everything appears under std::

So if the definition of M_PI appears in the same header with std::sin(double), why the discrepancy? I guess I could write in a check for sin,cos,tan, etc calls
with M_PI and -M_PI, and force them to return the correct values... but that
doesn't really sound like it should be my job... call me lazy. Or am I just
misinterpreting something?

edit note: cos(pi) appears correct. -1. as does sin(0), cos(0), cos(pi), and tan(0). tan(pi) however is the same story as sin(pi) only negative.
Last edited on
There's no discrepancy. The result has too many zeroes, so it can't be completely converted to the decimal system. What's printed are just the first six decimal places of 1.22460635382e-16.
By the way, trigonometrical operations are so slow, that unless high precision is necessary, it is common to just pre-calculate (before compile-time) the results and put them in a static vector.
Thanks. Yeah I just checked it out in GCC and got the same thing. I just never have really messed with trig functions much and assumed they were programmed to recognize the major arguments.
Topic archived. No new replies allowed.