-1

I've got a problem with diplaying big doubles.

Here's a code I've prepared:

#include <stdio.h>
#include <float.h>
#define M_PI 3.141592653589793238462643383279502884197169399375105820974944
int main()
{
    printf("%.70f\n",M_PI);
    return 0;
}

It displays:

3.1415926535897931159979634685441851615905761718750000000000000000000000

But once I change M_PI for DBL_MAX everything is shown correctly. What's worse is that when dot in M_PI definition is removed (leaving a really big number) the result is 0.

What do I do wrong? How can I display some huge numbers?

Igor
  • 163
  • 6
  • Removing the dot changes it to an integer. Try moving the dot to the end instead. – Patricia Shanahan Nov 24 '18 at 17:44
  • After moving it, 0 is no longer displayed but the number has chagned value after 16 digit, just like M_PI in my question. – Igor Nov 24 '18 at 17:49
  • `double` has effectively 53 bits of significand, equivalent to about 15.9 decimal digits, so rounding error after the 16th significant digit should be expected, and is nothing to do with printing. – Patricia Shanahan Nov 24 '18 at 17:54

1 Answers1

0

In IEEE 754 64-bit binary floating point, the most common implementation of double, 3.141592653589793115997963468544185161590576171875 is the closest representable number to 3.141592653589793238462643383279502884197169399375105820974944, and so is what should be printed. In most cases, rounding error on conversion to double will appear after about 16 decimal digits.

DBL_MAX, by its definition, has to be exactly representable as a double, so there is no rounding error on its conversion from decimal string to double.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75