I recommend using the @Jens Gustedt hex solution: use% a.
OP wants to "print with maximum precision (or at least to the most significant decimal place)."
A simple example would be the seventh seal, as in:
#include <float.h> int Digs = DECIMAL_DIG; double OneSeventh = 1.0/7.0; printf("%.*e\n", Digs, OneSeventh); // 1.428571428571428492127e-01
But let me dig deeper ...
Mathematically, the answer is "0.142857 142857 142857 ...", but we use floating point numbers with finite precision. Suppose IEEE 754 double precision binary code . Thus, the value of OneSeventh = 1.0/7.0 leads to the value below. The previous and next representable double floating point numbers are also shown.
OneSeventh before = 0.1428571428571428 214571170656199683435261249542236328125 OneSeventh = 0.1428571428571428 49212692681248881854116916656494140625 OneSeventh after = 0.1428571428571428 769682682968777953647077083587646484375
Printing the exact decimal representation of double has limited use.
C has 2 macro families in <float.h> to help us.
The first set is the number of significant digits to print per line in decimal value, so when we scan the line back, we get the original floating point. The minimum C spec and sample C11 compiler are shown.
FLT_DECIMAL_DIG 6, 9 (float) (C11) DBL_DECIMAL_DIG 10, 17 (double) (C11) LDBL_DECIMAL_DIG 10, 21 (long double) (C11) DECIMAL_DIG 10, 21 (widest supported floating type) (C99)
The second set is the number of significant digits that a string can be scanned at a floating point, and then printed by FP, while maintaining the same line presentation. The minimum C spec and sample C11 compiler are shown. I find the pre-C99 affordable.
FLT_DIG 6, 6 (float) DBL_DIG 10, 15 (double) LDBL_DIG 10, 18 (long double)
The first set of macros seems to match the OP goal for significant digits. But this macro is not always available.
#ifdef DBL_DECIMAL_DIG #define OP_DBL_Digs (DBL_DECIMAL_DIG) #else #ifdef DECIMAL_DIG #define OP_DBL_Digs (DECIMAL_DIG) #else #define OP_DBL_Digs (DBL_DIG + 3) #endif #endif
"+ 3" was the highlight of my previous answer. It focuses on knowing the conversion string in the opposite direction - FP string (macro set # 2 for C89), how to define numbers for FP-string-FP (set macro # 1 after C89)? In general, the addition of 3 was the result.
Now, how many significant digits to print are known and managed through <float.h> .
To print N significant decimal digits, you can use various formats.
With "%e" the precision field is the number of digits after the digit and decimal point. So, - 1 is fine. Note: This -1 is not in the initial int Digs = DECIMAL_DIG; `
printf("%.*e\n", OP_DBL_Digs - 1, OneSeventh);
With "%f" the precision field is the number of digits after the decimal point. For a number like OneSeventh/1000000.0 , you would OP_DBL_Digs + 6 to see all the significant numbers.
printf("%.*f\n", OP_DBL_Digs , OneSeventh); // 0.14285714285714285 printf("%.*f\n", OP_DBL_Digs + 6, OneSeventh/1000000.0); // 0.00000014285714285714285
Note. Many of them are used for "%f" . It displays 6 digits after the decimal point; 6 is the default value, not the accuracy of the number.