计算C中的浮点的epsilon [英] Calculate epsilon for a float in C
问题描述
我必须使用位/整数运算来找出C中给定值的epsilon.根据位模式,我知道计算下一个邻居需要增加尾数,如果溢出,则要增加指数-但我不确定从哪里开始计算ε.
I have to use bit/integer ops to figure out the epsilon for a given value in C. I know in terms of bit patterns that computing the next neighbor involves incrementing the mantissa, and if that overflows, to increment the exponent - but I'm not sure where to begin in terms of calculating the epsilon.
我无法使用浮点数学运算-因此,我需要直接生成位模式.这使操作变得棘手,因为我无法减去.
这是我的理解(基于我在SO上所做的一些研究):范围随着数字的增加而明显变化,但我不确定如何使用 FLT_EPSILON
生成正确的数字.对于2 ^ 2x数字,也许是(FLT_EPSILON-1)*数字
?
Here's my understanding (based on some research I've done here on SO): the range changes obviously as the numbers get bigger, but I'm not sure how to use FLT_EPSILON
to generate the right numbers. For 2^2x numbers, perhaps it is (FLT_EPSILON - 1) * number
?
推荐答案
如果epsilon只是尾数增加一个LSB的值,则该值应为2 ^(exp-23).因此,如果您输入的是 [s] [exp] [mantissa]
,则epsilon应该是 [0] [exp-23] [0]
.当然,当输入为1.0( [0] [127] [0]
)时,结果为 [0] [104] [0]
= 2 ^-23 = FLT_EPSILON
.
If your epsilon is simply the value of one LSB increment to the mantissa, the value should be 2^(exp-23). So if your input is [s][exp][mantissa]
your epsilon should be [0][exp-23][0]
. Sure enough, when given an input of 1.0 ([0][127][0]
), the result is [0][104][0]
= 2^-23 = FLT_EPSILON
.
这篇关于计算C中的浮点的epsilon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!