ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 13 Sep 2022 11:40:56 +0200SageMath sometimes returns a very small number instead of 0https://ask.sagemath.org/question/63721/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 [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.Tue, 23 Aug 2022 06:41:50 +0200https://ask.sagemath.org/question/63721/sagemath-sometimes-returns-a-very-small-number-instead-of-0/Comment by giwaum for <p>I am new to SageMath.
Today I used it as a calculator and found the following:</p>
<pre><code>In [1]: 14 * 1.2 - 16.8
Out [1]: 0.000000000000000
In [2]: 12 * 1.4 - 16.8
Out [2]: -3.55271367880050e-15
</code></pre>
<p>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?</p>
<p>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.</p>
https://ask.sagemath.org/question/63721/sagemath-sometimes-returns-a-very-small-number-instead-of-0/?comment=63993#post-id-63993Thank you all very much for your clear and helpful answers!Tue, 13 Sep 2022 11:40:56 +0200https://ask.sagemath.org/question/63721/sagemath-sometimes-returns-a-very-small-number-instead-of-0/?comment=63993#post-id-63993Comment by tmonteil for <p>I am new to SageMath.
Today I used it as a calculator and found the following:</p>
<pre><code>In [1]: 14 * 1.2 - 16.8
Out [1]: 0.000000000000000
In [2]: 12 * 1.4 - 16.8
Out [2]: -3.55271367880050e-15
</code></pre>
<p>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?</p>
<p>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.</p>
https://ask.sagemath.org/question/63721/sagemath-sometimes-returns-a-very-small-number-instead-of-0/?comment=63724#post-id-63724Text representation is better than screen capture, as it allows people to copy-paste.Tue, 23 Aug 2022 18:55:08 +0200https://ask.sagemath.org/question/63721/sagemath-sometimes-returns-a-very-small-number-instead-of-0/?comment=63724#post-id-63724Answer by tmonteil for <p>I am new to SageMath.
Today I used it as a calculator and found the following:</p>
<pre><code>In [1]: 14 * 1.2 - 16.8
Out [1]: 0.000000000000000
In [2]: 12 * 1.4 - 16.8
Out [2]: -3.55271367880050e-15
</code></pre>
<p>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?</p>
<p>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.</p>
https://ask.sagemath.org/question/63721/sagemath-sometimes-returns-a-very-small-number-instead-of-0/?answer=63727#post-id-63727You 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)Tue, 23 Aug 2022 21:39:52 +0200https://ask.sagemath.org/question/63721/sagemath-sometimes-returns-a-very-small-number-instead-of-0/?answer=63727#post-id-63727Answer by FrédéricC for <p>I am new to SageMath.
Today I used it as a calculator and found the following:</p>
<pre><code>In [1]: 14 * 1.2 - 16.8
Out [1]: 0.000000000000000
In [2]: 12 * 1.4 - 16.8
Out [2]: -3.55271367880050e-15
</code></pre>
<p>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?</p>
<p>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.</p>
https://ask.sagemath.org/question/63721/sagemath-sometimes-returns-a-very-small-number-instead-of-0/?answer=63722#post-id-63722If 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`.Tue, 23 Aug 2022 16:55:56 +0200https://ask.sagemath.org/question/63721/sagemath-sometimes-returns-a-very-small-number-instead-of-0/?answer=63722#post-id-63722