C++ exp() 函数有些奇怪

Something really weird with C++ exp() function

在 C++ 中实现 mySqrt 函数时,我使用了 exp() 函数,如下所示:

int mySqrt(int x) {
    // For x = 2147395600
    cout << exp(0.5*log(x)) << "  ";     // It prints 46340
    return exp(0.5*log(x));              // But returns 46339
}

我试图 google 此行为的原因,但找不到任何东西。我什至尝试使用 double 但仍然是相同的输出。

有什么解释吗?

使用此代码

#include <iostream>
#include <cmath>
#include <cstdio>
using std::cout;

int mySqrt(int x) {
    // For x = 2147395600
    cout << exp(0.5*log(x)) << "  ";     // It prints 46340
    return exp(0.5*log(x));              // But returns 46349
}

int main(void) {
    std::cout << mySqrt(2147395600) << "\n";
    printf("%.30f\n", exp(0.5*log(2147395600)));
    return 0;
}

I got output:

46340  46339
46339.999999999978172127157449722290

似乎该值在传递给 cout 时四舍五入,而在转换为 int 时被截断。