我不确定如何在C或C++中处理浮点异常。从维基百科得知,有以下几种类型的浮点异常:
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++给出解决方案。
谢谢和问候!