您能否解释一下为什么差异是 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
最佳答案
因为在 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
。
关于c - 为什么2^63减去1000,结果是1024而不是1000?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51459658/