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, 12 Aug 2011 13:24:34 +0200numerical_approx() weirdness?https://ask.sagemath.org/question/8267/numerical_approx-weirdness/Hi all:
I recently discovered that the numerical_approx() function (aliased as n()) does not perform as i expected. Is the following behavior intentional?
sage: x = pi.n(digits=3)
sage: print x, x.n(), QQ(x), QQ(x).n()
3.14 3.14160156250000 333/106 3.14150943396226
Where are the extra digits of `x` coming from to produce the last three results in the output above?
Tue, 09 Aug 2011 22:51:39 +0200https://ask.sagemath.org/question/8267/numerical_approx-weirdness/Comment by kcrisman for <p>Hi all:</p>
<p>I recently discovered that the numerical_approx() function (aliased as n()) does not perform as i expected. Is the following behavior intentional? </p>
<pre><code>sage: x = pi.n(digits=3)
sage: print x, x.n(), QQ(x), QQ(x).n()
3.14 3.14160156250000 333/106 3.14150943396226
</code></pre>
<p>Where are the extra digits of <code>x</code> coming from to produce the last three results in the output above?</p>
https://ask.sagemath.org/question/8267/numerical_approx-weirdness/?comment=21389#post-id-21389Thanks for the clarification!Fri, 12 Aug 2011 13:24:34 +0200https://ask.sagemath.org/question/8267/numerical_approx-weirdness/?comment=21389#post-id-21389Comment by araichev for <p>Hi all:</p>
<p>I recently discovered that the numerical_approx() function (aliased as n()) does not perform as i expected. Is the following behavior intentional? </p>
<pre><code>sage: x = pi.n(digits=3)
sage: print x, x.n(), QQ(x), QQ(x).n()
3.14 3.14160156250000 333/106 3.14150943396226
</code></pre>
<p>Where are the extra digits of <code>x</code> coming from to produce the last three results in the output above?</p>
https://ask.sagemath.org/question/8267/numerical_approx-weirdness/?comment=21405#post-id-21405Let me elaborate. I expected that `x` = 3.14, so that `x.n()` = 3.14000000000000 and `QQ(x)` = 157/50. But `x == 3.14` returns `False`, `x.n()` = 3.14160156250000 and `QQ(x)`= 333/106. Huh? Why are there more than 3 digits stored in x? Wed, 10 Aug 2011 00:45:39 +0200https://ask.sagemath.org/question/8267/numerical_approx-weirdness/?comment=21405#post-id-21405Answer by kcrisman for <p>Hi all:</p>
<p>I recently discovered that the numerical_approx() function (aliased as n()) does not perform as i expected. Is the following behavior intentional? </p>
<pre><code>sage: x = pi.n(digits=3)
sage: print x, x.n(), QQ(x), QQ(x).n()
3.14 3.14160156250000 333/106 3.14150943396226
</code></pre>
<p>Where are the extra digits of <code>x</code> coming from to produce the last three results in the output above?</p>
https://ask.sagemath.org/question/8267/numerical_approx-weirdness/?answer=12565#post-id-12565They don't come from anywhere special.
sage: x = pi.n(digits=3)
sage: y = QQ(x)
sage: type(y)
<type 'sage.rings.rational.Rational'>
sage: y.n()
3.14150943396226
sage: y.n(digits=100)
3.141509433962264150943396226415094339622641509433962264150943396226415094339622641509433962264150943
sage: n(333/106)
3.14150943396226
Once you turn `x` into a rational, it behaves as any other rational would. In particular, it has an eventually repeating decimal expansion, etc. The point is that you tried to turn a low-precision number into a rational, and this is what happens. Sorry that the rationals don't 'hold precision', but I don't think this would be intended.Tue, 09 Aug 2011 23:25:32 +0200https://ask.sagemath.org/question/8267/numerical_approx-weirdness/?answer=12565#post-id-12565Answer by Jason Bandlow for <p>Hi all:</p>
<p>I recently discovered that the numerical_approx() function (aliased as n()) does not perform as i expected. Is the following behavior intentional? </p>
<pre><code>sage: x = pi.n(digits=3)
sage: print x, x.n(), QQ(x), QQ(x).n()
3.14 3.14160156250000 333/106 3.14150943396226
</code></pre>
<p>Where are the extra digits of <code>x</code> coming from to produce the last three results in the output above?</p>
https://ask.sagemath.org/question/8267/numerical_approx-weirdness/?answer=12569#post-id-12569When you write `pi.n(digits=3)`, you do not get the rational number 314/100. What you get is a floating point number $x$ with a guarantee that $|\pi - x| \le 0.01$ (I believe this number also "knows" how many digits you care about; that's why `print` shows exactly 3 digits). Because floating point numbers are internally stored in base 2, it is impossible to represent 314/100 exactly. Thu, 11 Aug 2011 13:53:12 +0200https://ask.sagemath.org/question/8267/numerical_approx-weirdness/?answer=12569#post-id-12569Comment by kcrisman for <p>When you write <code>pi.n(digits=3)</code>, you do not get the rational number 314/100. What you get is a floating point number $x$ with a guarantee that $|\pi - x| \le 0.01$ (I believe this number also "knows" how many digits you care about; that's why <code>print</code> shows exactly 3 digits). Because floating point numbers are internally stored in base 2, it is impossible to represent 314/100 exactly. </p>
https://ask.sagemath.org/question/8267/numerical_approx-weirdness/?comment=21390#post-id-21390I think that Jason's point is that with y=3.14, you get something with a lot of (base 2) precision, which can be nicely turned into a fraction, while when you limit it to 3 digits, this is the best you can get. At least, that's how I understand Jason's answer, and I think he's right.Fri, 12 Aug 2011 13:24:15 +0200https://ask.sagemath.org/question/8267/numerical_approx-weirdness/?comment=21390#post-id-21390Comment by araichev for <p>When you write <code>pi.n(digits=3)</code>, you do not get the rational number 314/100. What you get is a floating point number $x$ with a guarantee that $|\pi - x| \le 0.01$ (I believe this number also "knows" how many digits you care about; that's why <code>print</code> shows exactly 3 digits). Because floating point numbers are internally stored in base 2, it is impossible to represent 314/100 exactly. </p>
https://ask.sagemath.org/question/8267/numerical_approx-weirdness/?comment=21393#post-id-21393Thanks, but i'm still wondering why i don't get the floating point number 3.14. For instance i get the correct n() expansion and rational with the following commands. `sage: y = 3.14`, `sage: y.n(),QQ(y)`, `(3.14000000000000, 157/50)`
Thu, 11 Aug 2011 22:41:54 +0200https://ask.sagemath.org/question/8267/numerical_approx-weirdness/?comment=21393#post-id-21393