为什么我会得到"floating point exception"

Why do I get a "floating point exception"

本文关键字:floating exception point 为什么      更新时间:2023-10-16

不要求解决方案或其他任何问题,只是一个一般问题。当调用getGCD((函数时,我会得到浮点异常。为什么是这样?我已经研究了这一点,找不到直接的答案。谢谢,下面也是我正在使用的代码。

 #include <iostream>
using namespace std;
class D: public E {
private:
        int var2;
public:
        D(int var, int var2) : E(var) {
                var2 = var2;
        }
        void getgcd() {
                int temp;
                int newVar = E::var;
                while (var != 0) {
                        temp = newVar % var2;
                        newVar = var2;
                        var2 = temp;
                }
                cout << "The GCD of " << newVar << " and " << var2 << " is " << var2 << endl;
        }
};
#include <iostream>
#include "A4p2.cpp"
#include "A4p3.cpp"
using namespace std;
int main(int argc, char *argv[]) {
        int x, y;
        x = atoi(argv[1]);
        y = atoi(argv[2]);
        if (x < 1 || x > 50) {
                cout << "Input must be between 1-50" << endl;
                exit(0);
        }
        E test(x);
        D test1(x, y);
        test.play();
        test1.getgcd();
        return 0;
}

linux不幸的是,在 SIGFPE上通过零映射 integer disemo/modulo,然后将其天真地打印为"浮点异常"。我认为我从来没有看到它来自浮点操作(可能是因为大多数FP例外必须明确打开,而整数除以零在x86上总是有故障(。

很可能您的var2等于

中的零
temp = newVar % var2;

反过来可能是由于在该循环中检查var但使用/修改var2

而引起的。
while (var != 0) {
        temp = newVar % var2;
        newVar = var2;
        var2 = temp;
}

您可能想要while (var2 != 0)