您能解释一下为什么差异是1024而不是1000吗?
int main(void) {
unsigned long long int x = pow(2,63);
double y = pow(2,63) - 1000;
double z = 9223372036854775808.0 - 1000.0;
printf("%llu\n%f\n%f\n", x,y,z);
}
输出为:
9223372036854775808
9223372036854774784.000000
9223372036854774784.000000
答案 0 :(得分:1)
由于在类型double
中可表示的浮点数中,9223372036854774784
恰好是数学上正确的结果9223372036854774808
的最接近点。
让我们检查一下您的9223372036854774784
的代表社区
#include <float.h>
#include <math.h>
#include <stdio.h>
int main()
{
double d = 9223372036854774784;
printf("%lf\n%lf\n%lf\n", nextafter(d, -DBL_MAX), d, nextafter(d, DBL_MAX));
}
在我的平台上,输出为
9223372036854773760.000000
9223372036854774784.000000
9223372036854775808.000000
您会选择哪一个?您的实现决定使用9223372036854774784
。