不完全从 double 转换为 int - 问题似乎出在 CPU 指令中

发布于 2024-10-04 21:34:13 字数 1490 浏览 0 评论 0原文

当我调试我的代码时,我感到很惊讶。这里我提供示例代码

#include<QMessageBox>
#include<iostream>
#include<math.h>
#include<QApplication>
using namespace std;

class MyMessageBox: public QMessageBox
{
    public:
        MyMessageBox(string message,QWidget *parent=0) :
            QMessageBox(
                QMessageBox::NoIcon,
                QString("ErrorMessage"),
                QString(message.c_str()),
                QMessageBox::Ok,
                parent,
                Qt::Widget)
        {
        }
};

void Hai()
{
    int tempi = 4;
    double a = pow(10,tempi);
    int temp = int(pow(10,tempi));

    //int temp=a;

    MyMessageBox mb1((QString::number(pow(10,tempi))+
                      " *** "+
                      QString::number(temp)).toStdString());
    mb1.exec();
}

int main(int argc, char * argv[])
{

    QApplication app(argc,argv);
    Hai();

    return app.exec();
}

,结果是,

10000 *** 9999

要点是,这种情况仅发生在 4 次幂 (=pow(x,4) 上,而不是任何其他次幂上。

编辑:

我尝试了使用 Visual Studio 的最少代码,但它准确地生成了 10,000 但是,直到我显式进行类型转换后才编译。代码如下。

#include<iostream>
#include<math.h>

using namespace std;

void Hai()
{
    int tempi = 4;
    double a=pow(10.0,tempi);
    int temp=pow(10.0,tempi);

    cout << " a " << a << " temp " << temp << endl ;
}

int main(int argc, char * argv[])
{
    Hai();
    return 1;
}

I got surprised when I debugged my code. Here I provide example code

#include<QMessageBox>
#include<iostream>
#include<math.h>
#include<QApplication>
using namespace std;

class MyMessageBox: public QMessageBox
{
    public:
        MyMessageBox(string message,QWidget *parent=0) :
            QMessageBox(
                QMessageBox::NoIcon,
                QString("ErrorMessage"),
                QString(message.c_str()),
                QMessageBox::Ok,
                parent,
                Qt::Widget)
        {
        }
};

void Hai()
{
    int tempi = 4;
    double a = pow(10,tempi);
    int temp = int(pow(10,tempi));

    //int temp=a;

    MyMessageBox mb1((QString::number(pow(10,tempi))+
                      " *** "+
                      QString::number(temp)).toStdString());
    mb1.exec();
}

int main(int argc, char * argv[])
{

    QApplication app(argc,argv);
    Hai();

    return app.exec();
}

and the result is,

10000 *** 9999

And the main point is, this thing happens only for power 4 (=pow(x,4), not for any other powers.

EDIT:

I tried a minimal code with Visual Studio, but it yields 10,000 exactly. But, it never compiled until I made the type conversion explicitly. The code is given below

#include<iostream>
#include<math.h>

using namespace std;

void Hai()
{
    int tempi = 4;
    double a=pow(10.0,tempi);
    int temp=pow(10.0,tempi);

    cout << " a " << a << " temp " << temp << endl ;
}

int main(int argc, char * argv[])
{
    Hai();
    return 1;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

貪欢 2024-10-11 21:34:13

你的问题是浮点运算的结果是 9999.9999999... 所以 int(...)int(9999.9999999...) ,当然,是 9999。由于浮点运算很少是精确的,因此您编写的代码一定不能期望它们是精确的。在这种情况下,您可以使用舍入函数。

正如评论中所指出的,您的 pow(10,4) 实现不太可能不产生正整数,因此您的示例可能有缺陷。然而,这一点仍然成立:永远不要期望浮点结果是准确的。

另请参阅:为什么是 1.2 * 30 = 35?

Your problem is that the result of your floating-point operation is 9999.9999999... so int(...) is int(9999.9999999...) which, of course, is 9999. Since floating point operations are rarely exact, you must not write your code to expect them to be. In this case, you could use a rounding function.

As was pointed out in the comment, it's extremely unlikely that your implementation of pow(10,4) doesn't yield a positive integer, so your example is probably flawed. However, the point still stands: don't ever expect floating point results to be exact.

See also: why is 1.2 * 30 = 35?

小女人ら 2024-10-11 21:34:13

请记住 pow(10,4) 和 pow(10.0,4) 和 pow(10.0,4.0) 不调用相同的函数。

Remember pow(10,4) and pow(10.0,4) and pow(10.0,4.0) don't call the same function.

活泼老夫 2024-10-11 21:34:13

没有标准可以保证 pow 函数的准确性。负责任的库供应商尝试获得像这样的简单精确案例,但如果您想编写良好的可移植代码,则不能依赖于此(正如您的经验所示)。

如果您需要精确计算小整数幂,请通过重复乘法(或使用用于此目的的库函数)来计算它们,而不是通过调用 pow 来计算。

There is no standard that guarantees the accuracy of the pow function. Responsible library vendors try to get simple exact cases like this right, but you cannot depend on this (as your experience shows) if you want to write good portable code.

If you require small integer powers computed exactly, compute them with repeated multiplication (or with a library function intended for that purpose), not with a call to pow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文