[pypy-svn] r14692 - pypy/dist/pypy/lib

mwh at codespeak.net mwh at codespeak.net
Fri Jul 15 12:05:43 CEST 2005


Author: mwh
Date: Fri Jul 15 12:05:42 2005
New Revision: 14692

Modified:
   pypy/dist/pypy/lib/_formatting.py
Log:
grotesque code for testing for nans and infs (no tests, but a quick check
on windows both with 2.3 and 2.4 would be much appreciated).

use this to print nans and infs as "nan" and "inf", respectively.


Modified: pypy/dist/pypy/lib/_formatting.py
==============================================================================
--- pypy/dist/pypy/lib/_formatting.py	(original)
+++ pypy/dist/pypy/lib/_formatting.py	Fri Jul 15 12:05:42 2005
@@ -193,6 +193,16 @@
     return floater()
 
 
+# isinf isn't too hard...
+def isinf(v):
+    return v != 0 and v*2.0 == v
+
+# To get isnan, working x-platform and both on 2.3 and 2.4, is a
+# horror.  I think this works (for reasons I don't really want to talk
+# about), and probably when implemented on top of pypy, too.
+def isnan(v):
+    return v != v*1.0 or (v == 1.0 and v == 2.0)
+
 from _float_formatting import flonum2digits
 
 class FloatFormatter(Formatter):
@@ -221,6 +231,10 @@
 
     def format(self):
         v = maybe_float(self.value)
+        if isnan(v):
+            return 'nan'
+        elif isinf(v):
+            return 'inf'
         v, sign = self.numeric_preprocess(v)
         if self.prec is None:
             self.prec = 6



More information about the Pypy-commit mailing list