Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, ideally, you'd wait until Intel fixes the bug or provides a workaround :-)</p> <p>But if you want to detect <code>NaN</code> and <code>Inf</code> from IEEE754 values, map it to an integer (32 or 64 bit depending on whether it's single or double precision) and check if the exponent bits are all 1. This indicates those two cases.</p> <p>You can distinguish between <code>NaN</code> and <code>Inf</code> by checking the high order bit of the mantissa. If it's 1, that's <code>NaN</code> otherwise <code>Inf</code>.</p> <p><code>+/-Inf</code> is dictated by the sign bit.</p> <p>For single precision (32-bit values), the sign is the high-order bit (b31), exponent is the next eight bits (plus a 23-bit mantissa). For double precision, the sign is still the high-order bit but the exponent is eleven bits (plus 52 bits for the mantissa).</p> <p><a href="http://en.wikipedia.org/wiki/IEEE_754-2008" rel="noreferrer">Wikipedia</a> has all the gory details.</p> <p>The following code shows you how the encoding works.</p> <pre><code>#include &lt;stdio.h&gt; static void decode (char *s, double x) { long y = *(((long*)(&amp;x))+1); printf("%08x ",y); if ((y &amp; 0x7ff80000L) == 0x7ff80000L) { printf ("NaN (%s)\n", s); return; } if ((y &amp; 0xfff10000L) == 0x7ff00000L) { printf ("+Inf (%s)\n", s); return; } if ((y &amp; 0xfff10000L) == 0xfff00000L) { printf ("-Inf (%s)\n", s); return; } printf ("%e (%s)\n", x, s); } int main (int argc, char *argv[]) { double dvar; printf ("sizeof double = %d\n", sizeof(double)); printf ("sizeof long = %d\n", sizeof(long)); dvar = 1.79e308; dvar = dvar * 10000; decode ("too big", dvar); dvar = -1.79e308; dvar = dvar * 10000; decode ("too big and negative", dvar); dvar = -1.0; dvar = sqrt(dvar); decode ("imaginary", dvar); dvar = -1.79e308; decode ("normal", dvar); return 0; } </code></pre> <p>and it outputs:</p> <pre><code>sizeof double = 8 sizeof long = 4 7ff00000 +Inf (too big) fff00000 -Inf (too big and negative) fff80000 NaN (imaginary) ffefdcf1 -1.790000e+308 (normal) </code></pre> <p>Just keep in mind that this code (but not the method) depends a great deal on the sizes of your longs which is not overly portable. But, if you have to bit-fiddle to get the information, you've already entered that territory :-)</p> <p>As an aside, I've always found <a href="http://www.h-schmidt.net/FloatApplet/IEEE754.html" rel="noreferrer">Harald Schmidt's IEEE754 converter</a> very useful for floating point analysis.</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