C++:在数学错误中创建实际错误而不是 NAN
c++: create actual error in math errors instead of nan
我有一个非常大的代码,很难调试。 在某些情况下,它会为结果提供nan
。
我知道它可能是在像sqrt(-1)
这样的数学错误中产生的,但我无法发现错误。如果我能在数学错误中产生错误,而不是nan,我就能很容易地发现错误。
我可以通过定义宏来实现这一点吗? 我想我在某处看到了这样的解决方案。
注意:我不想在每次数学运算后都使用if(isnan(res)) exit(0);
。
感谢克里斯,这里是数学错误处理的夏天。
这些浮点异常定义在头文件中cfenv
:FE_ALL_EXCEPT
、FE_DIVBYZERO
、FE_INEXACT
、FE_INVALID
、FE_OVERFLOW
、FE_UNDERFLOW
。 名称是不言自明的。
我们可以确定当前使用 fetestex 例外设置了浮点异常的指定子集。 我们可以使用 FeclearExclude 清除已发生的异常列表。
下面是一个代码,它显示了我们如何确定引发的浮点异常:
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 reportedn";
} else {
std::cout << "divsion by zero not reportedn";
}
std::feclearexcept(FE_ALL_EXCEPT);
std::cout << "1.0/10 = " << one/10 << 'n';
if(std::fetestexcept(FE_INEXACT)) {
std::cout << "inexact result reportedn";
} else {
std::cout << "inexact result not reportedn";
}
std::feclearexcept(FE_ALL_EXCEPT);
std::cout << "sqrt(-1) = " << std::sqrt(-1) << 'n';
if(std::fetestexcept(FE_INVALID)) {
std::cout << "invalid result reportedn";
} else {
std::cout << "invalid result not reportedn";
}
}
在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 %sn",
math_errhandling & MATH_ERREXCEPT ? "set" : "not set");
printf("0.0/0.0 = %fn", 0.0/0.0);
show_fe_exceptions();
printf("1.0/0.0 = %fn", 1.0/0.0);
show_fe_exceptions();
printf("1.0/10.0 = %fn", 1.0/10.0);
show_fe_exceptions();
printf("sqrt(-1) = %fn", sqrt(-1));
show_fe_exceptions();
printf("DBL_MAX*2.0 = %fn", DBL_MAX*2.0);
show_fe_exceptions();
printf("nextafter(DBL_MIN/pow(2.0,52),0.0) = %.1fn",
nextafter(DBL_MIN/pow(2.0,52),0.0));
show_fe_exceptions();
}
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- g++的分段错误(在NaN上使用to_string两次时)
- 调用某个回调函数两次会导致分段错误:Nan
- 插件中节点.js/Nan 回调C++不频繁的隔离错误
- 运行代码时出现 NaN 错误
- C++:在数学错误中创建实际错误而不是 NAN
- 查找NaN错误的原因