NaN!= NaN的直观原因是什么? [英] What is the intuitive reason that NaN != NaN?

查看:97
本文介绍了NaN!= NaN的直观原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的发现,IEEE定义NaN在浮点数系统中不等于自身.我唯一一次必须将NaN与NaN进行比较是在比较数据帧条目时,在这种情况下,NaN的此属性实际上是一个很大的麻烦.

From what I have been able to find, the IEEE defines NaN to not equal itself in the floating point number system. The only time I've ever had to compare NaN to NaN is in comparing dataframe entries, in which case this property of NaN is actually a big nuisance.

只是想知道为什么要做出使NaN不等于自身的决定,这与其他Null值不同. (例如,像无",例如在python中.)

Was just wondering why the decision was made to make NaN not equal itself, unlike other Null values. (Like None, in python, for example.)

推荐答案

IEEE 754定义了浮点算术系统.它的相等运算符测试两个数字是否相等. (这不同于测试两个对象是否相同.)NaN表示不是数字的内容.两个NaN可以相同,但不能是相等的数字,因为它们不是数字.

IEEE 754 defines a system of floating-point arithmetic. Its equality operator tests whether two numbers are equal. (This is different from testing whether two objects are identical.) NaN represents something that is Not A Number. Two NaNs can be identical, but they cannot be numbers that are equal because they are not numbers.

重要的一个原因是,NaN用于传达操作已误入歧途的信息.例如,假设已经使用浮点运算计算了两个数字xy.但是,进一步假设由于舍入误差,在计算x时的某个时候,理想情况下原本应为小正值的值为小负值,然后取其平方根.结果是NaN.同样,在计算y时,理想情况下该值应小于1,而取其反正弦值时则稍大于1,结果再次为NaN.

One reason this is important is that NaN is used to convey the information that an operation has gone astray. For example, suppose two numbers x and y had been calculated with floating-point operations. However, further suppose that, due to rounding errors, at some point when calculating x, a value that ideally would have been a small positive value was instead a small negative value, and then its square root was taken. The result is a NaN. Similarly, while calculating y, a value that ideally would have been less than one was slightly greater than one when its arcsine was taken, and the result is again a NaN.

现在xy均为NaN.如果x == y返回true,则表示两个计算值相等.但是,实际上,我们不知道要计算的值是否相等.所以这不是我们想要的结果.

Now x and y are both NaN. If x == y returned true, it would be indicating that the two computed values are equal. But, in fact, we have no idea whether the values intended to be computed are equal. So this is not the result we want.

类似地,如果当xy之一或两者均为NaN时为x < yx > y分配结果,则浮点算法将得到错误的结果.如果一个操作数是NaN,那么我们想要的答案是"xy在哪个顺序?"是我们不知道."因此,当两个操作数均为NaN时,x == yx < yx > y都将返回false,因此在设计浮点算法时必须牢记这一点.

Similarly, if we assigned results for x < y or x > y when one or both of x or y was a NaN, then floating-point algorithms would get wrong results. If one operand is a NaN, then the answer we want to "Which order are x and y in?" Is "We do not know." So x == y, x < y, and x > y all return false when either operand is a NaN, and floating-point algorithms must be designed with this in mind.

创建NaN的一个原因是,当发生错误时,浮点运算不必停止.可以继续执行非常长的浮点算法,处理许多数字,然后程序可以在最后检查结果以查看是否存在NaN.然后,程序将以适合该特定程序的任何方式处理这些程序. (通常,对于浮点算法中的问题,没有唯一的解决方案.因此,IEEE 754旨在为实现提供一些灵活性.程序可以根据需要修正结果或转移其操作.)

One reason that NaNs were created was so that floating-point operations do not have to stop when an error occurs. A very lengthy floating-point algorithm working with many numbers can continue executing, and then the program can examine the results at the end to see if there are any NaNs. Then it is up to the program to handle those in whatever way is suitable for that particular program. (In general, there is not a single solution for what to do about problems in floating-point algorithms. So IEEE 754 was designed to give implementations some flexibility. Programs can fix up results or divert their operations as needed.)

提供IEEE-754操作的平台通常应该既提供数字相等性的比较(用于测试算术属性),又提供排序顺序和标识的比较(用于在将对象插入排序列表或其他结构(如字典)时使用)

Platforms that provide IEEE-754 operations generally ought to provide both a comparison for numeric equality (for testing arithmetic properties) and a comparison for sort order and identity (for use when inserting objects into sorted lists or other structures such as dictionaries).

这篇关于NaN!= NaN的直观原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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