正负南斯 [英] Positive vs negative nans
问题描述
我有一些在AMD64 Linux(使用LLVM 3.2)上开发的数字代码.
I have some numerical code that was developed on AMD64 Linux (using LLVM 3.2).
我最近使用XCode将其移植到OSX 10.9.它运行良好,但是在许多单元测试中均未通过:似乎在Linux上返回NaN(或-NaN)的某些计算现在在OSX上返回-NaN(或NaN).
I have recently ported it to OSX 10.9 with XCode. It runs fine, but it fails a lot of the unit tests: it seems that some calculations which on Linux return NaN (or -NaN) now return, on OSX, -NaN (or NaN).
我可以安全地假设阳性和阴性NaN相等,并调整我的单元测试以使其成功,或者这是更严重的错误迹象吗?
Can I safely assume that positive and negative NaNs are equivalent and adjust my unit tests to accept either as a success, or is this a sign of something more serious going wrong?
推荐答案
在IEEE-754算术中没有负NaN"的概念. NaN编码仍然具有符号位,并且存在使用或影响该位("copysign","abs"和其他一些符号)的符号位"操作的概念,但是当解释NaN编码时,它没有任何意义.作为价值.许多打印例程碰巧将位打印为负号,但在形式上毫无意义,因此标准中没有多少内容可以控制该位的值(除上述功能外).
There is no notion of a "negative NaN" in IEEE-754 arithmetic. The NaN encoding still has a sign bit, and there is a notion of a "sign bit" operation which uses or affects this bit (copysign, abs, a few others), but it does not have any meaning when the NaN encoding is interpreted as a value. Many print routines happen to print the bit as a negative sign, but it is formally meaningless, and therefore there isn't much in the standard to govern what its value should be (except w.r.t. the aforementioned functions).
这是IEEE-754(2008)的相关部分:
Here's the relevant section of IEEE-754 (2008):
以受支持的格式将安静的NaN转换为外部字符序列应产生语言定义的"nan"或除大小写(例如,"NaN")以外的等效序列,并带有可选的前导符号. (此标准不解释NaN的符号.)
Conversion of a quiet NaN in a supported format to an external character sequence shall produce a language-defined one of "nan" or a sequence that is equivalent except for case (e.g., "NaN"), with an optional preceding sign. (This standard does not interpret the sign of a NaN.)
因此,平台的转换功能可能会打印NaN值的符号",但没有意义,因此不应出于测试目的考虑它.
So your platform's conversion functions may print the "sign" of NaN values, but it has no meaning, and you shouldn't consider it for the purposes of testing.
编辑得更强大:将含义附加到NaN数据的符号位"几乎总是一个错误.
Edited to be a bit stronger: it is almost always a bug to attach meaning to the "sign bit" of a NaN datum.
这篇关于正负南斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!