[pypy-svn] pypy default: format(inf, 'F') == 'INF'

amauryfa commits-noreply at bitbucket.org
Thu Jan 27 19:27:33 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: 
Changeset: r41402:935b6b2afba0
Date: 2011-01-27 17:50 +0100
http://bitbucket.org/pypy/pypy/changeset/935b6b2afba0/

Log:	format(inf, 'F') == 'INF'

diff --git a/pypy/rlib/rdtoa.py b/pypy/rlib/rdtoa.py
--- a/pypy/rlib/rdtoa.py
+++ b/pypy/rlib/rdtoa.py
@@ -58,17 +58,20 @@
     finally:
         lltype.free(end_ptr, flavor='raw')
 
-def format_nonfinite(digits, sign, flags):
+lower_special_strings = ['inf', '+inf', '-inf', 'nan']
+upper_special_strings = ['INF', '+INF', '-INF', 'NAN']
+
+def format_nonfinite(digits, sign, flags, special_strings):
     "Format dtoa's output for nonfinite numbers"
     if digits[0] == 'i' or digits[0] == 'I':
         if sign == 1:
-            return '-inf'
+            return special_strings[2]
         elif flags & rarithmetic.DTSF_SIGN:
-            return '+inf'
+            return special_strings[1]
         else:
-            return 'inf'
+            return special_strings[0]
     elif digits[0] == 'n' or digits[0] == 'N':
-        return 'nan'
+        return special_strings[3]
     else:
         # shouldn't get here
         raise ValueError
@@ -210,7 +213,8 @@
 
     return s
 
-def dtoa(value, code='r', mode=0, precision=0, flags=0):
+def dtoa(value, code='r', mode=0, precision=0, flags=0,
+         special_strings=lower_special_strings):
     decpt_ptr = lltype.malloc(rffi.INTP.TO, 1, flavor='raw')
     try:
         sign_ptr = lltype.malloc(rffi.INTP.TO, 1, flavor='raw')
@@ -229,7 +233,8 @@
 
                     # Handle nan and inf
                     if buflen and not digits[0].isdigit():
-                        return format_nonfinite(digits, sign, flags)
+                        return format_nonfinite(digits, sign, flags,
+                                                special_strings)
 
                     decpt = rffi.cast(lltype.Signed, decpt_ptr[0])
 
@@ -248,6 +253,9 @@
 def dtoa_formatd(value, code, precision, flags):
     if code in 'EFG':
         code = code.lower()
+        special_strings = upper_special_strings
+    else:
+        special_strings = lower_special_strings
 
     if code == 'e':
         mode = 2
@@ -266,4 +274,5 @@
     else:
         raise ValueError('Invalid mode')
 
-    return dtoa(value, code, mode=mode, precision=precision, flags=flags)
+    return dtoa(value, code, mode=mode, precision=precision, flags=flags,
+                special_strings=special_strings)

diff --git a/pypy/objspace/std/test/test_newformat.py b/pypy/objspace/std/test/test_newformat.py
--- a/pypy/objspace/std/test/test_newformat.py
+++ b/pypy/objspace/std/test/test_newformat.py
@@ -307,3 +307,11 @@
     def test_dont_switch_to_g(self):
         skip("must fix when float formatting is figured out")
         assert len(format(1.1234e90, "f")) == 98
+
+    def test_infinite(self):
+        inf = 1e400
+        nan = inf/inf
+        assert format(inf, "f") == "inf"
+        assert format(inf, "F") == "INF"
+        assert format(nan, "f") == "nan"
+        assert format(nan, "F") == "NAN"


More information about the Pypy-commit mailing list