0

I'm using Visual Studio Code and I have the following code:

double a = 0.10;
printf("%.20lf", a);

I got the result

0.10000000000000001000

Why does the value have '1000' at the end there? Shouldn't it be all zeros after '0.1'?

  • Your computer uses a binary floating-point representation internally, not decimal. The number 1/10 cannot be represented exactly in binary -- it's an infinitely-repeating fraction, just like 1/3 is in decimal. The closest you can get, as a `double`, is a binary number that's equivalent to 0.1000000000000000055511151231257827021181583404541015625 in decimal. Printed as `%.20f`, it should have been 0.10000000000000000555. So it looks like Visual Studio Code's library didn't round it quite right. – Steve Summit Nov 21 '21 at 18:52
  • @SteveSummit: Various Microsoft software converts `double` to at most 17 digits and then adds if the format specification calls for more digits. I presume they used 17 because that is what they thought the “limit” of the `double` format was. – Eric Postpischil Nov 21 '21 at 18:57
  • @EricPostpischil Sigh. That would just about do it. ("Adds zeros", I assume you mean.) And on the one hand, [Dragon](https://dl.acm.org/doi/10.1145/93548.93559) is hard, so I might have a tiny bit of sympathy for Microsoft not managing to get it quite right. But on the other hand, it's only been published for, what, 30 years now? – Steve Summit Nov 21 '21 at 19:02
  • @Unstuck To say a little more: 1/10 in binary is `0b0.0001100110011…`, where the `0011` bits repeat forever. Type `double` has 53 bits of precision, so what you get, rounded to 53 significant bits, is `0b0.00011001100110011001100110011001100110011001100110011010`, which if you do the math converts back to the number 0.1000…5625 I mentioned. – Steve Summit Nov 21 '21 at 19:13

0 Answers0