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

前端 未结 8 1731
忘掉有多难
忘掉有多难 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:29

    Whats happens is the pow function returns a double so when you do this

    int i = pow(sections, 5- t -1);  
    

    the decimal .99999 cuts of and you get 9999.

    while printing directly or comparing it with 10000 is not a problem because it is runded of in a sense.

提交回复
热议问题