[Solved] incorrect calculation in Qt Script
-
[quote author="Ruzik" date="1317636902"]As i understude, you dont understunde me(maybe i am dont right, my english is bad) i say not about
e-16 in 1.2246467991473532e-16, i say about sin(pi) should be equal 0 as cos(pi/2)[/quote]I'm afraid they are right, mate. With floating point, you cannot really get exactly 0, save for a few cases.
-
[quote author="Ruzik" date="1317636902"]As i understude, you dont understunde me(maybe i am dont right, my english is bad) i say not about
e-16 in 1.2246467991473532e-16, i say about sin(pi) should be equal 0 as cos(pi/2)[/quote]Well, 6.123233995736766e-17 ≈ 0, as far as floating point numbers are concerned.
-
Also, the trigonometric functions are transcendental functions and so cannot be calculated exactly. All implementations use approximations that are good to some degree of accuracy for a range of inputs.
As Andre suggested, test it with an epsilon (small value) to see if it is in agreement with your criteria. i.e.
@
const double eps = 1.0e-10; // as an arbitrary example
double value = sin( pi ); // pi defined somewhere else
const double test = 0.0;
if ( qAbs( value - test ) < eps )
qDebug() << "Values agree within some small value epsilon";
@Obviously you can use different values for eps and test as needed in your particular cases. This is just a fact of life when working with floating point representations on machines with limited memory.