为什么我会得到"floating point exception"
Why do I get a "floating point exception"
不要求解决方案或其他任何问题,只是一个一般问题。当调用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)
。
相关文章:
- 为重写std::exception的库生成swig接口时出错
- 什么是 std::exception::what() 以及为什么要使用它?
- 链表"exception thrown"
- 为什么程序员同时使用 std::bad_alloc 和 std::exception.是否 std::例外 仅是不够的
- C++ broken_promise exception
- 请参阅在 Visual Studio 2019 中捕获 std::exception 时对函数模板实例化消息的引用
- QVTKWidget SetRenderWindow() with PCLVisualizer Exception
- 自定义派生的 std::exception 类的 'what' 函数返回神秘的废话
- 使用flag、return、exception、goto或break中止过程
- 由于某种原因,我的代码中出现了 [json.exception.type_error.302]。我知道错误意味着什么,但我不知道哪里有故障
- 派生自 std::exception 的类的赋值运算符
- 尝试打印字符串时"Python Exception <class 'gdb.error'> There is no member named _M_dataplus."
- std::exception :使用虚函数和继承与变量
- MFC ODBC SQLConfigDataSource() Unhandled Exception
- 这段 C++ 代码的奇怪行为(std::wcout 和 std::exception)
- VTK 7.1.1: vtkX3DExporter exception
- 无法在"catch(const std::exception &ex)"中捕获 std::invalid_argument
- opencv cv::addText exception if no window
- 为什么我会得到"floating point exception"
- 为什么我会收到错误"Floating point exception"?