Ask Your Question
1

SageMath sometimes returns a very small number instead of 0

asked 2022-08-23 06:41:50 +0100

giwaum gravatar image

updated 2022-08-23 17:54:03 +0100

slelievre gravatar image

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

In  [1]: 14 * 1.2 - 16.8
Out [1]: 0.000000000000000

In  [2]: 12 * 1.4 - 16.8
Out [2]: -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 flag offensive close merge delete

Comments

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

tmonteil gravatar imagetmonteil ( 2022-08-23 18:55:08 +0100 )edit

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

giwaum gravatar imagegiwaum ( 2022-09-13 11:40:56 +0100 )edit

2 Answers

Sort by » oldest newest most voted
2

answered 2022-08-23 21:39:52 +0100

tmonteil gravatar image

updated 2022-08-23 21:46:19 +0100

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)
edit flag offensive delete link more
2

answered 2022-08-23 16:55:56 +0100

FrédéricC gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-08-23 06:41:50 +0100

Seen: 377 times

Last updated: Sep 13 '22