title: How %a conversion work in printf statement?
body: <p>Maybe this doesn't belong on SO but I don't know where else.</p>
  
  <p>I have to reimplement <code>printf(3)</code> with <code>C</code> without using any function that would do the conversion for me, I'm nearly done, but I'm stuck on <code>%a</code>, I really don't understand what is happening here for example:</p>
  
  <pre><code>printf("%a\n", 3.0); //#=&gt; 0x1.8p+1
  printf("%a\n", 3.1); //#=&gt; 0x1.8cccccccccccdp+1
  printf("%a\n", 3.2); //#=&gt; 0x1.999999999999ap+1
  printf("%a\n", 3.3); //#=&gt; 0x1.a666666666666p+1
  printf("%a\n", 3.4); //#=&gt; 0x1.b333333333333p+1
  printf("%a\n", 3.5); //#=&gt; 0x1.cp+1
  printf("%a\n", 3.6); //#=&gt; 0x1.ccccccccccccdp+1
  </code></pre>
  
  <p>Of course I read the man which says:</p>
  
  <blockquote>
    <p>The double argument is rounded and converted to hexadecimal notation 
    in the style[-]0xh.hhhp[+-]d, where the number of digits after 
    the hexadecimal-point character is equal to the precision specification.</p>
  </blockquote>
  
  <p>But this doesn't really help I don't understand <em>the process</em> that transforms <code>3.2</code> to <code>1.999999999999ap+1</code></p>
  
  <p>I don't need any code but really more an explanation.</p>
  
  <p>PS: If this isn't the place for this question could you point me to the right place?</p>
  
  <p>EDIT: While <code>@juhist</code> answer works for numbers >= <code>1.0</code> it doesn't explain how to get the result for numbers between <code>0.0</code> et <code>1.0</code>:</p>
  
  <pre><code>printf("%a\n", 0.01); //#=&gt; 0x1.47ae147ae147bp-7
  printf("%a\n", 0.1);  //#=&gt; 0x1.999999999999ap-4
  printf("%a\n", 0.2);  //#=&gt; 0x1.999999999999ap-3
  printf("%a\n", 0.3);  //#=&gt; 0x1.3333333333333p-2
  printf("%a\n", 0.4);  //#=&gt; 0x1.999999999999ap-2
  printf("%a\n", 0.5);  //#=&gt; 0x1p-1
  printf("%a\n", 0.6);  //#=&gt; 0x1.3333333333333p-1
  </code></pre>
  
  <p>Also I would really like a precision on this <code>The "a" near the end occurs due to limited floating point calculation precision</code> concerning the conversion: <code>printf("%a\n", 3.2); //#=&gt; 0x1.999999999999ap+1</code></p>
  
  <p>EDIT2: Now the last mystery is to explain why in this case:</p>
  
  <pre><code>printf("%a\n", 0.1); //#=&gt; 0x1.999999999999ap-4
  </code></pre>
  
  <p>The last <code>9</code> becomes and <code>a</code> and in this case:</p>
  
  <pre><code>printf("%a\n", 0.3); //#=&gt; 0x1.3333333333333p-2
  </code></pre>
  
  <p>The last <code>3</code> stays a <code>3</code>?</p>