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

tismer at codespeak.net tismer at codespeak.net
Tue Aug 9 23:56:45 CEST 2005


Author: tismer
Date: Tue Aug  9 23:56:42 2005
New Revision: 15873

Modified:
   pypy/dist/pypy/lib/_float_formatting.py
   pypy/dist/pypy/lib/_formatting.py
Log:
some corrections that make _formatting and _float_formatting
work correctly for marshal.
May be re-enabled when other stuff is ready.

Modified: pypy/dist/pypy/lib/_float_formatting.py
==============================================================================
--- pypy/dist/pypy/lib/_float_formatting.py	(original)
+++ pypy/dist/pypy/lib/_float_formatting.py	Tue Aug  9 23:56:42 2005
@@ -16,6 +16,18 @@
 
 # XXX should run this at interpreter level, really....
 
+def calc_mantissa_bits():
+    bits = 1 # I know it is almost always 53, but let it compute...
+    while 1:
+        pattern = (1L << bits) - 1
+        comp = long(float(pattern))
+        if comp!= pattern:
+            return bits - 1
+        bits += 1
+
+MANTISSA_BITS = calc_mantissa_bits()
+del calc_mantissa_bits
+
 def decode_float(f):
     """decode_float(float) -> int, int
 
@@ -23,8 +35,8 @@
     f (assuming f is an IEEE double), i.e. f == m * 2**e and
     2**52 <= m < 2**53."""
     m, e = math.frexp(f)
-    m = long(m*2.0**53)
-    e -= 53
+    m = long(m*2.0**MANTISSA_BITS)
+    e -= MANTISSA_BITS
     return m, e
 
 def decompose(f):
@@ -38,7 +50,7 @@
     the next smallest."""
     m, e = decode_float(f)
     if e >= 0:
-        if not m != 2**52:
+        if not m != 2**(MANTISSA_BITS-1):
             be = 2**e
             return m*be*2, 2, be, be
         else:
@@ -46,7 +58,7 @@
             be1 = 2*be
             return m*be1*2, 4, be1, be
     else:
-        if e == -1075 or m != 2**52:
+        if e == -1075 or m != 2**(MANTISSA_BITS-1):
             return m*2, 2**(-e+1), 1, 1
         else:
             return m*4, 2**(-e+2), 2, 1

Modified: pypy/dist/pypy/lib/_formatting.py
==============================================================================
--- pypy/dist/pypy/lib/_formatting.py	(original)
+++ pypy/dist/pypy/lib/_formatting.py	Tue Aug  9 23:56:42 2005
@@ -251,7 +251,6 @@
         if v/1e25 > 1e25:
             return FloatGFormatter('g', self.flags, self.width,
                                    self.prec, self.value).format()
-
         return self._formatd('f', v)
         #ds, k = flonum2digits(v)
         #digits = self.fDigits(ds, k)
@@ -259,13 +258,22 @@
         #    digits = digits.rstrip('.')
         #return digits
 
+# system specific formatting. Linux does 3, Windows does 4...
+# XXX this works only when we use geninterp!
+if 0:
+    _x = `1.2e34`
+    _EF = len(_x) - _x.rindex('+')
+    del _x
+else:
+    _EF = 3
+
 
 class FloatEFormatter(FloatFormatter):
     def _format(self, v):
         return self._formatd('e', v)
         #ds, k = flonum2digits(v)
         #digits = self.eDigits(ds)
-        #return "%s%c%+03d"%(digits, self.char, k-1)
+        #return "%%s%%c%%+0%dd" % _EF %(digits, self.char, k-1)
 
 
 class FloatGFormatter(FloatFormatter):
@@ -275,9 +283,12 @@
     # (One has to wonder who might care).
     def _format(self, v):
         return self._formatd('g', v)
+        ## the following is btw. correct for marshal, now:
         #ds, k = flonum2digits(v)
         #ds = ds[:self.prec] # XXX rounding!
         #if -4 < k <= self.prec:
+        #    if k < 0:
+        #        self.prec -= k # grow prec for extra zeros
         #    digits = self.fDigits(ds, k)
         #    if not self.flags.f_alt:
         #        digits = digits.rstrip('0').rstrip('.')
@@ -286,7 +297,7 @@
         #    digits = self.eDigits(ds)
         #    if not self.flags.f_alt:
         #        digits = digits.rstrip('0').rstrip('.')
-        #    r = "%se%+03d"%(digits, k-1)
+        #    r = "%%se%%+0%dd" % _EF %(digits, k-1)
         #return r
 
 



More information about the Pypy-commit mailing list