title: the precision of printf with specifier "%g"
body: <p>Can anybody explain me how the <code>[.precision]</code> in printf works with specifier "%g"? I'm quite confused by the following output:</p>
  
  <pre><code>double value = 3122.55;
  printf("%.16g\n", value); //output: 3122.55
  printf("%.17g\n", value); //output: 3122.5500000000002
  </code></pre>
  
  <p>I've learned  that <code>%g</code> uses the shortest representation.</p>
  
  <p>But the following outputs still confuse me</p>
  
  <pre><code>printf("%.16e\n", value); //output: 3.1225500000000002e+03
  printf("%.16f\n", value); //output: 3122.5500000000001819
  printf("%.17e\n", value); //output: 3.12255000000000018e+03
  printf("%.17f\n", value); //output: 3122.55000000000018190
  </code></pre>
  
  <p>My question is: why <code>%.16g</code> gives the exact number while <code>%.17g</code> can't?</p>
  
  <p>It seems 16 significant digits can be accurate. Could anyone tell me the reason?</p>