[pypy-svn] pypy shorter-float-repr: Kill some code that looks similar to parts_to_float()

amauryfa commits-noreply at bitbucket.org
Fri Jan 21 17:28:22 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: shorter-float-repr
Changeset: r41153:0cf0889a128d
Date: 2011-01-21 16:54 +0100
http://bitbucket.org/pypy/pypy/changeset/0cf0889a128d/

Log:	Kill some code that looks similar to parts_to_float()

diff --git a/pypy/rlib/rarithmetic.py b/pypy/rlib/rarithmetic.py
--- a/pypy/rlib/rarithmetic.py
+++ b/pypy/rlib/rarithmetic.py
@@ -573,6 +573,7 @@
 # string -> float helper
 
 def parts_to_float(sign, beforept, afterpt, exponent):
+    "NOT_RPYTHON"
     if not exponent:
         exponent = '0'
     return float("%s%s.%se%s" % (sign, beforept, afterpt, exponent))

diff --git a/pypy/objspace/std/strutil.py b/pypy/objspace/std/strutil.py
--- a/pypy/objspace/std/strutil.py
+++ b/pypy/objspace/std/strutil.py
@@ -189,83 +189,4 @@
     if not digits:
         raise ParseStringError("invalid literal for float()")
 
-    # 2) pre-calculate digit exponent dexp.
-    dexp = len(before_point)
-
-    # 3) truncate and adjust dexp.
-    p = 0
-    plim = dexp + len(after_point)
-    while p < plim and digits[p] == '0':
-        p += 1
-        dexp -= 1
-    digits = digits[p : p + MANTISSA_DIGITS]
-    p = len(digits) - 1
-    while p >= 0 and digits[p] == '0':
-        p -= 1
-    dexp -= p + 1
-    p += 1
-    assert p >= 0
-    digits = digits[:p]
-    if len(digits) == 0:
-        digits = '0'
-
-    # 4) compute the exponent and truncate to +-400
-    if not exponent:
-        exponent = '0'
-    long_exponent = rbigint.fromdecimalstr(exponent)
-    long_exponent = long_exponent.add(rbigint.fromint(dexp))
-    try:
-        e = long_exponent.toint()
-    except OverflowError:
-        # XXX poking at internals
-        e = long_exponent.sign * 400
-    else:
-        if e >= 400:
-            e = 400
-        elif e <= -400:
-            e = -400
-
-    # 5) compute the value using long math and proper rounding.
-    b_digits = rbigint.fromdecimalstr(digits)
-    b_10 = rbigint.fromint(10)
-    b_1 = rbigint.fromint(1)
-    if e >= 0:
-        bits = 0
-        b_power_of_ten = b_10.pow(rbigint.fromint(e))
-        b_mantissa = b_digits.mul(b_power_of_ten)
-    else:
-        # compute a sufficiently large scale
-        prec = MANTISSA_DIGITS * 2 + 22 # 128, maybe
-        bits = - (int(math.ceil(-e / math.log10(2.0) - 1e-10)) + prec)
-        b_scale = b_1.lshift(-bits)
-        b_power_of_ten = b_10.pow(rbigint.fromint(-e))
-        b_mantissa = b_digits.mul(b_scale).div(b_power_of_ten)
-
-    # we now have a fairly large mantissa.
-    # Shift it and round the last bit.
-
-    # first estimate the bits and do a big shift
-    mbits = b_mantissa._count_bits()
-    needed = MANTISSA_BITS
-    if mbits > needed:
-        if mbits > needed+1:
-            shifted = mbits - (needed+1)
-            b_mantissa = b_mantissa.rshift(shifted)
-            bits += shifted
-        # do the rounding
-        bits += 1
-        round = b_mantissa.is_odd()
-        b_mantissa = b_mantissa.rshift(1).add(rbigint.fromint(round))
-
-    try:
-        r = math.ldexp(b_mantissa.tofloat(), bits)
-        # XXX I guess we do not check for overflow in ldexp as we agreed to!
-        if r == 2*r and r != 0.0:
-            raise OverflowError
-    except OverflowError:
-        r = INFINITY
-
-    if sign == '-':
-        r = -r
-
-    return r
+    return parts_to_float(sign, before_point, after_point, exponent)


More information about the Pypy-commit mailing list