Strange behaviour of the pow function

后端 未结 5 586
借酒劲吻你
借酒劲吻你 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:51

    Mathematically, the integer power of an integer is an integer.

    In a good quality pow() routine this specific calculation should NOT produce any round-off errors. I ran your code on Eclipse/Microsoft C and got the following output:

    1   10  100 1000    10000   
    

    This test does NOT indicate if Microsoft is using floats and rounding or if they are detecting the type of your numbers and choosing the appropriate method.

    So, I ran the following code:

    #include 
    #include 
    main ()
    {
        double i,a;
    
        for(i=0.0; i <= 4.0 ;i++)
        {
            a=pow(10,i);
            printf("%lf\t",a);
        }
    }
    

    And got the following output:

    1.000000    10.000000   100.000000  1000.000000 10000.000000    
    

提交回复
热议问题