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.Sun, 20 Oct 2013 00:16:05 -0500boolean with numpyhttp://ask.sagemath.org/question/10580/boolean-with-numpy/Hello!
Please explain me why
numpy.array([1/5])[0] == 0.2
is False but
numpy.array([1/5.])[0] == 0.2
is True. At 2nd line of code there is point after 5. <br>
What should I do to get true at first line of code? <br>
Thanks a lot.Sun, 29 Sep 2013 01:28:25 -0500http://ask.sagemath.org/question/10580/boolean-with-numpy/Answer by tmonteil for <p>Hello!
Please explain me why</p>
<pre><code>numpy.array([1/5])[0] == 0.2
</code></pre>
<p>is False but</p>
<pre><code>numpy.array([1/5.])[0] == 0.2
</code></pre>
<p>is True. At 2nd line of code there is point after 5. <br/>
What should I do to get true at first line of code? <br/>
Thanks a lot.</p>
http://ask.sagemath.org/question/10580/boolean-with-numpy/?answer=15509#post-id-15509For Sage, `1/5` is a rational number, whereas `1/5.` is a floating point number:
sage: (1/5).parent()
Rational Field
sage: (1/5.).parent()
Real Field with 53 bits of precision
Now, when you put them in a numpy array, they are converted to a type named `numpy.float64`:
sage: type(numpy.array([1/5])[0])
<type 'numpy.float64'>
sage: type(numpy.array([1/5.])[0])
<type 'numpy.float64'>
As you can see, this conversion do not lead to the same approximation for `1/5` and `1/5.`, they seem not rounded to the same direction:
sage: numpy.float64(1/5)
0.19999999999999998
sage: numpy.float64(1/5.)
0.20000000000000001
sage: numpy.float64(0.2)
0.20000000000000001
This explains your problem. There are various issues about the rounding direction in Sage and the tools its uses, and i agree there should be a general audit.
Now, if you want 1/5 to be rounded in the right direction, you can first round it using `RealField()` which seems more consistent than `numpy.float64`:
sage: numpy.array([RR(1/5)])[0] == 0.2
True
Sun, 29 Sep 2013 02:15:15 -0500http://ask.sagemath.org/question/10580/boolean-with-numpy/?answer=15509#post-id-15509Comment by AndreWin for <p>For Sage, <code>1/5</code> is a rational number, whereas <code>1/5.</code> is a floating point number:</p>
<pre><code>sage: (1/5).parent()
Rational Field
sage: (1/5.).parent()
Real Field with 53 bits of precision
</code></pre>
<p>Now, when you put them in a numpy array, they are converted to a type named <code>numpy.float64</code>:</p>
<pre><code>sage: type(numpy.array([1/5])[0])
<type 'numpy.float64'>
sage: type(numpy.array([1/5.])[0])
<type 'numpy.float64'>
</code></pre>
<p>As you can see, this conversion do not lead to the same approximation for <code>1/5</code> and <code>1/5.</code>, they seem not rounded to the same direction:</p>
<pre><code>sage: numpy.float64(1/5)
0.19999999999999998
sage: numpy.float64(1/5.)
0.20000000000000001
sage: numpy.float64(0.2)
0.20000000000000001
</code></pre>
<p>This explains your problem. There are various issues about the rounding direction in Sage and the tools its uses, and i agree there should be a general audit.</p>
<p>Now, if you want 1/5 to be rounded in the right direction, you can first round it using <code>RealField()</code> which seems more consistent than <code>numpy.float64</code>:</p>
<pre><code>sage: numpy.array([RR(1/5)])[0] == 0.2
True
</code></pre>
http://ask.sagemath.org/question/10580/boolean-with-numpy/?comment=16975#post-id-16975Thanks a lot!)Sun, 29 Sep 2013 02:19:35 -0500http://ask.sagemath.org/question/10580/boolean-with-numpy/?comment=16975#post-id-16975Answer by tmonteil for <p>Hello!
Please explain me why</p>
<pre><code>numpy.array([1/5])[0] == 0.2
</code></pre>
<p>is False but</p>
<pre><code>numpy.array([1/5.])[0] == 0.2
</code></pre>
<p>is True. At 2nd line of code there is point after 5. <br/>
What should I do to get true at first line of code? <br/>
Thanks a lot.</p>
http://ask.sagemath.org/question/10580/boolean-with-numpy/?answer=15563#post-id-15563This has been fixed now (trac ticket #14416), so if you upgrade, you will now get:
sage: numpy.array([1/5])[0] == 0.2
True
sage: numpy.float64(1/5)
0.20000000000000001
Sat, 19 Oct 2013 10:52:42 -0500http://ask.sagemath.org/question/10580/boolean-with-numpy/?answer=15563#post-id-15563Comment by AndreWin for <p>This has been fixed now (trac ticket #14416), so if you upgrade, you will now get:</p>
<pre><code>sage: numpy.array([1/5])[0] == 0.2
True
sage: numpy.float64(1/5)
0.20000000000000001
</code></pre>
http://ask.sagemath.org/question/10580/boolean-with-numpy/?comment=16904#post-id-16904Thanks a lot!)Sun, 20 Oct 2013 00:16:05 -0500http://ask.sagemath.org/question/10580/boolean-with-numpy/?comment=16904#post-id-16904