查看include文件,在float.h头文件中有很多关于浮点数的宏定义:
#define FLT_EPSILON 1.19209290E-07F
#define LDBL_EPSILON 1.084202172485504E-19[/quote]
这两个宏定义可用来作为float、 long double趋0最小的判断值。即:
#include <float.h>;
double a, b;
if( abs(a-b) < FLT_EPSILON)
For EPSILON, you can use the constants FLT_EPSILON, which is defined for float as 1.192092896e-07F,
or DBL_EPSILON, which is defined for double as 2.2204460492503131e-016.
You need to include float.h for these constants.
These constants are defined as the smallest positive number x, such that x+1.0 is not equal to 1.0.
Because this is a very small number, you should employ user-defined tolerance
for calculations involving very large numbers.
FLT_EPSILON用于float类型。
它是满足 x+1.0不等于1.0的最小的正数
也就是说,所有比FLT_EPSILON小的正数x,x+1.0==1.0都是成立的。
那段定义我明白,我的不明白的是为什么是1.192093e-007,而不是0.000001?
为什么要是0.000001,这个数是由浮点数的表示方法决定的,而不是随便设置的
单精度的浮点数所能识别的最小精度
大伙谁能按照float的存储给咱们讲讲为啥不?
1 8 23
1.0*2^-127
我感觉最小的数字应该是这个数
浮点1 为 符号位 0 , 阶码 0 + 127 , 尾数 1 (0000..) 23 个0 , 最前面的 1 省略 , 既 0x3f800000 , 加上最小的让其不为1的数后结果是 0x3f800001 , 既 1 + 2^-23 ....
因此 flt_epsilon == 2^-23 ....
人家没有装b,float尾数是23位
计算机表示为(1/2)^23 = 0。00000011920
这个其实就是一个定义,叫做机器-ε(machine epsilon),表示1与大于的第一个(或最小)浮点数之差。
对单精度来说,这个差就是2^-23 ,也就是1.190928955078125-e7 约等于1.19092896-e7,对于双精度来说就是
2^-52,约等于2.2204460492503131e-016,这两个分别是FLT_EPSILON 和DBL_EPSILON