Why pow(10,5) = 9,999 in C++

前端 未结 8 1764
忘掉有多难
忘掉有多难 2020-11-22 08:55

Recently i write a block of code:

const int sections = 10;

for(int t= 0; t < 5; t++){
   int i = pow(sections, 5- t -1);  
   cout << i << en         


        
8条回答
  •  忘掉有多难
    2020-11-22 09:22

    You're assigning the result to an int. That coerces it, truncating the number.

    This should work fine:

    for(int t= 0; t < 5; t++){
       double i = pow(sections, 5- t -1);  
       cout << i << endl;
    }
    

提交回复
热议问题