English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C Standard Library 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 the program more portable. Before explaining these constants, it is best to first understand that floating-point numbers are composed of the following four elements:
component | component description |
---|---|
S | symbol ( +/- ) |
b | the base represented by the exponent,2 representing binary,10 representing decimal,16 representing hexadecimal, etc... |
e | exponent, a value between the minimum value emin and the maximum value emax between integers. |
p | precision, the number of significant digits of the base b |
Based on the above 4 An individual component, the value of a floating-point number is as follows:
floating-point = ( S ) p x be
or}}
floating-point = (+/-) precision x baseexponent
The following values are specific to the implementation and are defined by the #define directive. These values must not be lower than the values given below. Please note that all examples FLT refer to the type float, DBL refers to the type double, and LDBL refers to the type long double.
Macro | Description |
---|---|
FLT_ROUNDS | Define the rounding mode for floating-point addition, which can be any of the following values:
|
FLT_RADIX 2 | This macro defines the base for exponent representation. Base 2 Represent in binary, base 10 Represent in decimal, base 16 Represent in hexadecimal. |
FLT_MANT_DIG DBL_MANT_DIG LDBL_MANT_DIG | These macros define the number of digits in the base FLT_RADIX. |
FLT_DIG 6 DBL_DIG 10 LDBL_DIG 10 | These macros define the maximum decimal digit value that will not change after rounding (base 10) |
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 base for 10 The minimum negative integer value of the exponent when it is %. |
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 base for 10 The maximum integer value of the exponent when it is %. |
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 positive representable number. |
FLT_MIN 1E-37 DBL_MIN 1E-37 LDBL_MIN 1E-37 | These macros define the smallest floating-point value. |
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 = %.10e\n", FLT_MANT_DIG); }
Let's compile and run the above program, which will produce the following results:
The maximum value of float = 3.4028234664e+38 The minimum value of float = 1.1754943508e-38 The number of digits of number = 7.2996655210e-312