【发布时间】:2013-11-30 02:11:35
【问题描述】:
我正在移植一些比较浮点数以处理 64 位双精度数而不是 32 位浮点数的代码,但我对代码中使用的一些幻数有点困惑。
来源:http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
代码如下:
bool AlmostEqual2sComplement(float A, float B, int maxUlps)
{
// Make sure maxUlps is non-negative and small enough that the
// default NAN won't compare as equal to anything.
assert(maxUlps > 0 && maxUlps < 4 * 1024 * 1024);
int aInt = *(int*)&A;
// Make aInt lexicographically ordered as a twos-complement int
if (aInt < 0)
aInt = 0x80000000 - aInt;
// Make bInt lexicographically ordered as a twos-complement int
int bInt = *(int*)&B;
if (bInt < 0)
bInt = 0x80000000 - bInt;
int intDiff = abs(aInt - bInt);
if (intDiff <= maxUlps)
return true;
return false;
}
问题:
让我难过的主要是断言中的4 * 1024 * 1024 数字。这代表什么?对于 64 位双精度,这个值是多少?两者都一样吗?
0x80000000 幻数也用作负零浮点数的 int 表示。所以我猜对于 64 位双打,这将不得不增加到 0x8000000000000000?
【问题讨论】:
标签: c++ floating-point floating-point-precision