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.Fri, 17 Sep 2021 10:23:01 +0200Weird rounding error in n and Nhttps://ask.sagemath.org/question/59039/weird-rounding-error-in-n-and-n/ This is weird. While dabbling in sage, It seems I hit a rounding error.
sage: number=180*arccos(14/205*sqrt(41)*sqrt(5))/pi
sage: n(number)
12.0947570770121
sage: n(number,digits=4)
12.10
Is there any good reason that I'm not getting 12.09 as a result? This happens in N as well.
Here's my sage info.
SageMath version 9.2, Release Date: 2020-10-24
Using Python 3.9.5. Type "help()" for help.Wed, 15 Sep 2021 22:52:41 +0200https://ask.sagemath.org/question/59039/weird-rounding-error-in-n-and-n/Comment by eric_g for <p>This is weird. While dabbling in sage, It seems I hit a rounding error.</p>
<pre><code>sage: number=180*arccos(14/205*sqrt(41)*sqrt(5))/pi
sage: n(number)
12.0947570770121
sage: n(number,digits=4)
12.10
</code></pre>
<p>Is there any good reason that I'm not getting 12.09 as a result? This happens in N as well.</p>
<p>Here's my sage info.</p>
<pre><code>SageMath version 9.2, Release Date: 2020-10-24
Using Python 3.9.5. Type "help()" for help.
</code></pre>
https://ask.sagemath.org/question/59039/weird-rounding-error-in-n-and-n/?comment=59076#post-id-59076It seems to me that Max Alekseyev's comment provides the correct answer (and should changed into a regular answer btw). Why are you saying that the original question is not answered?Fri, 17 Sep 2021 10:23:01 +0200https://ask.sagemath.org/question/59039/weird-rounding-error-in-n-and-n/?comment=59076#post-id-59076Comment by jakupl for <p>This is weird. While dabbling in sage, It seems I hit a rounding error.</p>
<pre><code>sage: number=180*arccos(14/205*sqrt(41)*sqrt(5))/pi
sage: n(number)
12.0947570770121
sage: n(number,digits=4)
12.10
</code></pre>
<p>Is there any good reason that I'm not getting 12.09 as a result? This happens in N as well.</p>
<p>Here's my sage info.</p>
<pre><code>SageMath version 9.2, Release Date: 2020-10-24
Using Python 3.9.5. Type "help()" for help.
</code></pre>
https://ask.sagemath.org/question/59039/weird-rounding-error-in-n-and-n/?comment=59067#post-id-59067That's a solid hypothesis!
I did find a better method:
sage: round(number,2)
12.09
This is probably how one should do it. I will however leave this post unanswered, as the original question is still not definitely answered.Thu, 16 Sep 2021 16:22:45 +0200https://ask.sagemath.org/question/59039/weird-rounding-error-in-n-and-n/?comment=59067#post-id-59067Comment by Max Alekseyev for <p>This is weird. While dabbling in sage, It seems I hit a rounding error.</p>
<pre><code>sage: number=180*arccos(14/205*sqrt(41)*sqrt(5))/pi
sage: n(number)
12.0947570770121
sage: n(number,digits=4)
12.10
</code></pre>
<p>Is there any good reason that I'm not getting 12.09 as a result? This happens in N as well.</p>
<p>Here's my sage info.</p>
<pre><code>SageMath version 9.2, Release Date: 2020-10-24
Using Python 3.9.5. Type "help()" for help.
</code></pre>
https://ask.sagemath.org/question/59039/weird-rounding-error-in-n-and-n/?comment=59066#post-id-59066It may be possible that `digits=4` is applied to every term in the expression before it's evaluated, resulting in the loss of precision, like in:
sage: 180*arccos(14/205*n(sqrt(41),digits=4)*n(sqrt(5),digits=4))/n(pi,digits=4)
12.10Thu, 16 Sep 2021 15:51:58 +0200https://ask.sagemath.org/question/59039/weird-rounding-error-in-n-and-n/?comment=59066#post-id-59066Comment by jakupl for <p>This is weird. While dabbling in sage, It seems I hit a rounding error.</p>
<pre><code>sage: number=180*arccos(14/205*sqrt(41)*sqrt(5))/pi
sage: n(number)
12.0947570770121
sage: n(number,digits=4)
12.10
</code></pre>
<p>Is there any good reason that I'm not getting 12.09 as a result? This happens in N as well.</p>
<p>Here's my sage info.</p>
<pre><code>SageMath version 9.2, Release Date: 2020-10-24
Using Python 3.9.5. Type "help()" for help.
</code></pre>
https://ask.sagemath.org/question/59039/weird-rounding-error-in-n-and-n/?comment=59055#post-id-59055Same thing happens in CoCalc, running SageMath version 9.3Thu, 16 Sep 2021 08:51:14 +0200https://ask.sagemath.org/question/59039/weird-rounding-error-in-n-and-n/?comment=59055#post-id-59055Comment by Max Alekseyev for <p>This is weird. While dabbling in sage, It seems I hit a rounding error.</p>
<pre><code>sage: number=180*arccos(14/205*sqrt(41)*sqrt(5))/pi
sage: n(number)
12.0947570770121
sage: n(number,digits=4)
12.10
</code></pre>
<p>Is there any good reason that I'm not getting 12.09 as a result? This happens in N as well.</p>
<p>Here's my sage info.</p>
<pre><code>SageMath version 9.2, Release Date: 2020-10-24
Using Python 3.9.5. Type "help()" for help.
</code></pre>
https://ask.sagemath.org/question/59039/weird-rounding-error-in-n-and-n/?comment=59050#post-id-59050As a temporary workaround you can use `n(n(number),digits=4)`Thu, 16 Sep 2021 05:03:58 +0200https://ask.sagemath.org/question/59039/weird-rounding-error-in-n-and-n/?comment=59050#post-id-59050Comment by Max Alekseyev for <p>This is weird. While dabbling in sage, It seems I hit a rounding error.</p>
<pre><code>sage: number=180*arccos(14/205*sqrt(41)*sqrt(5))/pi
sage: n(number)
12.0947570770121
sage: n(number,digits=4)
12.10
</code></pre>
<p>Is there any good reason that I'm not getting 12.09 as a result? This happens in N as well.</p>
<p>Here's my sage info.</p>
<pre><code>SageMath version 9.2, Release Date: 2020-10-24
Using Python 3.9.5. Type "help()" for help.
</code></pre>
https://ask.sagemath.org/question/59039/weird-rounding-error-in-n-and-n/?comment=59049#post-id-59049Something fishy is going on here. If we take the numerical value directly. it's rounded correctly:
sage: a = 12.0947570770121
sage: n(a,digits=4)
12.09Thu, 16 Sep 2021 04:34:39 +0200https://ask.sagemath.org/question/59039/weird-rounding-error-in-n-and-n/?comment=59049#post-id-59049