[Solved] incorrect calculation in Qt Script
While when i calculete Math.sin(Math.PI) i get this answer: 1.2246467991473532e-16 instead of 0
Same with Math.cos(Math.PI/2) = 6.123233995736766e-17
Why is this happening?
I advance thank you for your help!
8 replies
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.
You must log in to post a reply. Not a member yet? Register here!



