C Standard Library -
Introduction
is a header file in the C standard library that defines macros related to floating-point types (float, double and long double). These macros provide the characteristics and limits of floating-point numbers, such as maximum value, minimum value, precision, etc.
The C standard library's float.h The header file contains a set of platform-dependent constants related to floating-point values. These constants were proposed by ANSI C, making programs more portable. Before explaining these constants, it is best to understand that a floating-point number is composed of the following four elements:
| Component | Component description |
|---|---|
| S | Sign ( +/- ) |
| b | The base of the exponent representation: 2 for binary, 10 for decimal, 16 for hexadecimal, and so on... |
| e | The 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 the above four components, the value of a floating-point number is as follows:
floating-point = ( S ) p x be
or
floating-point = (+/-) precision x baseexponent
Library macros
The following values are implementation-specific and are defined via #define directives; none of these values may be lower than the values given below. Note that in all instances, FLT refers to the type float, DBL refers to the type double, and LDBL refers to the type long double.
Single-precision floating-point number (float)
FLT_RADIX: the base of the floating-point number (usually 2).FLT_MANT_DIG:float: The number of significant digits of the type.FLT_DIG:floatThe decimal precision of the type, i.e., the maximum number of decimal digits that can be guaranteed without distortion.FLT_MIN_EXP:floatThe minimum negative exponent of the type (in the given base).FLT_MIN_10_EXP:floatThe minimum negative decimal exponent of the type.FLT_MAX_EXP:floatThe maximum exponent of the type (in the given base).FLT_MAX_10_EXP:floatThe maximum decimal exponent of the type.FLT_MAX:floatThe maximum positive value of the type.FLT_MIN:floatThe minimum positive value of the type.FLT_EPSILON:floatThe relative error of the type, i.e., the difference between 1.0 and the smallest floating-point number greater than 1.0.
Double-precision floating-point number (double)
DBL_MANT_DIG:double: The number of significant digits of the type.DBL_DIG:doubleThe decimal precision of the type.DBL_MIN_EXP:doubleThe minimum negative exponent of the type (in the given base).DBL_MIN_10_EXP:doubleThe minimum negative decimal exponent of the type.DBL_MAX_EXP:doubleThe maximum exponent of the type (in the given base).DBL_MAX_10_EXP:doubleThe maximum decimal exponent of the type.DBL_MAX:doubleThe maximum positive value of the type.DBL_MIN:doubleThe minimum positive value of the type.DBL_EPSILON:doubleThe relative error of the type.
Extended-precision floating-point number (long double)
LDBL_MANT_DIG:long double: The number of significant digits of the type.LDBL_DIG:long doubleThe decimal precision of the type.LDBL_MIN_EXP:long doubleThe minimum negative exponent of the type (in the given base).LDBL_MIN_10_EXP:long doubleThe minimum negative decimal exponent of the type.LDBL_MAX_EXP:long doubleThe maximum exponent of the type (in the given base).LDBL_MAX_10_EXP:long doubleThe maximum decimal exponent of the type.LDBL_MAX:long doubleThe maximum positive value of the type.LDBL_MIN:long doubleThe minimum positive value of the type.LDBL_EPSILON:long doubleThe relative error of the type.
Example
The following example demonstrates the use of some constants defined in the float.h file.
Example
#include
#include
int
main
(
)
{
printf
(
"Single precision (float):\n"
)
;
printf
(
"FLT_RADIX: %d\n"
,
FLT_RADIX
)
;
printf
(
"FLT_MANT_DIG: %d\n"
,
FLT_MANT_DIG
)
;
printf
(
"FLT_DIG: %d\n"
,
FLT_DIG
)
;
printf
(
"FLT_MIN_EXP: %d\n"
,
FLT_MIN_EXP
)
;
printf
(
"FLT_MIN_10_EXP: %d\n"
,
FLT_MIN_10_EXP
)
;
printf
(
"FLT_MAX_EXP: %d\n"
,
FLT_MAX_EXP
)
;
printf
(
"FLT_MAX_10_EXP: %d\n"
,
FLT_MAX_10_EXP
)
;
printf
(
"FLT_MAX: %e\n"
,
FLT_MAX
)
;
printf
(
"FLT_MIN: %e\n"
,
FLT_MIN
)
;
printf
(
"FLT_EPSILON: %e\n"
,
FLT_EPSILON
)
;
printf
(
"\nDouble precision (double):\n"
)
;
printf
(
"DBL_MANT_DIG: %d\n"
,
DBL_MANT_DIG
)
;
printf
(
"DBL_DIG: %d\n"
,
DBL_DIG
)
;
printf
(
"DBL_MIN_EXP: %d\n"
,
DBL_MIN_EXP
)
;
printf
(
"DBL_MIN_10_EXP: %d\n"
,
DBL_MIN_10_EXP
)
;
printf
(
"DBL_MAX_EXP: %d\n"
,
DBL_MAX_EXP
)
;
printf
(
"DBL_MAX_10_EXP: %d\n"
,
DBL_MAX_10_EXP
)
;
printf
(
"DBL_MAX: %e\n"
,
DBL_MAX
)
;
printf
(
"DBL_MIN: %e\n"
,
DBL_MIN
)
;
printf
(
"DBL_EPSILON: %e\n"
,
DBL_EPSILON
)
;
printf
(
"\nExtended precision (long double):\n"
)
;
printf
(
"LDBL_MANT_DIG: %d\n"
,
LDBL_MANT_DIG
)
;
printf
(
"LDBL_DIG: %d\n"
,
LDBL_DIG
)
;
printf
(
"LDBL_MIN_EXP: %d\n"
,
LDBL_MIN_EXP
)
;
printf
(
"LDBL_MIN_10_EXP: %d\n"
,
LDBL_MIN_10_EXP
)
;
printf
(
"LDBL_MAX_EXP: %d\n"
,
LDBL_MAX_EXP
)
;
printf
(
"LDBL_MAX_10_EXP: %d\n"
,
LDBL_MAX_10_EXP
)
;
printf
(
"LDBL_MAX: %Le\n"
,
LDBL_MAX
)
;
printf
(
"LDBL_MIN: %Le\n"
,
LDBL_MIN
)
;
printf
(
"LDBL_EPSILON: %Le\n"
,
LDBL_EPSILON
)
;
return
0
;
}
Let's compile and run the above program, which will produce the following result:
Single precision (float):
FLT_RADIX: 2
FLT_MANT_DIG: 24
FLT_DIG: 6
FLT_MIN_EXP: -125
FLT_MIN_10_EXP: -37
FLT_MAX_EXP: 128
FLT_MAX_10_EXP: 38
FLT_MAX: 3.402823e+38
FLT_MIN: 1.175494e-38
FLT_EPSILON: 1.192093e-07
Double precision (double):
DBL_MANT_DIG: 53
DBL_DIG: 15
DBL_MIN_EXP: -1021
DBL_MIN_10_EXP: -307
DBL_MAX_EXP: 1024
DBL_MAX_10_EXP: 308
DBL_MAX: 1.797693e+308
DBL_MIN: 2.225074e-308
DBL_EPSILON: 2.220446e-16
Extended precision (long double):
LDBL_MANT_DIG: 64
LDBL_DIG: 18
LDBL_MIN_EXP: -16381
LDBL_MIN_10_EXP: -4931
LDBL_MAX_EXP: 16384
LDBL_MAX_10_EXP: 4932
LDBL_MAX: 1.189731e+4932
LDBL_MIN: 3.362103e-4932
LDBL_EPSILON: 1.084202e-19
provides a set of macros for describing the characteristics and limits of floating-point types. By using these macros, we can write more robust code that handles boundary cases and special situations related to floating-point numbers.