【发布时间】:2011-01-14 05:22:32
【问题描述】:
我不确定如何在 C 或 C++ 中处理浮点异常。从 wiki 中,有以下类型的浮点异常:
IEEE 754 specifies five arithmetic errors that are to be recorded in "sticky bits" (by default; note that trapping and other alternatives are optional and, if provided, non-default). * inexact, set if the rounded (and returned) value is different from the mathematically exact result of the operation. * underflow, set if the rounded value is tiny (as specified in IEEE 754) and inexact (or maybe limited to if it has denormalisation loss, as per the 1984 version of IEEE 754), returning a subnormal value (including the zeroes). * overflow, set if the absolute value of the rounded value is too large to be represented (an infinity or maximal finite value is returned, depending on which rounding is used). * divide-by-zero, set if the result is infinite given finite operands (returning an infinity, either +∞ or −∞). * invalid, set if a real-valued result cannot be returned (like for sqrt(−1), or 0/0), returning a quiet NaN.
是不是当出现上述任何一种异常时,程序都会异常退出?或者程序会在不提及任何内容的情况下继续此错误,从而使错误难以调试?
像 gcc 这样的编译器是否能够对一些明显的情况发出警告?
在编写程序时我可以做些什么来通知错误发生的位置以及错误发生时的类型,以便我可以在我的代码中轻松定位错误?请给出 C 和 C++ 两种情况的解决方案。
感谢和问候!
【问题讨论】:
-
答案可能特定于操作系统。你有一个想法吗?
-
linux和windows都有,虽然我现在用的比较多。
-
我看不出 ISO/IEEE 定义的浮点语义将如何依赖于操作系统。
标签: c++ c exception floating-point signals