C Standard Library 简明教程
C Library - <float.h>
C 库 float.h 头文件包含一组与浮点值相关的各种平台相关常量。这些常量由 ANSI C 提出。浮点数宏允许开发人员创建更多可移植的程序。在继续使用所有常量之前,最好先了解浮点数,它由以下四个元素组成:
The C Library float.h header file contains a set of various platform-dependent constants related to floating point values. These constants are proposed by ANSI C. The float macros allow developers to create more portable programs. Before proceeding with all the constants, it is better to understand the floating-point number which consist following four elements −
Sr.No. |
Component & Component Description |
1 |
S sign ( +/- ) |
2 |
b The base or radix of the exponent representation, 2 for binary, 10 for decimal, 16 for hexadecimal, and so on… |
3 |
e Exponent, an integer between a minimum emin and a maximum emax. |
4 |
p The precision is a number of base-b digits in the significand. |
根据上述 4 个组成部分,浮点数将以以下形式显示其值 −
Based on the above 4 components, a floating point will have its value as follows −
floating-point = ( S ) p x be
or
floating-point = (+/-) precision x baseexponent
Library Macros
以下值特定于实现,并且通过 #define 指令定义,但是这些值可能不会低于此处所示的值。请注意,在所有实例中,FLT 指的是类型 float ,DBL 指的是 double ,而 LDBL 指的是 long double 。
The following values are implementation-specific and defined with the #define directive, but these values may not be any lower than what is given here. Note that in all instances FLT refers to type float, DBL refers to double, and LDBL refers to long double.
Sr.No. |
Macro & Description |
1 |
FLT_ROUNDS Defines the rounding mode for floating point addition and it can have any of the following values − -1 − indeterminable0 − towards zero1 − to nearest2 − towards positive infinity3 − towards negative infinity |
2 |
FLT_RADIX 2 This defines the base radix representation of the exponent. A base-2 is binary, base-10 is the normal decimal representation, base-16 is Hex. |
3 |
FLT_MANT_DIG DBL_MANT_DIG LDBL_MANT_DIG These macros define the number of digits in the number (in the FLT_RADIX base). |
4 |
FLT_DIG 6 DBL_DIG 10 LDBL_DIG 10 These macros define the maximum number decimal digits (base-10) that can be represented without change after rounding. |
5 |
FLT_MIN_EXP DBL_MIN_EXP LDBL_MIN_EXP These macros define the minimum negative integer value for an exponent in base FLT_RADIX. |
6 |
FLT_MIN_10_EXP -37 DBL_MIN_10_EXP -37 LDBL_MIN_10_EXP -37 These macros define the minimum negative integer value for an exponent in base 10. |
7 |
FLT_MAX_EXP DBL_MAX_EXP LDBL_MAX_EXP These macros define the maximum integer value for an exponent in base FLT_RADIX. |
8 |
FLT_MAX_10_EXP +37 DBL_MAX_10_EXP +37 LDBL_MAX_10_EXP +37 These macros define the maximum integer value for an exponent in base 10. |
9 |
FLT_MAX 1E+37 DBL_MAX 1E+37 LDBL_MAX 1E+37 These macros define the maximum finite floating-point value. |
10 |
FLT_EPSILON 1E-5 DBL_EPSILON 1E-9 LDBL_EPSILON 1E-9 These macros define the least significant digit representable. |
11 |
FLT_MIN 1E-37 DBL_MIN 1E-37 LDBL_MIN 1E-37 These macros define the minimum floating-point values. |
Example 1
以下是 C 库头文件 float.h,用于定义少量宏(float)常量的值。
Following is the C Library header file float.h to define the values of few macros(float) constant.
#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);
}
Example 2
以下是 C 库头文件 float.h,用于定义少量宏(float)常量的值。
Following is the C Library header file float.h to define the values of few macros(float) constant.
#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);
}
Example 3
以下程序使用宏(FLT_RADIX)测量基底指数的值。
Below the program uses the macros(FLT_RADIX) to measure the value of base exponents.
#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;
}
Example 4
在这里,我们定义了三种不同的宏,分别是 FLT_MAX、DBL_MAX 和 LDBL_MAX,它们使用香蕉长度计算将香蕉堆叠至珠穆朗玛峰高度所需的香蕉数量。
Here, we define the three different macros which are FLT_MAX, DBL_MAX, and LDBL_MAX that calculates the number of banayan needed to stack up to Mount Everest’s height using banayan lengths.
#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;
}