从战俘输出异常 [英] unusual output from pow
问题描述
下面的C code
int main(){
int n=10;
int t1=pow(10,2);
int t2=pow(n,2);
int t3=2*pow(n,2);
printf("%d\n",t1);
printf("%d\n",t2);
printf("%d\n",t3);
return (0);
}
给出以下输出
100
99
199
我使用的是DEVCPP编译器。
它没有任何意义,对吧?
有任何想法吗?
(这POW(10,2)也许是东西
像99.9999不解释第一
输出。此外,我得到了相同的
即使我包括math.h中输出)
I am using a devcpp compiler. It does not make any sense, right? Any ideas? (That pow(10,2) is maybe something like 99.9999 does not explain the first output. Moreover, I got the same output even if I include math.h)
推荐答案
您使用的是劣质数学库。一个好的数学库返回确切的结果对于那些正好重新presentable值。
You are using a poor-quality math library. A good math library returns exact results for values that are exactly representable.
一般情况下,数学库例程必须是近似值,因为两者浮点格式不能准确地重新present精确的数学结果因为计算的各种功能是困难的。然而,对于 POW
,也有数量有限的结果是精确重新presentable,如10 2 。一个好的数学库将确保这些结果是正确返回。您正在使用的库无法做到这一点。
Generally, math library routines must be approximations both because floating-point formats cannot exactly represent the exact mathematical results and because computing the various functions is difficult. However, for pow
, there are a limited number of results that are exactly representable, such as 102. A good math library will ensure that these results are returned correctly. The library you are using fails to do that.
这篇关于从战俘输出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!