[pypy-svn] r50673 - in pypy/dist/pypy/module/struct: . test

fijal at codespeak.net fijal at codespeak.net
Wed Jan 16 15:26:49 CET 2008


Author: fijal
Date: Wed Jan 16 15:26:48 2008
New Revision: 50673

Modified:
   pypy/dist/pypy/module/struct/ieee.py
   pypy/dist/pypy/module/struct/test/test_ieee.py
Log:
(arigo, fijal) Fix this also for single precision floats


Modified: pypy/dist/pypy/module/struct/ieee.py
==============================================================================
--- pypy/dist/pypy/module/struct/ieee.py	(original)
+++ pypy/dist/pypy/module/struct/ieee.py	Wed Jan 16 15:26:48 2008
@@ -20,7 +20,7 @@
 
     if isnan(number):
         sign = 0x80
-        man, e = 1.5, 1024
+        man, e = 1.5, bias + 1
     else:
         if number < 0:
             sign = 0x80
@@ -32,7 +32,7 @@
         else:
             sign = 0x00
         if isinf(number):
-            man, e = 1.0, 1024
+            man, e = 1.0, bias + 1
         else:
             man, e = math.frexp(number)
 
@@ -98,7 +98,7 @@
     e -= bias
     e += 1
     sign = bytes[-1] & 0x80
-    if e == 1025:
+    if e == bias + 2:
         if mantissa == 0.5:
             number = INFINITY
         else:

Modified: pypy/dist/pypy/module/struct/test/test_ieee.py
==============================================================================
--- pypy/dist/pypy/module/struct/test/test_ieee.py	(original)
+++ pypy/dist/pypy/module/struct/test/test_ieee.py	Wed Jan 16 15:26:48 2008
@@ -10,6 +10,9 @@
     (2.0 ** 100, 8, False, '\x00\x00\x00\x00\x00\x000F'),
     (0.0, 8, True, '\x00\x00\x00\x00\x00\x00\x00\x00'),
     (-123456789, 8, True, '\xc1\x9do4T\x00\x00\x00'),
+    (1e200*1e200, 4, False, '\x00\x00\x80\x7f'),
+    (-1e200*1e200, 4, False, '\x00\x00\x80\xff'),
+    ((1e200*1e200)/(1e200*1e200), 4, False, '\x00\x00\xc0\xff'),
     (1e200*1e200, 8, False, '\x00\x00\x00\x00\x00\x00\xf0\x7f'),
     (-1e200*1e200, 8, False, '\x00\x00\x00\x00\x00\x00\xf0\xff'),
     ((1e200*1e200)/(1e200*1e200), 8, False, '\x00\x00\x00\x00\x00\x00\xf8\xff')
@@ -27,10 +30,10 @@
             fmt += 'f'
         else:
             fmt += 'd'
+        assert struct.pack(fmt, number) == expected
         res, = struct.unpack(fmt, expected)
         assert (isnan(res) and isnan(number)) or \
                 res == number or abs(res - number) < 1E-6
-        assert struct.pack(fmt, number) == expected
 
 def test_pack():
     for number, size, bigendian, expected in testcases:
@@ -51,7 +54,7 @@
             if size == 8:
                 assert res == expected    # exact result expected
             else:
-                assert abs(res - expected) < 1E-6
+                assert res == expected or abs(res - expected) < 1E-6
 
 
 def test_llinterpreted():



More information about the Pypy-commit mailing list