Why am I getting unexpected output when using floor with pow?

元气小坏坏 提交于 2019-12-02 07:20:34

问题


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 ??


回答1:


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



来源:https://stackoverflow.com/questions/27479620/why-am-i-getting-unexpected-output-when-using-floor-with-pow

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!