Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you use <code>BitConverter.DoubleToInt64Bits</code>, it gives you the <code>double</code> value already encoded in IEEE 754 format. This means the significand is encoded with an implicit leading bit. (“Significand” is the preferred term for the fraction portion of a floating-point value and is used in IEEE 754. A significand is linear. A mantissa is logarithmic. “Mantissa” stems from the days when people had to use logarithms and paper and tables of functions to do crude calculations.) To recover the unencoded significand, you would have to restore the implicit bit.</p> <p>That is not hard. Once you have separated the sign bit, the encoded exponent (as an integer), and the encoded significand (as an integer), then, for 64-bit binary floating-point:</p> <ul> <li>If the encoded exponent is its maximum (2047) and the encoded significand is non-zero, the value is a NaN. There is additional information in the significand about whether the NaN is signaling or not and other user- or implementation-defined information.</li> <li>If the encoded exponent is its maximum and the encoded significand is zero, the value is an infinity (+ or – according to the sign).</li> <li>If the encoded exponent is zero, the implicit bit is zero, the actual significand is the encoded significand multiplied by 2<sup>–52</sup>, and the actual exponent is one minus the bias (1023) (so –1022).</li> <li>Otherwise, the implicit bit is one, the actual significand is the encoded significand first multiplied by 2<sup>–52</sup> and then added to one, and the actual exponent is the encoded exponent minus the bias (1023).</li> </ul> <p>(If you want to work with integers and not have fractions for the significand, you can omit the multiplications by 2<sup>–52</sup> and add –52 to the exponent instead. In the last case, the significand is added to 2<sup>52</sup> instead of to one.)</p> <p>There is an alternative method that avoids <code>BitConverter</code> and the IEEE-754 encoding. If you can call the <code>frexp</code> routine from C#, it will return the fraction and exponent mathematically instead of as encodings. First, handle zeroes, infinities, and NaNs separately. Then use:</p> <pre><code>int exponent; double fraction = frexp(value, &amp;exponent); </code></pre> <p>This sets <code>fraction</code> to a value with magnitude in [½, 1) and <code>exponent</code> such that <code>fraction</code>•2<sup><code>exponent</code></sup> equals <code>value</code>. (Note that <code>fraction</code> still has the sign; you might want to separate that and use the absolute value.)</p> <p>At this point, you can scale <code>fraction</code> as desired (and adjust <code>exponent</code> accordingly). To scale it so that it is an odd integer, you could multiply it by two repeatedly until it has no fractional part.</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. 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