gpt4 book ai didi

c++ - 该程序的输出是什么?

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

#include<stdio.h>
void main()
{
int i;
float x=0.8;
for(i=0; i<100; i++)
x=x*(1-x)*5;
printf("%f \n",x);
}

在我看来,答案0.80000000看起来很简单,但真正的答案是别的东西。其输出值“-inf”出现运行时错误。为什么?

最佳答案

这意味着负无穷大。

EEE 754 floating point numbers can represent positive or negative infinity, and NaN (not a number). These three values arise from calculations whose result is undefined or cannot be represented accurately. You can also deliberately set a floating-point variable to any of them, which is sometimes useful. Some examples of calculations that produce infinity or NaN:

这是您的代码的输出

0.800000 
0.800000
0.800000
0.800001
0.799996
0.800011
0.799966
0.800103
0.799692
0.800923
0.797227
0.808280
0.774816
0.872380
0.556664
1.233946
-1.443383
-17.633698
-1642.905029
-13503899.000000
-911776526893056.000000
-4156682286578109086379962007552.000000
-inf
after this -inf -inf

因为该值超出了 flat 的范围

关于c++ - 该程序的输出是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43504035/

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