C Standard Library - <float.h>
Introduction
The float.h header file of the C Standard Library contains a set of platform-dependent constants related to floating-point values. These constants are proposed by ANSI C, making programs more portable. Before explaining these constants, it's best to understand that a floating-point number consists of the following four elements:
Component | Description |
---|---|
S | Sign ( +/- ) |
b | Base of the exponent representation, 2 for binary, 10 for decimal, 16 for hexadecimal, etc... |
e | Exponent, an integer between the minimum value emin and the maximum value emax. |
p | Precision, the number of significant digits in base b |
Based on these four components, a floating-point value is as follows:
floating-point = ( S ) p x b
or
floating-point = (+/-) precision x base
Library Macros
The following values are implementation-specific and are defined using the #define directive. These values must not be lower than the values provided below. Note that all instances of FLT refer to the float type, DBL refers to the double type, and LDBL refers to the long double type.
Macro | Description |
---|---|
FLT_ROUNDS | Defines the rounding mode for floating-point addition. It can be one of the following values: -1 - indeterminable<br>0 - toward zero<br>1 - to the nearest value<br>2 - toward positive infinity<br>3 - toward negative infinity |
FLT_RADIX 2 | This macro defines the base of the exponent representation. Base 2 represents binary, base 10 represents decimal, base 16 represents hexadecimal. |
FLT_MANT_DIG DBL_MANT_DIG LDBL_MANT_DIG | These macros define the number of digits in the FLT_RADIX base. |
FLT_DIG 6 DBL_DIG 10 LDBL_DIG 10 | These macros define the maximum number of decimal digits (base 10) that can be represented without change after rounding. |
FLT_MIN_EXP DBL_MIN_EXP LDBL_MIN_EXP | These macros define the minimum negative integer value of the exponent when the base is FLT_RADIX. |
FLT_MIN_10_EXP -37 DBL_MIN_10_EXP -37 LDBL_MIN_10_EXP -37 | These macros define the minimum negative integer value of the exponent when the base is 10. |
FLT_MAX_EXP DBL_MAX_EXP LDBL_MAX_EXP | These macros define the maximum integer value of the exponent when the base is FLT_RADIX. |
FLT_MAX_10_EXP +37 DBL_MAX_10_EXP +37 LDBL_MAX_10_EXP +37 | These macros define the maximum integer value of the exponent when the base is 10. |
FLT_MAX 1E+37 DBL_MAX 1E+37 LDBL_MAX 1E+37 | These macros define the largest finite floating-point value. |
FLT_EPSILON 1E-5 DBL_EPSILON 1E-9 LDBL_EPSILON 1E-9 | These macros define the smallest representable significant digit. |
FLT_MIN 1E-37 DBL_MIN 1E-37 LDBL_MIN 1E-37 | These macros define the smallest floating-point value. |
Example
The following example demonstrates the use of some constants defined in the float.h file.
#include <stdio.h>
#include <float.h>
int main()
{
printf("The maximum value of float = %.10e\n", FLT_MAX);
printf("The minimum value of float = %.10e\n", FLT_MIN);
printf("The number of digits in the number = %.10e\n", FLT_MANT_DIG);
}
Let's compile and run the above program, which will produce the following result:
The maximum value of float = 3.4028234664e+38
The minimum value of float = 1.1754943508e-38
The number of digits in the number = 7.2996655210e-312