gpt4 book ai didi

c - 为什么2^63减去1000,结果是1024而不是1000?

转载 作者:行者123 更新时间:2023-11-30 20:19:09 24 4
gpt4 key购买 nike

您能否解释一下为什么差异是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com