gpt4 book ai didi

c - 在 C 中获取 inf 输出的简单精度测试

转载 作者:行者123 更新时间:2023-11-30 18:18:33 25 4
gpt4 key购买 nike

尝试测试精度,使总和从 1 到 100 万,但我的输出始终为 inf。

    #include <stdio.h>



int main()
{
float x=0.0 ;
double y =0.0;
int i;
for(i=1; i<100000; i = i+1)
{
x=(float) x+( 1.0f/(3.0f*(float)(i*i)));
y=(double) y+ ( 1.0/(3.0*(double)(i*i)));
}
printf("Natural Order \n");
printf("Single Precision: ");
printf("%f", x);
printf("\n");
printf("Double Precision: ");
printf("%lf", y);
printf("\n");
}

我已经多次更改 i 范围,但仍然得到 inf 作为我的输出。

最佳答案

表达式i^2表示i BITWISE-XOR 2,而不是重复乘法。

i^2 的某些值为零 - 当 i=2 时 - 这意味着有时会被零除。

(i^2) 替换为 (i*i)

关于c - 在 C 中获取 inf 输出的简单精度测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35158630/

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