- 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程式設計資源
- C程式設計 - 教程
- C - 有用資源
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
示例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