| C++ Builder 串口控件 | | C++ Builder 编程技巧 | | C++ Builder 操作指南 | | C++ Builder 参考手册 | | 基础知识 | | cfloat 浮点数 | | cmath 数学函数 | | • acos, acosf, acosl | | • acosh, acoshf, acoshl | | • asin, asinf, asinl | | • asinh, asinhf, asinhl | | • atan, atanf, atanl | | • atan2, atan2f, atan2l | | • atanh, atanhf, atanhl | | • ceil, ceilf, ceill | | • copysign, copysignf, copysignl | | • cos, cosf, cosl | | • cosh, coshf, coshl | | • exp, expf, expl | | • exp2, exp2f, exp2l | | • expm1, expm1f, expm1l | | • fabs, fabsf, fabsl | | • floor, floorf, floorl | | • fmod, fmodf, fmodl | | • frexp, frexpf, frexpl | | • hypot, hypotf, hypotl | | • ldexp, ldexpf, ldexpl | | • log, logf, logl | | • log10, log10f, log10l | | • log1p, log1pf, log1pl | | • log2, log2f, log2l | | • modf, modff, modfl | | • nan, nanf, nanl | | • poly, polyl | | • pow, powf, powl | | • pow10, pow10l | | • round, roundf, roundl | | • sin, sinf, sinl | | • sinh, sinhf, sinhl | | • sqrt, sqrtf, sqrtl | | • tan, tanf, tanl | | • tanh, tanhf, tanhl | | • trunc, truncf, truncl | | • _exception, _exceptionl | | • _matherr, _matherrl | | • HUGE_VAL, HUGE_VALF, HUGE_VALL, _LHUGE_VAL | | • EDOM, ERANGE | | • _mexcep, DOMAIN, SING, OVERFLOW, UNDERFLOW, TLOSS, PLOSS, STACKFAULT | | • M_E, M_LOG2E, M_LOG10E, M_LN2, M_LN10 | | • M_PI, M_PI_2, M_PI_4, M_1_PI, M_2_PI, M_1_SQRTPI, M_2_SQRTPI | | • M_SQRT2, M_SQRT_2 | | • DOMAIN error 定义域错误 | | cstdlib 标准库函数 | | System 字符串 | | System 日期和时间 | | System.Math.hpp 数学函数 | | 其他数据类型 | | VCL 基础类 | | VCL 应用程序 | | Pictures 图片 | | Graphics 绘图 | | Additional 控件 | | System 控件 | | A ~ Z 字母顺序排列的目录 | | 网友留言/技术支持 |
|
| DOMAIN error 定义域错误 |
这是数学函数 cmath 或 math.h 里面的函数的定义域错误。
相关链接:浮点数异常处理
cmath 或 math.h 里面很多函数都有定义域范围,超过范围就无法计算,
例如负数不能求平方根,零和负数不能求对数等,如果函数的参数取了这样的值,就会提示定义域错误,
这样的错误会弹出错误提示窗口,并且无法用 try 来捕获异常,影像程序的正常运行。

正确的捕获异常方法是使用 C 语言函数 _matherr 和 _matherrl
#ifdef __cplusplus
extern "C" {
#endif
int _matherr(struct _exception *e) // 捕获 double 版本的函数异常
{
return 1; // 忽略错误,计算结果为 NAN
}
int _matherrl(struct _exceptionl *e) // 捕获 long double 版本的函数异常
{
return 1; // 忽略错误,计算结果为 NAN
}
#ifdef __cplusplus
} // extern "C"
#endif |
这段代码写在程序里面的任何一个 .c 或 .cpp 里面的任何位置都可以,是整个应用程序的 cmath / math.h 函数异常捕获。
现在再执行这些数学函数的时候,如果参数超过定义域范围,计算结果会等于 NAN。
可以通过 std::_finite 和 std::_finitel 判断计算结果。
相关链接:
• _matherr • _exception • _mexcep • 浮点数异常处理
|
|
|
|