unusual output from pow

前端 未结 3 1302
面向向阳花
面向向阳花 2020-12-11 20:38

The following 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(\"%         


        
3条回答
  •  -上瘾入骨i
    2020-12-11 21:12

    You are using a poor-quality math library. A good math library returns exact results for values that are exactly representable.

    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.

提交回复
热议问题