为什么我在使用带 pow 的地板时会得到意外的输出

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

本文关键字:输出 意外 为什么 pow      更新时间:2023-10-16

所以,我在代码块上运行了这段代码:

#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;
}

我得到的输出为:

 999999999
 100000000
 100000000

由于截断效应,第一个输出不是 10^9,这意味着 pow(10,9) 类似于999999999.99999..,但是为什么这个东西的地板是1000000000??

实际上,int的最大值是 2,147,483,647,因此不应该有溢出或截断(这是一个int)。我的输出正是:

1000000000 1000000000 1000000000