ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 30 Nov 2015 07:49:24 -0600Rounding error in sqrt(2) in Sagehttp://ask.sagemath.org/question/31162/rounding-error-in-sqrt2-in-sage/In the following code,
sage: floor(sqrt(2)*sqrt(2.)/2)==1
True
sage: (sqrt(2.)*sqrt(2.)/2)==1.0
False
sage: (sqrt(2.)*sqrt(2.)/2)
1.00000000000000
sage :((2^(1/2.))== sqrt(2.))
True
sage: (((2.^(1/2))* sqrt(2.)) == 2.)
False
why is there an approximation error?
Sat, 28 Nov 2015 12:00:03 -0600http://ask.sagemath.org/question/31162/rounding-error-in-sqrt2-in-sage/Answer by tmonteil for <p>In the following code,</p>
<pre><code>sage: floor(sqrt(2)*sqrt(2.)/2)==1
True
sage: (sqrt(2.)*sqrt(2.)/2)==1.0
False
sage: (sqrt(2.)*sqrt(2.)/2)
1.00000000000000
sage :((2^(1/2.))== sqrt(2.))
True
sage: (((2.^(1/2))* sqrt(2.)) == 2.)
False
</code></pre>
<p>why is there an approximation error?</p>
http://ask.sagemath.org/question/31162/rounding-error-in-sqrt2-in-sage/?answer=31177#post-id-31177When you write `sqrt(2.)`, you define a floating-point approximation of $\sqrt{2}$ with 53 bits of precision. So, since $\sqrt{2}$ is not rational (hence not of the form p/2^53), there is a rounding, so there is no reason that at the end of the computation you will get an exact result. However, you can see that the error is very small:
sage: two = 2.
sage: two.parent()
Real Field with 53 bits of precision
sage: sqrt(two)
1.41421356237310
sage: sqrt(two).parent()
Real Field with 53 bits of precision
sage: sqrt(two)*sqrt(two)/2
1.00000000000000
sage: sqrt(two)*sqrt(two)/2 - 1
2.22044604925031e-16
If you want exact computation, you can define 2 as an integer:
sage: two = 2
sage: two.parent()
Integer Ring
sage: sqrt(two)
sqrt(2)
sage: sqrt(two).parent()
Symbolic Ring
sage: sqrt(two)*sqrt(two)/2
1
sage: sqrt(two)*sqrt(two)/2 == 1
1 == 1
sage: bool(sqrt(two)*sqrt(two)/2 == 1)
True
or as an algebraic number:
sage: two = AA(2)
sage: two.parent()
Algebraic Real Field
sage: sqrt(two)
1.414213562373095?
sage: sqrt(two).parent()
Algebraic Real Field
sage: sqrt(two)*sqrt(two)/2
1.000000000000000?
sage: sqrt(two)*sqrt(two)/2 == 1
True
If you still want to work with floating-point numbers, but want a guarantee
on the error, you can work with real intervals:
sage: two = RIF(2)
sage: two.parent()
Real Interval Field with 53 bits of precision
sage: sqrt(two).parent()
Real Interval Field with 53 bits of precision
sage: sqrt(two)*sqrt(two)/2
1.000000000000000?
sage: sqrt(two)*sqrt(two)/2 == 1
False
sage: 1 in sqrt(two)*sqrt(two)/2
True
sage: (sqrt(two)*sqrt(two)/2).endpoints()
(0.999999999999999, 1.00000000000001)
If you want to increase the precision with more bits you can do:
sage: two = RealField(100)(2)
sage: two
2.0000000000000000000000000000
sage: two.parent()
Real Field with 100 bits of precision
sage: sqrt(two).parent()
Real Field with 100 bits of precision
Sun, 29 Nov 2015 03:41:19 -0600http://ask.sagemath.org/question/31162/rounding-error-in-sqrt2-in-sage/?answer=31177#post-id-31177Comment by kcrisman for <p>When you write <code>sqrt(2.)</code>, you define a floating-point approximation of $\sqrt{2}$ with 53 bits of precision. So, since $\sqrt{2}$ is not rational (hence not of the form p/2^53), there is a rounding, so there is no reason that at the end of the computation you will get an exact result. However, you can see that the error is very small:</p>
<pre><code>sage: two = 2.
sage: two.parent()
Real Field with 53 bits of precision
sage: sqrt(two)
1.41421356237310
sage: sqrt(two).parent()
Real Field with 53 bits of precision
sage: sqrt(two)*sqrt(two)/2
1.00000000000000
sage: sqrt(two)*sqrt(two)/2 - 1
2.22044604925031e-16
</code></pre>
<p>If you want exact computation, you can define 2 as an integer:</p>
<pre><code>sage: two = 2
sage: two.parent()
Integer Ring
sage: sqrt(two)
sqrt(2)
sage: sqrt(two).parent()
Symbolic Ring
sage: sqrt(two)*sqrt(two)/2
1
sage: sqrt(two)*sqrt(two)/2 == 1
1 == 1
sage: bool(sqrt(two)*sqrt(two)/2 == 1)
True
</code></pre>
<p>or as an algebraic number:</p>
<pre><code>sage: two = AA(2)
sage: two.parent()
Algebraic Real Field
sage: sqrt(two)
1.414213562373095?
sage: sqrt(two).parent()
Algebraic Real Field
sage: sqrt(two)*sqrt(two)/2
1.000000000000000?
sage: sqrt(two)*sqrt(two)/2 == 1
True
</code></pre>
<p>If you still want to work with floating-point numbers, but want a guarantee
on the error, you can work with real intervals:</p>
<pre><code>sage: two = RIF(2)
sage: two.parent()
Real Interval Field with 53 bits of precision
sage: sqrt(two).parent()
Real Interval Field with 53 bits of precision
sage: sqrt(two)*sqrt(two)/2
1.000000000000000?
sage: sqrt(two)*sqrt(two)/2 == 1
False
sage: 1 in sqrt(two)*sqrt(two)/2
True
sage: (sqrt(two)*sqrt(two)/2).endpoints()
(0.999999999999999, 1.00000000000001)
</code></pre>
<p>If you want to increase the precision with more bits you can do:</p>
<pre><code>sage: two = RealField(100)(2)
sage: two
2.0000000000000000000000000000
sage: two.parent()
Real Field with 100 bits of precision
sage: sqrt(two).parent()
Real Field with 100 bits of precision
</code></pre>
http://ask.sagemath.org/question/31162/rounding-error-in-sqrt2-in-sage/?comment=31200#post-id-31200Try it now.Mon, 30 Nov 2015 07:49:24 -0600http://ask.sagemath.org/question/31162/rounding-error-in-sqrt2-in-sage/?comment=31200#post-id-31200