c ++:在数学错误而不是nan中创建实际错误

c++: create actual error in math errors instead of nan

我有一个非常大的代码,很难调试。在某些情况下,它会给出 nan 作为结果。

我知道它可能是由像 sqrt(-1) 这样的数学错误产生的。但我无法发现错误。如果我能在数学错误中产生错误,而不是 nan,我就能很容易地发现错误。

我可以通过定义宏来实现吗?我想我在某处看到了这样的解决方案。

注意:我不想在每次数学运算后使用if(isnan(res)) exit(0);

感谢chris here is a summery of math error handling

这些浮点异常在 cfenv 头文件中定义:FE_ALL_EXCEPTFE_DIVBYZEROFE_INEXACTFE_INVALIDFE_OVERFLOWFE_UNDERFLOW。这些名称是不言自明的。

我们可以使用 fetestexcept. we can clear list of happened exceptions with feclearexcept.

来确定当前设置了哪些指定的浮点异常子集

下面是一段代码,显示了我们如何确定引发的浮点异常:

C++11 标准:

#include <iostream>
#include <cfenv>
#include <cmath>

#pragma STDC FENV_ACCESS ON

volatile double zero = 0.0; // volatile not needed where FENV_ACCESS is supported
volatile double one = 1.0;  // volatile not needed where FENV_ACCESS is supported

int main()
{
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout <<  "1.0/0.0 = " << 1.0 / zero << '\n';
    if(std::fetestexcept(FE_DIVBYZERO)) {
        std::cout << "division by zero reported\n";
    } else {
        std::cout << "divsion by zero not reported\n";
    }

    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "1.0/10 = " << one/10 << '\n';
    if(std::fetestexcept(FE_INEXACT)) {
        std::cout << "inexact result reported\n";
    } else {
        std::cout << "inexact result not reported\n";
    }

    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "sqrt(-1) = " << std::sqrt(-1) << '\n';
    if(std::fetestexcept(FE_INVALID)) {
        std::cout << "invalid result reported\n";
    } else {
        std::cout << "invalid result not reported\n";
    }
}

在 C99 中:

#include <stdio.h>
#include <math.h>
#include <float.h>//for DBL_MIN and DBL_MAX
#include <fenv.h>

#pragma STDC FENV_ACCESS ON
void show_fe_exceptions(void)
{
    printf("exceptions raised:");
    if(fetestexcept(FE_DIVBYZERO)) printf(" FE_DIVBYZERO");
    if(fetestexcept(FE_INEXACT))   printf(" FE_INEXACT");
    if(fetestexcept(FE_INVALID))   printf(" FE_INVALID");
    if(fetestexcept(FE_OVERFLOW))  printf(" FE_OVERFLOW");
    if(fetestexcept(FE_UNDERFLOW)) printf(" FE_UNDERFLOW");
    feclearexcept(FE_ALL_EXCEPT);
    printf("\n");
}

int main(void)
{
    printf("MATH_ERREXCEPT is %s\n",
           math_errhandling & MATH_ERREXCEPT ? "set" : "not set");

    printf("0.0/0.0 = %f\n", 0.0/0.0);
    show_fe_exceptions();

    printf("1.0/0.0 = %f\n", 1.0/0.0);
    show_fe_exceptions();

    printf("1.0/10.0 = %f\n", 1.0/10.0);
    show_fe_exceptions();

    printf("sqrt(-1) = %f\n", sqrt(-1));
    show_fe_exceptions();

    printf("DBL_MAX*2.0 = %f\n", DBL_MAX*2.0);
    show_fe_exceptions();

    printf("nextafter(DBL_MIN/pow(2.0,52),0.0) = %.1f\n",
                      nextafter(DBL_MIN/pow(2.0,52),0.0));
    show_fe_exceptions();
}