The best C standard library - <float.h> Tutorial In 2024, In this tutorial you can learn Brief introduction,Macro library,Examples,
C standard libraryfloat.h header file contains a set of constants associated with floating-point value depends on the platform.These constants are proposed by the ANSI C, which makes programs more portable. Before explaining these constants, it is best to clarify float is composed of four elements in the following composition:
组件 | 组件描述 |
---|---|
S | 符号 ( +/- ) |
b | 指数表示的基数,2 表示二进制,10 表示十进制,16 表示十六进制,等等/en. |
e | 指数,一个介于最小值e min和最大值e max之间的整数。 |
p | 精度,基数 b 的有效位数 |
Based on the above four components, the value of a floating-point number as follows:
floating-point = (S) pxb e
or
floating-point = (+/-) precision x base exponent
The following values are implementation-specific, and is defined by #define directive, these values are not lower than the values given below. Please note that all instances of FLT refers to the type float, DBL refers to the type double, LDBL refers to type long double.
宏 | 描述 |
---|---|
FLT_ROUNDS | 定义浮点加法的舍入模式,它可以是下列任何一个值:
|
FLT_RADIX 2 | 这个宏定义了指数表示的基数。基数 2 表示二进制,基数 10 表示十进制,基数 16 表示十六进制。 |
FLT_MANT_DIG DBL_MANT_DIG LDBL_MANT_DIG | 这些宏定义了 FLT_RADIX 基数中的位数。 |
FLT_DIG 6 DBL_DIG 10 LDBL_DIG 10 | 这些宏定义了舍入后不会改变表示的十进制数字的最大值(基数 10)。 |
FLT_MIN_EXP DBL_MIN_EXP LDBL_MIN_EXP | 这些宏定义了基数为 FLT_RADIX 时的指数的最小负整数值。 |
FLT_MIN_10_EXP -37 DBL_MIN_10_EXP -37 LDBL_MIN_10_EXP -37 | 这些宏定义了基数为 10 时的指数的最小负整数值。 |
FLT_MAX_EXP DBL_MAX_EXP LDBL_MAX_EXP | 这些宏定义了基数为 FLT_RADIX 时的指数的最大整数值。 |
FLT_MAX_10_EXP +37 DBL_MAX_10_EXP +37 LDBL_MAX_10_EXP +37 | 这些宏定义了基数为 10 时的指数的最大整数值。 |
FLT_MAX 1E+37 DBL_MAX 1E+37 LDBL_MAX 1E+37 | 这些宏定义最大的有限浮点值。 |
FLT_EPSILON 1E-5 DBL_EPSILON 1E-9 LDBL_EPSILON 1E-9 | 这些宏定义了可表示的最小有效数字。 |
FLT_MIN 1E-37 DBL_MIN 1E-37 LDBL_MIN 1E-37 | 这些宏定义了最小的浮点值。 |
The following example demonstrates the use of float.h some constants defined in the 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 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