Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are dividing using the true division operator <code>/</code>, which will <em>always</em> result in floating point values. Use floor division instead, <code>//</code>, to get integer results:</p> <pre><code>&gt;&gt;&gt; a = 313585730727764141482831584863 &gt;&gt;&gt; b = a*2 &gt;&gt;&gt; c = b // 2 &gt;&gt;&gt; a == c True </code></pre> <p>Your computer hardware cannot handle float values with the required precision.</p> <p>The alternative is to use <a href="http://docs.python.org/3/library/decimal.html" rel="nofollow"><code>decimal.Decimal()</code> values</a>, but this will result in slower arithmetic operations.</p> <p>In Python 2, the <code>/</code> operator <em>is</em> the floor division operator, but when applied to integers <em>only</em>. To get the same behaviour in Python 2, add:</p> <pre><code>from __future__ import division </code></pre> <p>The behaviour was changed because the difference between using only integer operators and using at least one float argument was confusing.</p> <p>In other words, the standard Python 2 <code>/</code> operator is a <em>different</em> beast from the Python 3 <code>/</code> division operator. When applied to two integer operands, it acts just like the <code>//</code> floor division operator in Python 3. But if either one of the two operands is a float instead, then it acts like the <code>/</code> float division operator <em>instead</em>. The above <code>__future__</code> import swaps out the Python 2 <code>/</code> operator for the true division operator found in Python 3.</p> <p>You can see this when disassembling Python bytecode:</p> <pre><code>&gt;&gt;&gt; import dis &gt;&gt;&gt; def olddivision(x, y): return x / y ... &gt;&gt;&gt; dis.dis(olddivision) 1 0 LOAD_FAST 0 (x) 3 LOAD_FAST 1 (y) 6 BINARY_DIVIDE 7 RETURN_VALUE &gt;&gt;&gt; from __future__ import division &gt;&gt;&gt; def newdivision(x, y): return x / y ... &gt;&gt;&gt; dis.dis(newdivision) 1 0 LOAD_FAST 0 (x) 3 LOAD_FAST 1 (y) 6 BINARY_TRUE_DIVIDE 7 RETURN_VALUE </code></pre> <p>the <code>__future__</code> import caused the Python compiler to use a different bytecode for the division operator, swapping <code>BINARY_DIVIDE</code> for <code>BINARY_TRUE_DIVIDE</code>.</p>
 

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