Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>After that, however, it subtracted (dividend/2^31) from the result.</p> </blockquote> <p>Actually, it subtracts <code>dividend &gt;&gt; 31</code>, which is <code>-1</code> for negative <code>dividend</code>, and 0 for non-negative dividend, when right-shifting negative integers is arithmetic right-shift (and <code>int</code> is 32 bits wide).</p> <pre><code>0x6666667 = (2^34 + 6)/10 </code></pre> <p>So for <code>x &lt; 0</code>, we have, writing <code>x = 10*k + r</code> with <code>-10 &lt; r &lt;= 0</code>,</p> <pre><code>0x66666667 * (10*k+r) = (2^34+6)*k + (2^34 + 6)*r/10 = 2^34*k + 6*k + (2^34+6)*r/10 </code></pre> <p>Now, arithmetic right shift of negative integers yields the floor of <code>v / 2^n</code>, so</p> <pre><code>(0x66666667 * x) &gt;&gt; 34 </code></pre> <p>results in</p> <pre><code>k + floor((6*k + (2^34+6)*r/10) / 2^34) </code></pre> <p>So we need to see that</p> <pre><code>-2^34 &lt; 6*k + (2^34+6)*r/10 &lt; 0 </code></pre> <p>The right inequality is easy, both <code>k</code> and <code>r</code> are non-positive, and not both are 0.</p> <p>For the left inequality, a bit more analysis is needed.</p> <pre><code>r &gt;= -9 </code></pre> <p>so the absolute value of <code>(2^34+6)*r/10</code> is at most <code>2^34+6 - (2^34+6)/10</code>.</p> <pre><code>|k| &lt;= 2^31/10, </code></pre> <p>so <code>|6*k| &lt;= 3*2^31/5</code>.</p> <p>And it remains to verify that</p> <pre><code>6 + 3*2^31/5 &lt; (2^34+6)/10 1288490194 &lt; 1717986919 </code></pre> <p>Yup, true.</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