gpt4 book ai didi

c - C 中 Linux 上的奇怪 cbrt() 结果

转载 作者:IT王子 更新时间:2023-10-29 01:25:47 25 4
gpt4 key购买 nike

为什么这两个返回值为cbrt()功能不同?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main() {
double nb = 56623104;
double v1 = cbrt(nb);
printf("v1 -> %.15f\n",v1);

double v2 = cbrt((double) 56623104);
printf("v2 -> %.15f\n",v2);
}

编译:

gcc toto.c -o toto -lm && ./toto

结果:

v1 -> 384.000000000000057
v2 -> 384.000000000000000

最佳答案

恭喜,这是一个编译器错误。编译器通过提前评估其中一个 cbrt 调用来优化您的代码,不幸的是,编译器的 cbrt 版本与您在 libm 中的版本不同。您还会注意到传递 -O2 会导致 v2 结果也“错误”(即使从数学上讲它是正确的)。

我确认我的系统上存在该错误

cc (Debian 6.3.0-5) 6.3.0 20170124

此错误应报告给编译器开发人员 (https://gcc.gnu.org/bugs/),但最好先搜索错误存储库。

关于c - C 中 Linux 上的奇怪 cbrt() 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42124233/

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