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.Wed, 03 Mar 2021 22:52:49 +0100Numerical approximation errorhttps://ask.sagemath.org/question/55994/numerical-approximation-error/I'm apparently having a very specific rounding/floating point error when using the numerical approximation function .n(). I reproduced it in the example below. The "1" would be a function whose numerical value amounts to 1.
Input:
print(int(-1/6*1.n() + 49/6))
print(int(-1/6 + 49/6))
Output:
7
8
The error does not happen if we specify a precision for approximation, such as:
Input:
print(int(-1/6*1.n(1)+ 49/6))
print(int(-1/6 + 49/6))
Output:
8
8
Can this be corrected?
Tue, 02 Mar 2021 15:44:22 +0100https://ask.sagemath.org/question/55994/numerical-approximation-error/Answer by John Palmieri for <p>I'm apparently having a very specific rounding/floating point error when using the numerical approximation function .n(). I reproduced it in the example below. The "1" would be a function whose numerical value amounts to 1. </p>
<p>Input:</p>
<pre><code>print(int(-1/6*1.n() + 49/6))
print(int(-1/6 + 49/6))
</code></pre>
<p>Output:</p>
<pre><code>7
8
</code></pre>
<p>The error does not happen if we specify a precision for approximation, such as:
Input:</p>
<pre><code>print(int(-1/6*1.n(1)+ 49/6))
print(int(-1/6 + 49/6))
</code></pre>
<p>Output:</p>
<pre><code>8
8
</code></pre>
<p>Can this be corrected?</p>
https://ask.sagemath.org/question/55994/numerical-approximation-error/?answer=55998#post-id-55998Once you use a numerical approximation, you probably shouldn't do further arithmetic and then another approximation (such as applying `int`) and expect reliable results. The number you're getting is very close to 8, just a little smaller, so when `int` truncates it, the result is 7.
sage: 8-(49/6-(1/6*1.n()))
8.88178419700125e-16
Perhaps it would be better to use `round` instead of `int`: `int` is a built in Python function, while `round` is better suited to work with Sage number types. In general, though, it's probably best not to use `.n()` until the end of the calculation.Wed, 03 Mar 2021 06:02:54 +0100https://ask.sagemath.org/question/55994/numerical-approximation-error/?answer=55998#post-id-55998Comment by John Palmieri for <p>Once you use a numerical approximation, you probably shouldn't do further arithmetic and then another approximation (such as applying <code>int</code>) and expect reliable results. The number you're getting is very close to 8, just a little smaller, so when <code>int</code> truncates it, the result is 7.</p>
<pre><code>sage: 8-(49/6-(1/6*1.n()))
8.88178419700125e-16
</code></pre>
<p>Perhaps it would be better to use <code>round</code> instead of <code>int</code>: <code>int</code> is a built in Python function, while <code>round</code> is better suited to work with Sage number types. In general, though, it's probably best not to use <code>.n()</code> until the end of the calculation.</p>
https://ask.sagemath.org/question/55994/numerical-approximation-error/?comment=56012#post-id-56012One more comment: when you do `-1/6 * 1.n()`, the use of `1.n()` forces all remaining computations to be done using approximate computer arithmetic, as opposed to `-1/6 * 1` which Sage recognizes as being a fraction with exact value -1/6. That's why the rounding issues occur.Wed, 03 Mar 2021 22:52:49 +0100https://ask.sagemath.org/question/55994/numerical-approximation-error/?comment=56012#post-id-56012