Strange behaviour of the pow function

后端 未结 5 710
借酒劲吻你
借酒劲吻你 2020-11-22 12:23

While running the following lines of code:

int i,a;    

for(i=0;i<=4;i++)  
{    
    a=pow(10,i);    
    printf(\"%d\\t\",a);    
}   
5条回答
  •  深忆病人
    2020-11-22 12:53

    The function pow() returns a double. You're assigning it to variable a, of type int. Doing that doesn't "round off" the floating point value, it truncates it. So pow() is returning something like 99.99999... for 10^2, and then you're just throwing away the .9999... part. Better to say a = round(pow(10, i)).

提交回复
热议问题