信号和除以零

问题描述

我在我的代码中得到了一个不需要的除以零的结果(代码是用 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 部门在 doublefloat 中保持沉默。您将收到 NaN 而不是任何异常。这将使您能够更好地处理错误。

添加以解决评论

如果要生成信号,则需要将所有变量用作 integer 并避免编译器的优化。 《Linux 编程接口》一书中有很好的例子you can find here