20.5.2 Infinity and NaN
20.5.2 Infinity and NaN
IEEE 754 floating point numbers can represent positive or negative infinity, and NaN (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; see FP 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 is unordered: it is not equal to, greater than, or less than anything, including itself. x == x
is false if the value of x
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 the isnan
function (see Floating Point Classes). In addition, <
, >
, <=
, and >=
will raise an exception when applied to NaNs.
math.h defines macros that allow you to explicitly set a variable to infinity or NaN.
An 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. See Floating Point Classes.This macro was introduced in the ISO C99 standard.
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 include math.h.)
IEEE 754 also 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 with signbit
or copysign
.
http://www.linuxtopia.org/online_books/programming_books/gnu_libc_guide/Infinity-and-NaN.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!