Skip to main content
1 of 2
chux
  • 33.7k
  • 2
  • 38
  • 92

Side issue

"%f" does not well express the state of a floating point (FP) object

When a FP is large, printing 100s of digits is not informative. More compact options exist.

Worse, when a FP is much smaller than 1, "%f" only retains a few or zero digits - losing perhaps all precession.

As FP are encoded in exponential notation, use an exponential format

printf("%a", fp);  //Hexadecimal significant with binary exponent
// or 
printf("%.*e", DBL_DECIMAL_DIG - 1, fp);  // Decimal exponential
// or 
printf("%.*g", DBL_DECIMAL_DIG, fp);  // Decimal exponential when needed, else fix point.

ref

chux
  • 33.7k
  • 2
  • 38
  • 92