Latest web development tutorials

C standard library - <float.h>

Brief introduction

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 表示十六进制,等等...
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

Macro library

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定义浮点加法的舍入模式,它可以是下列任何一个值:
  • -1 - 无法确定

  • 0 - 趋向于零

  • 1 - 去最近的值

  • 2 - 趋向于正无穷

  • 3 - 趋向于负无穷

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

这些宏定义了最小的浮点值。

Examples

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