非数字(NaN) [英] Not a Number (NaN)

查看:148
本文介绍了非数字(NaN)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在浮点算术中某些数字被定义为非数字(NaN)? (尽管它们可以用IEEE格式表示,并且确实是实数)

Why some numbers are defined as not a number(NaN) in floating point arithmetic? (Although they can be represented by IEEE format and indeed are real numbers)

推荐答案

问题的作者还发布了此评论:

The author of the question also posted this comment:

我不明白所有指数位都是1且尾数不是全零"的数字不是实数.

I don't understand how numbers for which "all exponent bits are 1's and mantissa is not all zeros" is not a real number.

此注释背后的原因似乎是:二进制浮点格式具有符号位 s ,一些指数位 e 和一些有效位 f .对于大多数 e 值,表示的值是(−1) s •1. f •2 e - bias (其中"1. f "是通过将"1."和" f bias 位是指数的编码偏差).通过这种方案,所有指数的取值为一个数字,那么它是一个NaN呢?

The reasoning behind this comment appears to be: A binary floating-point format has a sign bit s, some exponent bits e, and some significand bits f. For most values of e, the value represented is (−1)s•1.f•2e-bias (where "1.f" is the binary numeral formed by concatenating "1." and the bits f and bias is the encoding bias for the exponent). With this scheme, an exponent value of all ones would be a number, so how is it a NaN?

答案是IEEE-754标准指定了浮点编码的位表示什么,并指出:

The answer is that the IEEE-754 standard specifies what the bits of a floating-point encoding represent, and it states that:

  • 如果 e 为零,则表示的值为(−1) s •0. f • 2 e min ,其中 e min 是该格式的最小指数. ( e min 等于1- bias .)
  • 如果 e 不全为零或全为1,则表示的值为(-1) s •1. •2 e - bias .
  • 如果 e 全部为1并且 f 为零,则表示的值为(-1) s •∞.
  • 如果 e 全部为1,并且 f 不为零,则表示的值为NaN. (有关发信号通知NaN与静默NaN以及使用 f 的位来传达附加信息,有一些与实现相关的细节.)
  • If e is zero, the value represented is (−1)s•0.f•2emin, where emin is the minimum exponent for the format. (emin equals 1-bias.)
  • If e is not all zero or all ones, the value represented is (−1)s•1.f•2e-bias.
  • If e is all ones and f is zero, the value represented is (−1)s•∞.
  • If e is all ones and f is not zero, the value represented is a NaN. (There are some implementation-dependent details about signaling NaNs versus quiet NaNs and use of the bits of f for conveying additional information.)

事实上,存在一个为 e 的值表示的值模式,该模式介于(但不包括)全零和全零之间,并不意味着该模式必须扩展到 e 时. em>是全零或全一.关于逻辑或物理学的任何事情都没有强迫我们设计将模式扩展到所有 e 值的硬件.如上所述,制定了规则,IEEE-754浮点的实现遵循这些规则.

The fact there is a pattern of values represented for the values of e between (but not including) all zeros and all ones does not mean the pattern must extended to when e is all zeros or all ones. Nothing about logic or physics compels us to design hardware that extends the pattern to all values of e. The rules were made as described as above, and implementations of IEEE-754 floating-point follow those rules.

此外,上述值构成IEEE-754称为浮点数据的集合.该集合包括-∞和+∞,由 s e f 的值引起的各种非零实数,其中两个零"由符号区分:−0和+0,以及NaN.然后,许多IEEE-754算术运算规范都使用这些值.例如,加法被定义为产生一个结果,就好像计算了精确的数学和,然后对其应用了四舍五入规则.每个舍入规则都指定,如果值超出某些范围,则结果为无穷大.否则,将精确的数学和沿四舍五入规则指定的方向四舍五入到最接近的可表示值(例如,在任一方向上朝+∞,向-∞或朝零方向最近).

Additionally, the values described above form a set that IEEE-754 calls floating-point data. That set includes −∞ and +∞, the various non-zero real numbers arising from values of s, e, and f, two "zeros" distinguished by sign: −0 and +0, and NaN. Much of the IEEE-754 specification of arithmetic operations then uses those values. For example, addition is defined to produce a result as if the exact mathematical sum were calculated and then a rounding rule were applied to it. Each rounding rule specifies that, if the value goes out of certain bounds, the result is an infinity. Otherwise, the exact mathematical sum is rounded to the nearest representable value in a direction specified by the rounding rule (such as nearest in either direction, toward +∞, toward −∞, or toward zero).

因此,当在硬件或软件中实施IEEE-754时,该实施都将遵循这些规则.当规则说要产生无穷大时,实现将产生代表无穷大的位模式.当输入操作数具有无穷大的位模式时,实现将其视为无穷大,而不是如果指数编码的含义扩展了普通数模式的含义,则将其视为实数.

So, when IEEE-754 is implemented, in either hardware or software, the implementation is designed to follow these rules. When the rules say that infinity is to be produced, the implementation produces the bit pattern that represents an infinity. When an input operand has the bit pattern for infinity, the implementation treats it as infinity, not as the real number it would represent if the exponent encoding had a meaning that extended the pattern of normal numbers.

这篇关于非数字(NaN)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆