C语言inf错误是什么,c语言中浮点运算的inf和nan错误

============================================

作者:yuanlulu

http://blog.csdn.net/yuanlulu

版权没有,但是转载请保留此段声明

============================================

自己遇到了浮点运算的错误,打印出来获得的浮点数总是inf、-inf、nan。郁闷了两天,网上搜了一下才搜到正解,gnu就是牛啊。

自己程序中发现使用浮点协处理器优化处理这三个值耗时更长。反而不加浮点优化,处理这三个值更快。

翻译一下第一段:

IEEE 754标准允许浮点数表示正的或者负的无穷大和nan(不是一个数).当计算结果没有定义或者无法表示时就会出现这三个值。某些有用的情况下你也可以故意给浮点数赋予这些值。产生这些值的表达式的例子如下:

http://www.gnu.org/s/libc/manual/html_node/Infinity-and-NaN.html

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 == xis false if the value ofxis NaN. You can use this to test whether a value is NaN or not, but the recommended way to test for NaN is with theisnanfunction (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: float

INFINITY

An expression representing positive infinity. It is equal to the value produced by mathematical operations like1.0 / 0.0.-INFINITYrepresents 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 theisfinitemacro instead. SeeFloating Point Classes.

This macro was introduced in the ISO C99standard.

— Macro: float

NAN

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 withsignbitorcopysign.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值