Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This has to do with how computers store floating point numbers. A detailed description of this is <a href="http://docs.sun.com/source/806-3568/ncg_goldberg.html" rel="nofollow noreferrer">here</a>. However, for your case, the quick solution is to check not the printed representation of <code>point.x</code> but if <code>point.x</code> is equal to <code>4.7</code>. So...</p> <pre><code>&gt;&gt;&gt; point = Point(4.7, 8.2) &gt;&gt;&gt; point.x == 4.7 True </code></pre> <p>Or better:</p> <pre><code>&gt;&gt;&gt; point = Point(4.7, 8.2) &gt;&gt;&gt; eps = 2**-53 #get epsilon for standard double precision number &gt;&gt;&gt; -eps &lt;= point.x - 4.7 &lt;= eps True </code></pre> <p>Where <code>eps</code> is the maximum value for rounding errors in floating-point arithmetic. For details on epsilon, see <a href="http://en.wikipedia.org/wiki/Machine_epsilon" rel="nofollow noreferrer">here</a>.</p> <p><strong>EDIT:</strong> <code>-eps &lt;= point.x - 4.7 &lt;= eps</code> is equivalent to <code>abs(point.x - 4.7) &lt;= eps</code>. I only add this because not everyone is familiar with Python's chaining of comparison operators.</p> <p><strong>EDIT 2:</strong> Since you mentioned numpy, numpy has a method to get the eps without calculating it yourself. Use <code>eps = numpy.finfo(float).eps</code> instead of <code>2**-53</code> if you're using numpy. Note that the numpy epsilon is for some reason bigger than it should be and is equal to <code>2**-52</code> rather than <code>2**-53</code>. I have no idea why this is.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload