Author: Romain Guillebert <[email protected]>
Branch:
Changeset: r61496:974e7fe3c1d5
Date: 2013-02-20 16:31 +0100
http://bitbucket.org/pypy/pypy/changeset/974e7fe3c1d5/
Log: merge heads
diff --git a/pypy/objspace/std/floatobject.py b/pypy/objspace/std/floatobject.py
--- a/pypy/objspace/std/floatobject.py
+++ b/pypy/objspace/std/floatobject.py
@@ -10,7 +10,7 @@
from rpython.rlib.rarithmetic import ovfcheck_float_to_int, intmask, LONG_BIT
from rpython.rlib.rfloat import (
isinf, isnan, isfinite, INFINITY, NAN, copysign, formatd,
- DTSF_ADD_DOT_0, DTSF_STR_PRECISION)
+ DTSF_ADD_DOT_0, DTSF_STR_PRECISION, float_as_rbigint_ratio)
from rpython.rlib.rbigint import rbigint
from rpython.rlib import rfloat
from rpython.tool.sourcetools import func_with_new_name
@@ -553,27 +553,18 @@
def float_as_integer_ratio__Float(space, w_float):
value = w_float.floatval
- if isinf(value):
+ try:
+ num, den = float_as_rbigint_ratio(value)
+ except OverflowError:
w_msg = space.wrap("cannot pass infinity to as_integer_ratio()")
raise OperationError(space.w_OverflowError, w_msg)
- elif isnan(value):
+ except ValueError:
w_msg = space.wrap("cannot pass nan to as_integer_ratio()")
raise OperationError(space.w_ValueError, w_msg)
- float_part, exp = math.frexp(value)
- for i in range(300):
- if float_part == math.floor(float_part):
- break
- float_part *= 2.0
- exp -= 1
- w_num = W_LongObject.fromfloat(space, float_part)
- w_den = space.newlong(1)
- w_exp = space.newlong(abs(exp))
- w_exp = space.lshift(w_den, w_exp)
- if exp > 0:
- w_num = space.mul(w_num, w_exp)
- else:
- w_den = w_exp
- # Try to return int.
+
+ w_num = space.newlong_from_rbigint(num)
+ w_den = space.newlong_from_rbigint(den)
+ # Try to return int
return space.newtuple([space.int(w_num), space.int(w_den)])
def float_is_integer__Float(space, w_float):
diff --git a/rpython/rlib/rfloat.py b/rpython/rlib/rfloat.py
--- a/rpython/rlib/rfloat.py
+++ b/rpython/rlib/rfloat.py
@@ -419,3 +419,25 @@
def isfinite(x):
"NOT_RPYTHON"
return not isinf(x) and not isnan(x)
+
+def float_as_rbigint_ratio(value):
+ from rpython.rlib.rbigint import rbigint
+
+ if isinf(value):
+ raise OverflowError("cannot pass infinity to as_integer_ratio()")
+ elif isnan(value):
+ raise ValueError("cannot pass nan to as_integer_ratio()")
+ float_part, exp_int = math.frexp(value)
+ for i in range(300):
+ if float_part == math.floor(float_part):
+ break
+ float_part *= 2.0
+ exp_int -= 1
+ num = rbigint.fromfloat(float_part)
+ den = rbigint.fromint(1)
+ exp = den.lshift(abs(exp_int))
+ if exp_int > 0:
+ num = num.mul(exp)
+ else:
+ den = exp
+ return num, den
diff --git a/rpython/rlib/test/test_rfloat.py b/rpython/rlib/test/test_rfloat.py
new file mode 100644
--- /dev/null
+++ b/rpython/rlib/test/test_rfloat.py
@@ -0,0 +1,131 @@
+import sys, py
+
+from rpython.rlib.rfloat import float_as_rbigint_ratio
+from rpython.rlib.rfloat import break_up_float
+from rpython.rlib.rfloat import copysign
+from rpython.rlib.rfloat import round_away
+from rpython.rlib.rfloat import round_double
+from rpython.rlib.rbigint import rbigint
+
+def test_copysign():
+ assert copysign(1, 1) == 1
+ assert copysign(-1, 1) == 1
+ assert copysign(-1, -1) == -1
+ assert copysign(1, -1) == -1
+ assert copysign(1, -0.) == -1
+
+def test_round_away():
+ assert round_away(.1) == 0.
+ assert round_away(.5) == 1.
+ assert round_away(.7) == 1.
+ assert round_away(1.) == 1.
+ assert round_away(-.5) == -1.
+ assert round_away(-.1) == 0.
+ assert round_away(-.7) == -1.
+ assert round_away(0.) == 0.
+
+def test_round_double():
+ def almost_equal(x, y):
+ assert round(abs(x-y), 7) == 0
+
+ almost_equal(round_double(0.125, 2), 0.13)
+ almost_equal(round_double(0.375, 2), 0.38)
+ almost_equal(round_double(0.625, 2), 0.63)
+ almost_equal(round_double(0.875, 2), 0.88)
+ almost_equal(round_double(-0.125, 2), -0.13)
+ almost_equal(round_double(-0.375, 2), -0.38)
+ almost_equal(round_double(-0.625, 2), -0.63)
+ almost_equal(round_double(-0.875, 2), -0.88)
+
+ almost_equal(round_double(0.25, 1), 0.3)
+ almost_equal(round_double(0.75, 1), 0.8)
+ almost_equal(round_double(-0.25, 1), -0.3)
+ almost_equal(round_double(-0.75, 1), -0.8)
+
+ round_double(-6.5, 0) == -7.0
+ round_double(-5.5, 0) == -6.0
+ round_double(-1.5, 0) == -2.0
+ round_double(-0.5, 0) == -1.0
+ round_double(0.5, 0) == 1.0
+ round_double(1.5, 0) == 2.0
+ round_double(2.5, 0) == 3.0
+ round_double(3.5, 0) == 4.0
+ round_double(4.5, 0) == 5.0
+ round_double(5.5, 0) == 6.0
+ round_double(6.5, 0) == 7.0
+
+ round_double(-25.0, -1) == -30.0
+ round_double(-15.0, -1) == -20.0
+ round_double(-5.0, -1) == -10.0
+ round_double(5.0, -1) == 10.0
+ round_double(15.0, -1) == 20.0
+ round_double(25.0, -1) == 30.0
+ round_double(35.0, -1) == 40.0
+ round_double(45.0, -1) == 50.0
+ round_double(55.0, -1) == 60.0
+ round_double(65.0, -1) == 70.0
+ round_double(75.0, -1) == 80.0
+ round_double(85.0, -1) == 90.0
+ round_double(95.0, -1) == 100.0
+ round_double(12325.0, -1) == 12330.0
+
+ round_double(350.0, -2) == 400.0
+ round_double(450.0, -2) == 500.0
+
+ almost_equal(round_double(0.5e21, -21), 1e21)
+ almost_equal(round_double(1.5e21, -21), 2e21)
+ almost_equal(round_double(2.5e21, -21), 3e21)
+ almost_equal(round_double(5.5e21, -21), 6e21)
+ almost_equal(round_double(8.5e21, -21), 9e21)
+
+ almost_equal(round_double(-1.5e22, -22), -2e22)
+ almost_equal(round_double(-0.5e22, -22), -1e22)
+ almost_equal(round_double(0.5e22, -22), 1e22)
+ almost_equal(round_double(1.5e22, -22), 2e22)
+
+def test_round_half_even():
+ from rpython.rlib import rfloat
+ for func in (rfloat.round_double_short_repr,
+ rfloat.round_double_fallback_repr):
+ # 2.x behavior
+ assert func(2.5, 0, False) == 3.0
+ # 3.x behavior
+ assert func(2.5, 0, True) == 2.0
+
+def test_break_up_float():
+ assert break_up_float('1') == ('', '1', '', '')
+ assert break_up_float('+1') == ('+', '1', '', '')
+ assert break_up_float('-1') == ('-', '1', '', '')
+
+ assert break_up_float('.5') == ('', '', '5', '')
+
+ assert break_up_float('1.2e3') == ('', '1', '2', '3')
+ assert break_up_float('1.2e+3') == ('', '1', '2', '+3')
+ assert break_up_float('1.2e-3') == ('', '1', '2', '-3')
+
+ # some that will get thrown out on return:
+ assert break_up_float('.') == ('', '', '', '')
+ assert break_up_float('+') == ('+', '', '', '')
+ assert break_up_float('-') == ('-', '', '', '')
+ assert break_up_float('e1') == ('', '', '', '1')
+
+ py.test.raises(ValueError, break_up_float, 'e')
+
+
+def test_float_as_rbigint_ratio():
+ for f, ratio in [
+ (0.875, (7, 8)),
+ (-0.875, (-7, 8)),
+ (0.0, (0, 1)),
+ (11.5, (23, 2)),
+ ]:
+ num, den = float_as_rbigint_ratio(f)
+ assert num.eq(rbigint.fromint(ratio[0]))
+ assert den.eq(rbigint.fromint(ratio[1]))
+
+ with py.test.raises(OverflowError):
+ float_as_rbigint_ratio(float('inf'))
+ with py.test.raises(OverflowError):
+ float_as_rbigint_ratio(float('-inf'))
+ with py.test.raises(ValueError):
+ float_as_rbigint_ratio(float('nan'))
diff --git a/rpython/rtyper/test/test_rfloat.py
b/rpython/rtyper/test/test_rfloat.py
--- a/rpython/rtyper/test/test_rfloat.py
+++ b/rpython/rtyper/test/test_rfloat.py
@@ -216,26 +216,6 @@
# https://bugzilla.novell.com/show_bug.cgi?id=692493
assert not self.interpret(fn, [1e200, 1e200]) # nan
- def test_break_up_float(self):
- from rpython.rlib.rfloat import break_up_float
- assert break_up_float('1') == ('', '1', '', '')
- assert break_up_float('+1') == ('+', '1', '', '')
- assert break_up_float('-1') == ('-', '1', '', '')
-
- assert break_up_float('.5') == ('', '', '5', '')
-
- assert break_up_float('1.2e3') == ('', '1', '2', '3')
- assert break_up_float('1.2e+3') == ('', '1', '2', '+3')
- assert break_up_float('1.2e-3') == ('', '1', '2', '-3')
-
- # some that will get thrown out on return:
- assert break_up_float('.') == ('', '', '', '')
- assert break_up_float('+') == ('+', '', '', '')
- assert break_up_float('-') == ('-', '', '', '')
- assert break_up_float('e1') == ('', '', '', '1')
-
- py.test.raises(ValueError, break_up_float, 'e')
-
def test_formatd(self):
from rpython.rlib.rfloat import formatd
def f(x):
@@ -296,93 +276,6 @@
assert self.interpret(func, [0]) == 1e23
assert self.interpret(func, [1]) == -1e23
- def test_copysign(self):
- from rpython.rlib.rfloat import copysign
- assert copysign(1, 1) == 1
- assert copysign(-1, 1) == 1
- assert copysign(-1, -1) == -1
- assert copysign(1, -1) == -1
- assert copysign(1, -0.) == -1
-
- def test_round_away(self):
- from rpython.rlib.rfloat import round_away
- assert round_away(.1) == 0.
- assert round_away(.5) == 1.
- assert round_away(.7) == 1.
- assert round_away(1.) == 1.
- assert round_away(-.5) == -1.
- assert round_away(-.1) == 0.
- assert round_away(-.7) == -1.
- assert round_away(0.) == 0.
-
- def test_round_double(self):
- from rpython.rlib.rfloat import round_double
- def almost_equal(x, y):
- assert round(abs(x-y), 7) == 0
-
- almost_equal(round_double(0.125, 2), 0.13)
- almost_equal(round_double(0.375, 2), 0.38)
- almost_equal(round_double(0.625, 2), 0.63)
- almost_equal(round_double(0.875, 2), 0.88)
- almost_equal(round_double(-0.125, 2), -0.13)
- almost_equal(round_double(-0.375, 2), -0.38)
- almost_equal(round_double(-0.625, 2), -0.63)
- almost_equal(round_double(-0.875, 2), -0.88)
-
- almost_equal(round_double(0.25, 1), 0.3)
- almost_equal(round_double(0.75, 1), 0.8)
- almost_equal(round_double(-0.25, 1), -0.3)
- almost_equal(round_double(-0.75, 1), -0.8)
-
- round_double(-6.5, 0) == -7.0
- round_double(-5.5, 0) == -6.0
- round_double(-1.5, 0) == -2.0
- round_double(-0.5, 0) == -1.0
- round_double(0.5, 0) == 1.0
- round_double(1.5, 0) == 2.0
- round_double(2.5, 0) == 3.0
- round_double(3.5, 0) == 4.0
- round_double(4.5, 0) == 5.0
- round_double(5.5, 0) == 6.0
- round_double(6.5, 0) == 7.0
-
- round_double(-25.0, -1) == -30.0
- round_double(-15.0, -1) == -20.0
- round_double(-5.0, -1) == -10.0
- round_double(5.0, -1) == 10.0
- round_double(15.0, -1) == 20.0
- round_double(25.0, -1) == 30.0
- round_double(35.0, -1) == 40.0
- round_double(45.0, -1) == 50.0
- round_double(55.0, -1) == 60.0
- round_double(65.0, -1) == 70.0
- round_double(75.0, -1) == 80.0
- round_double(85.0, -1) == 90.0
- round_double(95.0, -1) == 100.0
- round_double(12325.0, -1) == 12330.0
-
- round_double(350.0, -2) == 400.0
- round_double(450.0, -2) == 500.0
-
- almost_equal(round_double(0.5e21, -21), 1e21)
- almost_equal(round_double(1.5e21, -21), 2e21)
- almost_equal(round_double(2.5e21, -21), 3e21)
- almost_equal(round_double(5.5e21, -21), 6e21)
- almost_equal(round_double(8.5e21, -21), 9e21)
-
- almost_equal(round_double(-1.5e22, -22), -2e22)
- almost_equal(round_double(-0.5e22, -22), -1e22)
- almost_equal(round_double(0.5e22, -22), 1e22)
- almost_equal(round_double(1.5e22, -22), 2e22)
-
- def test_round_half_even(self):
- from rpython.rlib import rfloat
- for func in (rfloat.round_double_short_repr,
- rfloat.round_double_fallback_repr):
- # 2.x behavior
- assert func(2.5, 0, False) == 3.0
- # 3.x behavior
- assert func(2.5, 0, True) == 2.0
class TestLLtype(BaseTestRfloat, LLRtypeMixin):
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit