Why am I getting unexpected output when using floo

2019-03-01 07:29发布

So, I ran this code on my code blocks:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int a;
    a=pow(10,9);
    cout<<a<<endl;
    a=ceil(pow(10,9));
    cout<<a<<endl;
    a=floor(pow(10,9));
    cout<<a<<endl;
    return 0;
}

I got the output as:

 999999999
 100000000
 100000000

1st output was not 10^9 due to truncation effect,which means that pow(10,9) was something like 999999999.99999.., but then how come floor of this thing is 1000000000 ??

标签: c++ pow floor
1条回答
女痞
2楼-- · 2019-03-01 08:15

Actually, the maximum value for int is 2,147,483,647, therefore there should be no overflow or truncation (it's an int). And my output is exactly:

1000000000 1000000000 1000000000

查看更多
登录 后发表回答