c語言中浮點運算的inf和nan錯誤
============================================
作者:yuanlulu
http://blog.csdn.net/yuanlulu
版權沒有,但是轉載請保留此段宣告
============================================
自己遇到了浮點運算的錯誤,打印出來獲得的浮點數總是inf、-inf、nan。鬱悶了兩天,網上搜了一下才搜到正解,gnu就是牛啊。
自己程式中發現使用浮點協處理器優化處理這三個值耗時更長。反而不加浮點優化,處理這三個值更快。
翻譯一下第一段:
IEEE 754標準允許浮點數表示正的或者負的無窮大和nan(不是一個數).當計算結果沒有定義或者無法表示時就會出現這三個值。某些有用的情況下你也可以故意給浮點數賦予這些值。產生這些值的表示式的例子如下:
20.5.2 Infinity and NaN
IEEE 754floating point numbers can represent positive or negative infinity, andNaN(not a number). These three values arise from calculations whose result is undefined or cannot be represented accurately. You can also deliberately set a floating-point variable to any of them, which is sometimes useful. Some examples of calculations that produce infinity or NaN:
1/0 = ∞ log (0) = -∞ sqrt (-1) = NaN
When a calculation produces any of these values, an exception also occurs; seeFP Exceptions.
The basic operations and math functions all accept infinity and NaN and produce sensible output. Infinities propagate through calculations as one would expect: for example, 2 + ∞ = ∞, 4/∞ = 0, atan (∞) = π/2. NaN, on the other hand, infects any calculation that involves it. Unless the calculation would produce the same result no matter what real value replaced NaN, the result is NaN.
In comparison operations, positive infinity is larger than all values except itself and NaN, and negative infinity is smaller than all values except itself and NaN. NaN isunordered: it is not equal to, greater than, or less than anything,including
itself.x == x
is false if the value ofx
is NaN. You can use this to test whether a value is NaN or not, but the recommended way to test for NaN is with theisnan
function
(seeFloating Point Classes). In addition,<
,>
,<=
, and>=
will
raise an exception when applied to NaNs.
math.hdefines macros that allow you to explicitly set a variable to infinity or NaN.
— Macro: floatINFINITY— Macro: floatNANAn expression representing positive infinity. It is equal to the value produced by mathematical operations like
1.0 / 0.0
.-INFINITY
represents negative infinity.You can test whether a floating-point value is infinite by comparing it to this macro. However, this is not recommended; you should use the
isfinite
macro instead. SeeFloating Point Classes.This macro was introduced in the ISO C99standard.
An expression representing a value which is “not a number”. This macro is a GNU extension, available only on machines that support the “not a number” value—that is to say, on all machines that support IEEE floating point.
You can use ‘#ifdef NAN’ to test whether the machine supports NaN. (Of course, you must arrange for GNU extensions to be visible, such as by defining
_GNU_SOURCE
, and then you must includemath.h.)
IEEE 754also allows for another unusual value: negative zero. This value is produced when you divide a positive number by negative infinity, or when a negative result is smaller than the limits of representation. Negative zero behaves identically
to zero in all calculations, unless you explicitly test the sign bit withsignbit
orcopysign
.