# SageMath sometimes returns a very small number instead of 0

I am new to SageMath. Today I used it as a calculator and found the following:

In  : 14 * 1.2 - 16.8
Out : 0.000000000000000

In  : 12 * 1.4 - 16.8
Out : -3.55271367880050e-15


Why is it that for two very similar calculations it returns once 0 (which is correct) and then an approximate version of 0? How can I avoid this approximate result to receive a more precise answer? Did I do something wrong, meaning should I specify something extra?

Note: I wanted to include a screen capture of this calculus but since I am a new user I was not yet allowed to include pictures.

edit retag close merge delete

Text representation is better than screen capture, as it allows people to copy-paste.

Sort by » oldest newest most voted

You can have a look at https://floating-point-gui.de/

The reason is that floating-point numbers are multiple of a (negative) power of 2, and since $0.2=1/5$, it can not be represented exactly (the equality $1/5 = m/2^e$ has no integer solution in $m$ and $e$).

Actually, none of 14 * 1.2 nor 16.8 are exact, but the cancellation in the sum luckilly leads to zero, so the correct result results from two errors, see:

sage: a = 16.8
sage: a.exact_rational()
4728779608739021/281474976710656
sage: a.sign_mantissa_exponent()
(1, 4728779608739021, -48)

more If you use numbers with a dot inside, they are considered as floating point numbers and the computations are never exact. Use rationals, for instance 12*14/10-168/10.

more

Thank you all very much for your clear and helpful answers!

more