Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is not possible to achieve perfectly accurate comparisons of numbers when using <code>float</code> values in Python. This is because floating point values are inherently limited in precision.</p> <p>In your example, the number <code>1.5999999999999999999999</code> will have the same floating point representation as <code>1.6</code> (as mentioned by nneonneo in a comment). What may be more surprising to you is that the floating point representation is not exactly equal to either of the source numbers (to 20 decimal places, it is <code>1.60000000000000008882</code>). Python conceals this a bit from you, since if you print the number out it will display <code>1.6</code> even though it is slightly larger.</p> <p>The extra bit at the end is rounding error, because there is not any non-repeating binary representation of 1.6. Just like one third can only be represented exactly by an infinitely repeating decimal, <code>1.6</code> requires an infinite number of bits to represent exactly in binary. The error comes from stopping the representation after a certain number of digits and rounding the rest (like representing <code>1/3</code> as <code>0.333333</code>, and rounding down the rest of the repeating 3s).</p> <p>There are a number of ways that you can write code to mitigate the effects of floating point errors, but you can't make them go away entirely, except, I suppose, by not using floating point values for situations where extremely high precision is required. In Python, if you want more precision (at the cost of some performance) you can instead use the Decimal class from the standard library:</p> <pre><code>from decimal import Decimal d1 = Decimal("1.5999999999999999999999") d2 = Decimal("1.6") print(d1 &gt;= d2) # prints: False </code></pre>
    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.
    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