October 2, 2011

Ruzik Ruzik
Robot Herder
283 posts

[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

October 2, 2011

Andre Andre
Mad Scientist
4667 posts

THis is not a Qt Script problem, but a computer problem. There is a limit to the precision of numbers you can express in floating point numbers. That is what you are seeing. You should compare them using an epsilon.

 Signature 

Nokia Certified Qt Specialist.
Interested in Qt consultancy and job opportunities.

October 3, 2011

ZapB ZapB
Gene Splicer
1206 posts

Limited numerical precision of the floating point representation of numbers is the problem.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

October 3, 2011

Ruzik Ruzik
Robot Herder
283 posts

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)

October 3, 2011

fluca1978 fluca1978
Dinosaur Breeder
487 posts

As far as I understand the problem is that Math.PI does not express the pure PI, since Math.PI is limited by the precision of the computer floating numbers. I guess you will have the same result (or a similar one) using another math library.

October 3, 2011

sierdzio sierdzio
Dinosaur Breeder
536 posts
Ruzik wrote:
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)

I’m afraid they are right, mate. With floating point, you cannot really get exactly 0, save for a few cases.

 Signature 

(N(:^

October 3, 2011

Andre Andre
Mad Scientist
4667 posts
Ruzik wrote:
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)

Well, 6.123233995736766e-17 ≈ 0, as far as floating point numbers are concerned.

 Signature 

Nokia Certified Qt Specialist.
Interested in Qt consultancy and job opportunities.

October 3, 2011

Ruzik Ruzik
Robot Herder
283 posts

Clear, thank you for your help!

October 3, 2011

ZapB ZapB
Gene Splicer
1206 posts

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.

  1. const double eps = 1.0e-10; // as an arbitrary example
  2. double value = sin( pi ); // pi defined somewhere else
  3. const double test = 0.0;
  4. if ( qAbs( value - test ) < eps )
  5.     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.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

 
  ‹‹ Inserting and removing data with QXmlQuery      problems with Qt and strange caracters! ››

You must log in to post a reply. Not a member yet? Register here!