
- C标准库
- C库 - 首页
- C库 - <assert.h>
- C库 - <complex.h>
- C库 - <ctype.h>
- C库 - <errno.h>
- C库 - <fenv.h>
- C库 - <float.h>
- C库 - <inttypes.h>
- C库 - <iso646.h>
- C库 - <limits.h>
- C库 - <locale.h>
- C库 - <math.h>
- C库 - <setjmp.h>
- C库 - <signal.h>
- C库 - <stdalign.h>
- C库 - <stdarg.h>
- C库 - <stdbool.h>
- C库 - <stddef.h>
- C库 - <stdio.h>
- C库 - <stdlib.h>
- C库 - <string.h>
- C库 - <tgmath.h>
- C库 - <time.h>
- C库 - <wctype.h>
C库 - <float.h>
C库float.h头文件包含一组与浮点值相关的各种平台相关的常量。这些常量由ANSI C提出。浮点宏允许开发人员创建更可移植的程序。在继续介绍所有常量之前,最好先了解浮点数,它包含以下四个元素:
序号 | 组件及组件描述 |
---|---|
1 |
S 符号 (+/-) |
2 |
b 指数表示的基数或基,二进制为2,十进制为10,十六进制为16,等等... |
3 |
e 指数,一个介于最小值emin和最大值emax之间的整数。 |
4 |
p 精度是有效数字中基数b的位数。 |
基于以上4个组件,浮点数的值如下:
floating-point = ( S ) p x be or floating-point = (+/-) precision x baseexponent
库宏
以下值是实现特定的,并用#define指令定义,但这些值可能不会低于此处给出的值。请注意,在所有情况下,FLT 指的是类型float,DBL 指的是double,LDBL 指的是long double。
序号 | 宏及描述 |
---|---|
1 | FLT_ROUNDS 定义浮点加法的舍入模式,它可以具有以下任何值:
|
2 | FLT_RADIX 2 这定义了指数的基数表示法。2为二进制,10为正常的十进制表示,16为十六进制。 |
3 | FLT_MANT_DIG DBL_MANT_DIG LDBL_MANT_DIG 这些宏定义数字中的位数(以FLT_RADIX为基数)。 |
4 | FLT_DIG 6 DBL_DIG 10 LDBL_DIG 10 这些宏定义舍入后可以不变地表示的最大十进制位数(基数-10)。 |
5 | FLT_MIN_EXP DBL_MIN_EXP LDBL_MIN_EXP 这些宏定义以FLT_RADIX为基数的指数的最小负整数值。 |
6 | FLT_MIN_10_EXP -37 DBL_MIN_10_EXP -37 LDBL_MIN_10_EXP -37 这些宏定义以10为基数的指数的最小负整数值。 |
7 | FLT_MAX_EXP DBL_MAX_EXP LDBL_MAX_EXP 这些宏定义以FLT_RADIX为基数的指数的最大整数值。 |
8 | FLT_MAX_10_EXP +37 DBL_MAX_10_EXP +37 LDBL_MAX_10_EXP +37 这些宏定义以10为基数的指数的最大整数值。 |
9 | FLT_MAX 1E+37 DBL_MAX 1E+37 LDBL_MAX 1E+37 这些宏定义最大有限浮点值。 |
10 | FLT_EPSILON 1E-5 DBL_EPSILON 1E-9 LDBL_EPSILON 1E-9 这些宏定义可表示的最小有效位。 |
11 | FLT_MIN 1E-37 DBL_MIN 1E-37 LDBL_MIN 1E-37 这些宏定义最小浮点值。 |
示例1
以下是C库头文件float.h,用于定义一些宏(浮点)常量的值。
#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 = %.10d\n", FLT_MANT_DIG); }
输出
执行上述代码后,我们将得到以下结果:
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
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
示例2
以下是C库头文件float.h,用于定义一些宏(浮点)常量的值。
#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 = %.10d\n", FLT_MANT_DIG); }
输出
执行上述代码后,我们将得到以下结果:
The maximum value of float = 3.4028234664e+38 The minimum value of float = 1.1754943508e-38 The number of digits in the number = 1.1754943508e-38
示例3
下面的程序使用宏(FLT_RADIX)来测量基数指数的值。
#include <stdio.h> #include <float.h> int main() { // Get the value of FLT_RADIX int radix = FLT_RADIX; printf("The base (radix) of the exponent representation: %d\n", radix); return 0; }
输出
上述代码产生以下输出:
The base (radix) of the exponent representation: 2
示例4
在这里,我们定义了三个不同的宏,即FLT_MAX、DBL_MAX和LDBL_MAX,它们使用香蕉的长度计算堆叠到珠穆朗玛峰高度所需的香蕉数量。
#include <stdio.h> #include <float.h> int main() { // Heights in inches double heightOfEverestInFeet = 29031.7; double heightOfEverestInInches = heightOfEverestInFeet * 12.0; // Banana length (FLT_MAX, DBL_MAX, and LDBL_MAX are all approximately 1E+37) double banayanLength = 1E+37; // Calculate the number of bananas needed double numBanayan = heightOfEverestInInches / banayanLength; printf("Height of Mount Everest: %.2lf feet\n", heightOfEverestInFeet); printf("Length of a magical banayan: %.2lf inches\n", banayanLength); printf("Number of bananas needed to reach the summit: %.2e banayan\n", numBanayan); return 0; }
输出
执行上述代码后,我们将得到以下输出:
Height of Mount Everest: 29031.70 feet Length of a magical banayan: 9999999999999999538762658202121142272.00 inches Number of bananas needed to reach the summit: 3.48e-32 banayan