title: How is infinity represented in a C double?
body: <p>I learned from the book <em>Computer Systems: A Programmer's Perspective</em> that the IEEE standard requires the double precision floating number to be represented using the following 64-bit binary format:</p>
  
  <ul>
  <li>s: 1 bit for sign</li>
  <li>exp: 11 bits for exponent</li>
  <li>frac: 52 bits for fraction</li>
  </ul>
  
  <p>The +infinity is represented as a special value with the following pattern:</p>
  
  <ul>
  <li>s = 0</li>
  <li>all exp bits are 1</li>
  <li>all fraction bits are 0</li>
  </ul>
  
  <p>And I think the full 64-bit for double should be in the following order:</p>
  
  <p>(s)(exp)(frac)</p>
  
  <p>So I write the following C code to verify it:</p>
  
  <pre><code>//Check the infinity
  double x1 = (double)0x7ff0000000000000;  // This should be the +infinity
  double x2 = (double)0x7ff0000000000001; //  Note the extra ending 1, x2 should be NaN
  printf("\nx1 = %f, x2 = %f sizeof(double) = %d", x1,x2, sizeof(x2));
  if (x1 == x2)
      printf("\nx1 == x2");
  else
      printf("\nx1 != x2");
  </code></pre>
  
  <p>But the result is:</p>
  
  <pre><code>x1 = 9218868437227405300.000000, x2 = 9218868437227405300.000000 sizeof(double) = 8
  x1 == x2
  </code></pre>
  
  <p>Why is the number a valid number rather than some infinity error?</p>
  
  <p>Why x1==x2?</p>
  
  <p>(I am using the MinGW GCC compiler.)</p>
  
  <h2>ADD 1</h2>
  
  <p>I modified the code as below and the validated the Infinity and NaN successfully.</p>
  
  <pre><code>//Check the infinity and NaN
  unsigned long long x1 = 0x7ff0000000000000ULL; // +infinity as double
  unsigned long long x2 = 0xfff0000000000000ULL; // -infinity as double
  unsigned long long x3 = 0x7ff0000000000001ULL; // NaN as double
  double y1 =* ((double *)(&amp;x1));
  double y2 =* ((double *)(&amp;x2));
  double y3 =* ((double *)(&amp;x3));
  
  printf("\nsizeof(long long) = %d", sizeof(x1));
  printf("\nx1 = %f, x2 = %f, x3 = %f", x1, x2, x3); // %f is good enough for output
  printf("\ny1 = %f, y2 = %f, y3 = %f", y1, y2, y3);
  </code></pre>
  
  <p>The result is:</p>
  
  <pre><code>sizeof(long long) = 8
  x1 = 1.#INF00, x2 = -1.#INF00, x3 = 1.#SNAN0
  y1 = 1.#INF00, y2 = -1.#INF00, y3 = 1.#QNAN0
  </code></pre>
  
  <p>The detailed output looks a bit strange, but I think the point is clear.</p>
  
  <p>PS.: It seems the pointer conversion is not necessary. Just use <code>%f</code> to tell the <code>printf</code> function to interpret the <code>unsigned long long</code> variable in <code>double</code> format.</p>
  
  <h2>ADD 2</h2>
  
  <p>Out of curiosity, I checked the bit represetation of the variables with the following code.</p>
  
  <pre><code>typedef unsigned char *byte_pointer;
  
  void show_bytes(byte_pointer start, int len)
  {
      int i;
      for (i = len-1; i&gt;=0; i--)
      {
          printf("%.2x", start[i]);
      }
      printf("\n");
  }
  </code></pre>
  
  <p>And I tried the code below:</p>
  
  <pre><code>//check the infinity and NaN
  unsigned long long x1 = 0x7ff0000000000000ULL; // +infinity as double
  unsigned long long x2 = 0xfff0000000000000ULL; // -infinity as double
  unsigned long long x3 = 0x7ff0000000000001ULL; // NaN as double
  double y1 =* ((double *)(&amp;x1));
  double y2 =* ((double *)(&amp;x2));
  double y3 = *((double *)(&amp;x3));
  
  unsigned long long x4 = x1 + x2;  // I want to check (+infinity)+(-infinity)
  double y4 = y1 + y2; // I want to check (+infinity)+(-infinity)
  
  printf("\nx1: ");
  show_bytes((byte_pointer)&amp;x1, sizeof(x1));
  printf("\nx2: ");
  show_bytes((byte_pointer)&amp;x2, sizeof(x2));
  printf("\nx3: ");
  show_bytes((byte_pointer)&amp;x3, sizeof(x3));
  printf("\nx4: ");
  show_bytes((byte_pointer)&amp;x4, sizeof(x4));
  
  printf("\ny1: ");
  show_bytes((byte_pointer)&amp;y1, sizeof(y1));
  printf("\ny2: ");
  show_bytes((byte_pointer)&amp;y2, sizeof(y2));
  printf("\ny3: ");
  show_bytes((byte_pointer)&amp;y3, sizeof(y3));
  printf("\ny4: ");
  show_bytes((byte_pointer)&amp;y4, sizeof(y4));
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>x1: 7ff0000000000000
  
  x2: fff0000000000000
  
  x3: 7ff0000000000001
  
  x4: 7fe0000000000000
  
  y1: 7ff0000000000000
  
  y2: fff0000000000000
  
  y3: 7ff8000000000001
  
  y4: fff8000000000000  // &lt;== Different with x4
  </code></pre>
  
  <p>The strange part is, though x1 and x2 have the identical bit pattern as y1 and y2, the sum x4 is different from y4.</p>
  
  <p>And</p>
  
  <pre><code>printf("\ny4=%f", y4);
  </code></pre>
  
  <p>gives this:</p>
  
  <pre><code>y4=-1.#IND00  // What does it mean???
  </code></pre>
  
  <p>Why are they different? And how is y4 obtained?</p>