title: representation of numbers in bash and printf of hexadecimal numbers in bash
body: <p>I want to understand how numbers (doubles) are represented in bash and what happens when I printf numbers in hexadecimal format in bash.</p>
  
  <p>According to the IEEE 754 standard double should be represented by 64 bits: 52 bits (13 hex numbers) for a significand, 11 bits for the exponent and 1 bit for sign.</p>
  
  <p>To check it, I wrote a simple C program that transforms hex into dec (using printf).</p>
  
  <pre><code>include &lt;stdio.h&gt;
  int main(int argc, char **argv)
  {
  printf("hex read = %40.24a\n", 0x1.000010C6F7A0B5E1Fp+0);
  }
  </code></pre>
  
  <p>compiling with gcc 4.2.1, I get</p>
  
  <pre><code>hex read =          0x1.000010c6f7a0b00000000000p+0
  </code></pre>
  
  <p>From this result I conclude, that as I expect significand is defined by 13 hex digits 000010c6f7a0b.</p>
  
  <p>Now I turn to bash and use the following script:</p>
  
  <pre><code>#!/bin/bash
  echo "hex read = 0x"$1
  printf "hex       =%80.70a\n" "0x"$1
  printf "hex -&gt; dec=%80.70f\n" `echo "0x"$1`
  </code></pre>
  
  <p><strong>GNU bash 3.2.48</strong></p>
  
  <pre><code>$ bash hex2dec 1.000010C6F7A0B5E1F
  hex read = 0x1.000010C6F7A0B5E1F
  hex       =   0x1.000010c6f7a0b000000000000000000000000000000000000000000000000000000000p+0
  hex -&gt; dec=        1.0000009999999999177333620536956004798412322998046875000000000000000000
  </code></pre>
  
  <p>So everything worked as I expected: 13 hex digits define the significand of the number.</p>
  
  <p><strong>GNU bash 4.1.5</strong></p>
  
  <pre><code>$ bash hex2dec 1.000010C6F7A0B5E1F
  hex read = 0x1.000010C6F7A0B5E1F
  hex       =   0x8.00008637bd05af10000000000000000000000000000000000000000000000000000000p-3
  hex -&gt; dec=        1.0000009999999999993737856418540843606024282053112983703613281250000000
  </code></pre>
  
  <p>This is not what I expected!</p>
  
  <p><strong>Question 1</strong> Why in GNU bash 4.1.5 double's significand is represented by 16 hex digits (instead of 13 according to IEEE 754)?</p>
  
  <p><strong>Question 2</strong> Why printf "%a" represents the hex number in a different format in different bash versions (<strong>bash 3.2.48</strong> 0x1.hh...hp+d and <strong>bash 4.1.5</strong> 0xh.hh...hp+d?). Shouldn't printf follow the same standard in both bash versions and be reglamented by <a href="http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html" rel="nofollow">http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html</a>?</p>