当我在 VC++ 2013(32 位,无优化)中运行以下代码时:
#include <cmath>
#include <iostream>
#include <limits>
double mulpow10(double const value, int const pow10)
{
static double const table[] =
{
1E+000, 1E+001, 1E+002, 1E+003, 1E+004, 1E+005, 1E+006, 1E+007,
1E+008, 1E+009, 1E+010, 1E+011, 1E+012, 1E+013, 1E+014, 1E+015,
1E+016, 1E+017, 1E+018, 1E+019,
};
return pow10 < 0 ? value / table[-pow10] : value * table[+pow10];
}
int main(void)
{
double d = 9710908999.008999;
int j_max = std::numeric_limits<double>::max_digits10;
while (j_max > 0 && (
static_cast<double>(
static_cast<unsigned long long>(
mulpow10(d, j_max))) != mulpow10(d, j_max)))
{
--j_max;
}
double x = std::floor(d * 1.0E9);
unsigned long long y1 = x;
unsigned long long y2 = std::floor(d * 1.0E9);
std::cout
<< "x == " << x << std::endl
<< "y1 == " << y1 << std::endl
<< "y2 == " << y2 << std::endl;
}
我明白了
x == 9.7109089990089994e+018
y1 == 9710908999008999424
y2 == 9223372036854775808
在调试器中。
我疯了。有人可以向我解释一下到底y1
有y2
什么不同的价值观吗?
更新:
这似乎只发生在/Arch:SSE2
or下/Arch:AVX
,而不是/Arch:IA32
or /Arch:SSE
。