C Reference ยท ctype.h, math.h and others
C float.h: FLT_EPSILON, DBL_MAX, DBL_DIG
Reference for float.h: the range, precision and epsilon of float, double and long double, with FLT_EPSILON, DBL_DIG, DBL_MAX, DBL_TRUE_MIN and safe comparison tips.
<float.h> describes the floating-point types of the platform: the largest and smallest values, how many decimal digits survive a round trip, and the gap between 1.0 and the next representable number (the machine epsilon). Each type has its own set of macros with a prefix: FLT_ for float, DBL_ for double and LDBL_ for long double.
Almost all current systems use IEEE 754 binary formats, so a double has 53 significant bits and a float has 24. The macros let portable code adapt anyway, and they are the right way to write tolerances for comparing floating-point numbers.
#include <float.h> double largest = DBL_MAX; double tolerance = 4 * DBL_EPSILON;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
#include <float.h> FLT_RADIX FLT_MANT_DIG DBL_MANT_DIG LDBL_MANT_DIG /* digits in the significand (base FLT_RADIX) */ FLT_DIG DBL_DIG LDBL_DIG /* decimal digits that survive text -> float -> text */ FLT_DECIMAL_DIG DBL_DECIMAL_DIG LDBL_DECIMAL_DIG /* digits that survive float -> text -> float */ FLT_MIN_EXP FLT_MAX_EXP (and DBL_, LDBL_) /* exponent ranges in base FLT_RADIX */ FLT_MIN_10_EXP FLT_MAX_10_EXP (and DBL_, LDBL_) /* exponent ranges in base 10 */ FLT_MAX DBL_MAX LDBL_MAX /* largest finite value */ FLT_MIN DBL_MIN LDBL_MIN /* smallest positive normal value */ FLT_TRUE_MIN DBL_TRUE_MIN LDBL_TRUE_MIN /* smallest positive value, subnormals included (C11) */ FLT_EPSILON DBL_EPSILON LDBL_EPSILON /* difference between 1 and the next larger value */ FLT_EVAL_METHOD FLT_ROUNDS DECIMAL_DIG
| Name | Description |
|---|---|
*_DIG |
Number of decimal digits a number can have and still be converted to the type and back without change: 6 for float, 15 for double. |
*_DECIMAL_DIG |
Digits needed to print a value so reading it back gives the identical value: 9 for float, 17 for double. Use as %.17g. |
*_EPSILON |
Spacing of numbers around 1.0. The gap grows with the magnitude of the numbers and shrinks toward zero. |
*_MIN |
Smallest normal positive number. Subnormal (denormal) numbers go below, down to *_TRUE_MIN, with less precision. |
FLT_EVAL_METHOD |
How intermediate results are evaluated: 0 means each type is used as written, 2 means everything is evaluated as long double (32-bit x87). |
FLT_ROUNDS |
Current rounding mode (1 is "to nearest"). To read or change the mode, use fegetround() and fesetround() from fenv.h. |
Returns: all macros are constants usable in constant expressions (except FLT_ROUNDS, which can change at run time).
Errors / undefined behavior: none for the macros. Operations that exceed the range produce infinity or zero as IEEE 754 defines, and sometimes raise floating-point exceptions that you can test with fetestexcept.
Usage notes
- Never compare with
==after arithmetic.0.1 + 0.2 == 0.3is false in binary floating point. Compare with a tolerance, and make it relative to the size of the numbers. An absoluteDBL_EPSILONtolerance only works near 1.0. DBL_EPSILONis relative to 1.0. For a number near 1000 the spacing is about 1000 times larger. For comparisons, multiply the epsilon by the magnitude, as in the second example.DBL_DIGversusDBL_DECIMAL_DIG. 15 digits is how much text precision a double keeps. 17 is how many are needed to print one exactly. Use%.17gto serialize doubles and%.15gfor display without noise.DBL_MINis not the most negative number. The most negative finite value is-DBL_MAX.DBL_MINis tiny and positive, the smallest normal value.- Subnormals fill the gap between 0 and
DBL_MIN. They keep gradual underflow, but arithmetic on them can be slow on some CPUs. - Overflow and underflow.
DBL_MAX * 2becomesinf;DBL_TRUE_MIN / 2becomes0. Check withisinfand friends in isnan, isinf, isfinite. - Printing.
floatarguments are promoted todoubleinprintf, so the cast in the first example is only for clarity; use%Leor%Lgforlong doublevalues. - Standard version: C89;
DECIMAL_DIGin C99;*_TRUE_MINand*_DECIMAL_DIGin C11. C23 adds macros for the optional decimal floating-point types (DEC32_*,DEC64_*,DEC128_*).