Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The difference between <code>str</code> and <code>repr</code></p> <pre><code>&gt;&gt;&gt; import scipy &gt;&gt;&gt; pi = scipy.pi &gt;&gt;&gt; str(pi) '3.14159265359' &gt;&gt;&gt; repr(pi) '3.1415926535897931' </code></pre> <p><code>str</code> truncates floating point numbers to 12 digits, where repr gives the internal representation (as a string).</p> <p>EDIT: So in summary, the problem arose because you rounded prematurely and are calculating the modulus of something via a number that's very close to it. With floating point numbers, rounding is inevitably involved in converting decimal numbers into binary.</p> <p>First, do an example of how rounding hurts you with actual math (not floating point math). Look at (3.14+3.14) % (3.14+3.14), which is obviously zero. Now what would happen if we rounded the digits to one decimal digit first on one side? Well (3.1+3.1) % (3.14+3.14) = 6.2 % (6.28) = 6.2 (what google gave you). Or if you did round(3.14159,5) + round(3.14159,5) % (3.14159 + 3.14159) = 6.2832 % 6.28318 = 2e-5.</p> <p>So in by rounding to N digits (by using <code>str</code> which effectively rounds the numbers), your calculation is only accurate to less than N digits. To have this work going forward force rounding at some higher digit (keeping two calculated digits for safety) is necessary. E.g., str rounds at digit 12, so maybe we should round at digit 10.</p> <pre><code>&gt;&gt;&gt; round(6.28187631024 + 0.001308996939,10) % (round(pi * 2,10)) 0 </code></pre>
 

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