Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://docs.python.org/2/library/decimal.html" rel="noreferrer">Python's implementation</a> follows the <a href="http://en.wikipedia.org/wiki/IEEE_754-1985" rel="noreferrer">IEEE-754 standard</a> pretty well, which you can use as a guidance, but it relies on the underlying system it was compiled on, so <a href="http://www.python.org/dev/peps/pep-0754/" rel="noreferrer">platform differences</a> may occur. Recently¹, a fix has been applied that allows <a href="https://docs.python.org/3.4/library/functions.html#float" rel="noreferrer">"infinity" as well as "inf"</a>, but that's of minor importance here.</p> <p>The following sections equally well apply to any language that implements IEEE floating point arithmetic correctly, it is not specific to just Python.</p> <h3>Comparison for inequality</h3> <p>When dealing with infinity and greater-than <code>&gt;</code> or less-than <code>&lt;</code> operators, the following counts:</p> <ul> <li>any number including <code>+inf</code> is higher than <code>-inf</code></li> <li>any number including <code>-inf</code> is lower than <code>+inf</code> </li> <li><code>+inf</code> is <a href="http://compilers.iecc.com/comparch/article/98-07-134" rel="noreferrer">neither higher nor lower</a> than <code>+inf</code></li> <li><code>-inf</code> is neither higher nor lower than <code>-inf</code></li> <li>any comparison involving <code>NaN</code> is false (<code>inf</code> is neither higher, nor lower than <code>NaN</code>)</li> </ul> <h3>Comparison for equality</h3> <p>When compared for equality, <code>+inf</code> and <code>+inf</code> are equal, as are <code>-inf</code> and <code>-inf</code>. This is a much debated issue and may sound controversial to you, but it's in the IEEE standard and Python behaves just like that.</p> <p>Of course, <code>+inf</code> is unequal to <code>-inf</code> and everything, including <code>NaN</code> itself, is unequal to <code>NaN</code>.</p> <h3>Calculations with infinity</h3> <p>Most calculations with infinity will yield infinity, unless both operands are infinity, when the operation division or modulo, or with multiplication with zero, there are some special rules to keep in mind:</p> <ul> <li>when multiplied by zero, for which the result is undefined, it yields <code>NaN</code></li> <li>when dividing any number (except infinity itself) by infinity, which yields <code>0.0</code> or <a href="http://en.wikipedia.org/wiki/Signed_zero" rel="noreferrer"><code>-0.0</code></a>².</li> <li>when dividing (including modulo) positive or negative infinity by positive or negative infinity, the result is undefined, so <code>NaN</code>.</li> <li>when subtracting, the results may be surprising, but follow <a href="https://math.stackexchange.com/questions/60766/what-is-the-result-of-infinity-minus-infinity">common math sense</a>: <ul> <li>when doing <code>inf - inf</code>, the result is undefined: <code>NaN</code>;</li> <li>when doing <code>inf - -inf</code>, the result is <code>inf</code>;</li> <li>when doing <code>-inf - inf</code>, the result is <code>-inf</code>;</li> <li>when doing <code>-inf - -inf</code>, the result is undefined: <code>NaN</code>.</li> </ul></li> <li>when adding, it can be similarly surprising too: <ul> <li>when doing <code>inf + inf</code>, the result is <code>inf</code>;</li> <li>when doing <code>inf + -inf</code>, the result is undefined: <code>NaN</code>;</li> <li>when doing <code>-inf + inf</code>, the result is undefined: <code>NaN</code>;</li> <li>when doing <code>-inf + -inf</code>, the result is <code>-inf</code>.</li> </ul></li> <li>using <code>math.pow</code>, <code>pow</code> or <code>**</code> is tricky, as it doesn't behave as it should. It throws an overflow exception when the result with two real numbers is too high to fit a double precision float (it should return infinity), but when the input is <code>inf</code> or <code>-inf</code>, it behaves correctly and returns either <code>inf</code> or <code>0.0</code>. When the second argument is <code>NaN</code>, it returns <code>NaN</code>, unless the first argument is <code>1.0</code>. There are more issues, not all <a href="https://docs.python.org/3.4/library/math.html#math.pow" rel="noreferrer">covered in the docs</a>.</li> <li><p><code>math.exp</code> suffers the same issues as <code>math.pow</code>. A solution to fix this for overflow is to use code similar to this:</p> <pre><code>try: res = math.exp(420000) except OverflowError: res = float('inf') </code></pre></li> </ul> <h3>Notes</h3> <p><em>Note 1:</em> as an additional caveat, that as defined by the IEEE standard, if your calculation result under-or overflows, the result will not be an under- or overflow error, but positive or negative infinity: <code>1e308 * 10.0</code> yields <code>inf</code>.</p> <p><em>Note 2:</em> because any calculation with <code>NaN</code> returns <code>NaN</code> and any comparison to <code>NaN</code>, including <code>NaN</code> itself is <code>false</code>, you should use the <a href="https://docs.python.org/2.7/library/math.html?highlight=math.isnan#math.isnan" rel="noreferrer"><code>math.isnan</code></a> function to determine if a number is indeed <code>NaN</code>.</p> <p><em>Note 3:</em> though Python supports writing <code>float('-NaN')</code>, the sign is ignored, because there exists no sign on <code>NaN</code> internally. If you divide <code>-inf / +inf</code>, the result is <code>NaN</code>, not <code>-NaN</code> (there is no such thing).</p> <p><em>Note 4:</em> be careful to rely on any of the above, as Python relies on the C or Java library it was compiled for and not all underlying systems implement all this behavior correctly. If you want to be sure, test for infinity prior to doing your calculations.</p> <p>¹) Recently means since <a href="https://docs.python.org/3.2/library/functions.html#float" rel="noreferrer">version 3.2</a>.<br> ²) Floating points support positive and negative zero, so: <code>x / float('inf')</code> keeps its sign and <code>-1 / float('inf')</code> yields <code>-0.0</code>, <code>1 / float(-inf)</code> yields <code>-0.0</code>, <code>1 / float('inf')</code> yields <code>0.0</code> and <code>-1/ float(-inf)</code> yields <code>0.0</code>. In addition, <a href="https://stackoverflow.com/questions/4083401/negative-zero-in-python"><code>0.0 == -0.0</code> is <code>true</code></a>, you have to manually check the sign if you don't want it to be true.</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