Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I got interested in this question because I'm trying to (de)serialize my data to &amp; from JSON.</p> <p>I think I have a clearer explanation (with less hand waiving) for why 17 decimal digits are sufficient to reconstruct the original number losslessly:</p> <p><img src="https://i.stack.imgur.com/LPZd2.png" alt="enter image description here"></p> <p>Imagine 3 number lines:<br> 1. for the original base 2 number<br> 2. for the rounded base 10 representation<br> 3. for the reconstructed number (same as #1 because both in base 2)<br></p> <p>When you convert to base 10, graphically, you choose the tic on the 2nd number line closest to the tic on the 1st. Likewise when you reconstruct the original from the rounded base 10 value.</p> <p>The critical observation I had was that in order to allow exact reconstruction, the base 10 step size (quantum) has to be &lt; the base 2 quantum. Otherwise, you inevitably get the bad reconstruction shown in red.</p> <p>Take the specific case of when the exponent is 0 for the base2 representation. Then the base2 quantum will be 2^-52 ~= 2.22 * 10^-16. The closest base 10 quantum that's less than this is 10^-16. Now that we know the required base 10 quantum, how many digits will be needed to encode all possible values? Given that we're only considering the case of exponent = 0, the dynamic range of values we need to represent is [1.0, 2.0). Therefore, 17 digits would be required (16 digits for fraction and 1 digit for integer part).</p> <p>For exponents other than 0, we can use the same logic:</p> <pre> exponent base2 quant. base10 quant. dynamic range digits needed --------------------------------------------------------------------- 1 2^-51 10^-16 [2, 4) 17 2 2^-50 10^-16 [4, 8) 17 3 2^-49 10^-15 [8, 16) 17 ... 32 2^-20 10^-7 [2^32, 2^33) 17 1022 9.98e291 1.0e291 [4.49e307,8.99e307) 17 </pre> <p>While not exhaustive, the table shows the trend that 17 digits are sufficient.</p> <p>Hope you like my explanation.</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