As I understood from this answer, there is a way to extend the precision using float.h
via the macro LDBL_MANT_DIG
. My goal is to enhance the floating point precision of double
values so that I can store a more accurate number, e.g., 0.000000000566666
instead of 0.000000
. Kindly, can someone give a short example of to use this macro so that I can extend the precision stored in the buffer?
-
3You can't increase the precision of `float`. The header gives you macros to check what kind of precision is offered by your system and its hardware, but that's about it. – Sergey Kalinichenko Apr 22 '17 at 00:39
-
1If you print with `"%f"`, you get just 6 decimal places. Use `%e` or `%g` instead, or add precision control, or both. – Jonathan Leffler Apr 22 '17 at 00:52
3 Answers
Your comment about wanting to store more accurate numbers so you don't get just 0.000000
suggests that the problem is not in the storage but in the way you're printing the numbers. Consider the following code:
#include <stdio.h>
int main(void)
{
float f = 0.000000000566666F;
double d = 0.000000000566666;
long double l = 0.000000000566666L;
printf("%f %16.16f %13.6e\n", f, f, f);
printf("%f %16.16f %13.6e\n", d, d, d);
printf("%lf %16.16lf %13.6le\n", d, d, d);
printf("%Lf %16.16Lf %13.6Le\n", l, l, l);
return 0;
}
When run, it produces:
0.000000 0.0000000005666660 5.666660e-10
0.000000 0.0000000005666660 5.666660e-10
0.000000 0.0000000005666660 5.666660e-10
0.000000 0.0000000005666660 5.666660e-10
As you can see, using the default "%f"
format prints 6 decimal places, which treats the value as 0.0
. However, as the format with more precision shows, the value is stored correctly and can be displayed with more decimal places, or with the %e
format, or indeed with the %g
format though the code doesn't show that in use — the output would be the same as the %e
format in this example.
The %f
conversion specification, as opposed to %lf
or %Lf
, says 'print a double
'. Note that when float
values are passed to printf()
, they are automatically converted to double
(just as numeric types shorter than int
are promoted to int
). Therefore, %f
can be used for both float
and double
types, and indeed the %lf
format (which was defined in C99 — everything else was defined in C90) can be used to format float
or double
values. The %Lf
format expects a long double
.
There isn't a way to store more precision in a float
or double
simply by using any of the macros from <float.h>
. Those are more descriptions of the characteristics of the floating-point types and the way that they behave than anything else.

- 730,956
- 141
- 904
- 1,278
The answer you cited only mentions that the macro is equal to the number of precision digits that you can store. It cannot in any way increase precision. But the macro is for "long doubles", not doubles. You can use the long double type if you need more precision than the double type:
long double x = 3.14L;
Notice the "L" after the number for specifying a long double literal.

- 44
- 3
Floating-point types are implemented in hardware. The precision is standardized across the industry and baked into the circuits of the CPU. There's no way to increase it beyond long double
except an extended-precision software library such as GMP.
The good news is that floating-point numbers don't get bogged down in leading zeroes. 0.000000000566666 won't round to zero. With only six digits, you only even need a single-precision float
to represent it well.
There is an issue with math.h
(not float.h
), where the POSIX standard fails to provide π and e with long double
precision. There are a couple workarounds: GNU defines e.g. M_PIl
and M_El
, or you can also use the preprocessor to paste an l
onto such literal constants in another library (giving the number long double
type) and hope for spare digits.

- 134,909
- 25
- 265
- 421