Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A double has the precision of 52 binary digits or 15.95 decimal digits. See <a href="http://en.wikipedia.org/wiki/IEEE_754-2008" rel="nofollow">http://en.wikipedia.org/wiki/IEEE_754-2008</a>. You need at least 16 decimal digits to record the full precision of a double in all cases. [But see fourth edit, below].</p> <p>By the way, this means significant digits.</p> <p>Answer to OP edits:</p> <p>Your floating point to decimal string runtime is outputing way more digits than are significant. A double can only hold 52 bits of significand (actually, 53, if you count a "hidden" 1 that is not stored). That means the the resolution is not more than 2 ^ -53 = 1.11e-16.</p> <p>For example: 1 + 2 ^ -52 = 1.0000000000000002220446049250313 . . . .</p> <p>Those decimal digits, .0000000000000002220446049250313 . . . . are the smallest binary "step" in a double <strong>when converted to decimal.</strong></p> <p>The "step" inside the double is:</p> <p>.0000000000000000000000000000000000000000000000000001 <strong>in binary.</strong></p> <p>Note that the binary step is exact, while the decimal step is inexact.</p> <p>Hence the decimal representation above,</p> <p>1.0000000000000002220446049250313 . . . </p> <p>is an inexact representation of the exact binary number: </p> <p>1.0000000000000000000000000000000000000000000000000001.</p> <p><strong>Third Edit:</strong></p> <p>The next possible value for a double, which in exact binary is:</p> <p>1.0000000000000000000000000000000000000000000000000010</p> <p>converts inexactly in decimal to </p> <p>1.0000000000000004440892098500626 . . . .</p> <p>So all of those extra digits in the decimal are not really significant, they are just <em>base conversion artifacts.</em></p> <p><strong>Fourth Edit:</strong></p> <p>Though a double stores at most 16 significant decimal digits, <strong>sometimes 17 decimal digits are necessary to represent the number</strong>. The reason has to do with <em>digit slicing</em>.</p> <p>As I mentioned above, there are 52 + 1 binary digits in the double. The "+ 1" is an assumed leading 1, and is neither stored nor significant. In the case of an integer, those 52 binary digits form a number between 0 and 2^53 - 1. How many decimal digits are necessary to store such a number? Well, log_10 (2^53 - 1) is about 15.95. So at most 16 decimal digits are necessary. Let's label these d_0 to d_15.</p> <p>Now consider that IEEE floating point numbers also have an <em>binary</em> exponent. What happens when we increment the exponet by, say, 2? We have multiplied our 52-bit number, whatever it was, by 4. Now, instead of our 52 binary digits aligning perfectly with our decimal digits d_0 to d_15, we have some significant binary digits represented in d_16. However, since we multiplied by something less than 10, we still have significant binary digits represented in d_0. So our 15.95 decimal digits now occuply d_1 to d_15, plus some upper bits of d_0 and some lower bits of d_16. This is why 17 decimal digits are sometimes needed to represent a IEEE double.</p> <p><strong>Fifth Edit</strong></p> <p>Fixed numerical errors</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