c++ - 信号和除零
问题描述
我在代码中得到了除以零的不需要的结果(代码是用 C++ 编写的):
#include <iostream>
#include <signal.h>
#include <fenv.h>
void div0(int signo, siginfo_t *info, void *ucontext)
{
if(signo == SIGFPE && info->si_code == FPE_FLTDIV)
{
std::cout << "You can't divide numbers by 0, idiot!\n";
exit(EXIT_FAILURE);
}
}
int main()
{
feenableexcept(FE_ALL_EXCEPT);
struct sigaction act;
act.sa_flags = SA_SIGINFO | SA_NODEFER;
act.sa_sigaction = div0;
int retn = sigaction(SIGFPE, &act, NULL);
if(retn == -1)
{
perror("sigaction");
return -1;
}
int n1, n2;
std::cout << "Enter a number: ";
std::cin >> n1;
std::cout << "Enter another number to divide by: ";
std::cin >> n2;
double div = n1 / 1.0 / n2;
std::cout << n1 << " / " << n2 << " = " << div << "\n";
return 0;
}
当我0
在变量中赋值n2
时,结果是,例如:5 / 0 = inf
. 但我想得到:You can't divide numbers by zero, idiot!
。我将SIGFPE
信号与FPE_FLTDIV
信号代码一起使用,并尝试将其更改double
为float
,但仍然无法正常工作。我在互联网上搜索并没有找到任何解决方案。我错过了什么?谢谢您的帮助。
解决方案
为什么不尝试简单if (n2 == 0) std::cout << "You can't divide numbers by zero, i***t!"
另一种方法是if isnan(div) std::cout << "You already divided by zero, i***t!"
0/0 部分在double
和中是无声的float
。您将收到一个NaN
而不是任何异常。这将使您能够更好地处理错误。
添加以解决评论
如果要生成信号,则需要使用所有变量 asinteger
并避免编译器进行优化。在“The linux programming interface”一书中有很好的例子,你可以在这里找到
推荐阅读
- java - JSON 解析错误:无法构造 `...` 的实例(尽管至少存在一个 Creator)
- kotlin - 致命异常:java.io.InvalidObjectException:前置错误
- javascript - 使用复选框进行尾部选择
- spring - 如何以弹簧形式制作一组单选按钮?
- code-push - 如何查看有多少用户持有最新的代码推送包?
- node.js - 如何在使用 playwright-test 时以编程方式传递作品集参数?
- ssl - 本地主机的主题备用名称
- java - 在java中螺旋打印矩阵
- javascript - 从 metaweather API 获取数据以响应页面
- go - Go 支持函数式编程吗?