0

It's a very simple code. I compiled and run this code in code blocks and the output was 11111111111111112.000000. I run this code also in https://www.codechef.com/ide. This time the output was same. But why it doesn't work properly?

How can I solve this problem?

Here's my code.

#include <stdio.h>
int main()
{
 double a = 11111111111111111;
 printf("%lf\n", a);
 return 0;
}
Nafis
  • 19
  • 3
  • `double` in your C implementation and common implementations does not record 17 decimal digits. It records 53 binary digits (bits). This is sufficient to distinguish any 15 decimal digits but not any 17. – Eric Postpischil Aug 20 '20 at 12:04
  • @Nirob: Have a play with some numbers on this site: https://www.exploringbinary.com/floating-point-converter/. Notice that in all cases when a whole number is changed, it is always divisible by 2 at least once. – Bathsheba Aug 20 '20 at 12:12

0 Answers0