May 11, 2021 C
The float.h header file for the C standard library contains a set of platform-dependent constants related to floating-point values. T hese constants are proposed by ANSI C, which makes the program more portable. Before explaining these constants, it's a good idea to figure out that floats are made up of four elements:
Assembly | Component description |
---|---|
S | Symbol (+/-) |
b | The base representation of the index, 2 indicates binary, 10 means decimal, 16 means hexadecimal, etc. ... |
e | Index, one is smaller e min And maximum e max The integer is integrated. |
p | Accuracy, base b |
Based on the above four components, the value of a floating point is as follows:
floating-point = ( S ) p x b<sup>e</sup> 或 floating-point = (+/-) precision x base<sup>exponent</sup>
The following values are specific and are defined by the #define directive, and they must not be lower than the values given below. Note that all instance FLT refers to type float, DBL refers to type double, and LDBL refers to type long double.
Macro | describe |
---|---|
FLT_ROUNDS |
Define the round-up plus rounding mode, which can be any of the following values:
|
FLT_RADIX 2 | This macro defines the base of the exponity representation.The base 2 represents binary, the base 10 represents decimal, and the base 16 represents the hexadecimal. |
FLT_MANT_DIG DBL_MANT_DIG LDBL_MANT_DIG |
These macros define the number of bits in the FLT_RADIX base. |
FLT_DIG 6 DBL_DIG 10 LDBL_DIG 10 |
These macros define the maximum value of the decimal number (the base 10) that does not change the decimal number of the representation. |
FLT_MIN_EXP DBL_MIN_EXP LDBL_MIN_EXP |
These macros define the minimum negative value of the index 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 index of 10 when the base is 10. |
FLT_MAX_EXP DBL_MAX_EXP LDBL_MAX_EXP |
These macros define the maximum integer value of the index 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 index of 10 when the base is 10. |
FLT_MAX 1E+37 DBL_MAX 1E+37 LDBL_MAX 1E+37 |
These macros define the maximum finite floating point value. |
FLT_EPSILON 1E-5 DBL_EPSILON 1E-9 LDBL_EPSILON 1E-9 |
These macros define the smallest valid numbers that can be represented. |
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 of the 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 program above, 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 in the number = 7.2996655210e-312