[issue11949] Make float('nan') unorderable

Alexander Belopolsky report at bugs.python.org
Fri Apr 29 01:44:12 CEST 2011


Alexander Belopolsky <belopolsky at users.sourceforge.net> added the comment:

Actually, my first attempt to fix the test was faulty.  The correct logic seems to be

+def is_negative_zero(x):
+    return x == 0 and math.copysign(1, x) < 0
+
+def almost_equal(value, expected):
+    if math.isfinite(expected) and math.isfinite(value):
+        if is_negative_zero(expected):
+            return is_negative_zero(value)
+        if is_negative_zero(value):
+            return is_negative_zero(expected)
+        return abs(value-expected) <= eps
+    if math.isnan(expected):
+        return math.isnan(value)
+    return value == expected
+
 class MathTests(unittest.TestCase):
+    
+    def test_xxx(self):
+        self.assertTrue(is_negative_zero(-0.0))
+        self.assertFalse(almost_equal(0.0, -0.0))
 
     def ftest(self, name, value, expected):
-        if abs(value-expected) > eps:
+        if not almost_equal(value, expected):

Now, the attached patch has two failures:

AssertionError: fmod(-10,1) returned -0.0, expected 0

and 

AssertionError: sqrt0002:sqrt(-0.0) returned -0.0, expected 0.0

The first seems to be a typo in the test, but I would not expect sqrt(-0.0) to return -0.0.  Does anyone know what the relevant standard says?

----------
Added file: http://bugs.python.org/file21831/unorderable-nans.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11949>
_______________________________________


More information about the Python-bugs-list mailing list