Author: Timo Paulssen <[email protected]>
Branch: py3k-ceil-floor
Changeset: r59062:351124282c1f
Date: 2012-11-23 01:18 +0100
http://bitbucket.org/pypy/pypy/changeset/351124282c1f/
Log: do the same changes with math.floor, too.
there are two commented lines in the test case. the one with
negative values in it would fail, but I don't know why or how.
diff --git a/pypy/module/math/interp_math.py b/pypy/module/math/interp_math.py
--- a/pypy/module/math/interp_math.py
+++ b/pypy/module/math/interp_math.py
@@ -158,14 +158,26 @@
def floor(space, w_x):
"""floor(x)
- Return the floor of x as a float.
+ Return the floor of x as an int.
This is the largest integral value <= x.
"""
w_descr = space.lookup(w_x, '__floor__')
if w_descr is not None:
return space.get_and_call_function(w_descr, w_x)
- x = _get_double(space, w_x)
- return space.wrap(math.floor(x))
+ w_floor_float_result = space.wrap(math.floor(space.float_w(w_x)))
+ return space.call_function(space.w_int, w_floor_float_result)
+
+def ceil(space, w_x):
+ """ceil(x)
+
+ Return the ceiling of x as a float.
+ This is the smallest integral value >= x.
+ """
+ w_descr = space.lookup(w_x, '__ceil__')
+ if w_descr is not None:
+ return space.get_and_call_function(w_descr, w_x)
+ w_ceil_float_result = space.wrap(math.ceil(space.float_w(w_x)))
+ return space.call_function(space.w_int, w_ceil_float_result)
def sqrt(space, w_x):
"""sqrt(x)
@@ -247,18 +259,6 @@
"""
return math1(space, math.atan, w_x)
-def ceil(space, w_x):
- """ceil(x)
-
- Return the ceiling of x as a float.
- This is the smallest integral value >= x.
- """
- w_descr = space.lookup(w_x, '__ceil__')
- if w_descr is not None:
- return space.get_and_call_function(w_descr, w_x)
- w_ceil_float_result = space.wrap(math.ceil(space.float_w(w_x)))
- return space.call_function(space.w_int, w_ceil_float_result)
-
def sinh(space, w_x):
"""sinh(x)
diff --git a/pypy/module/math/test/test_direct.py
b/pypy/module/math/test/test_direct.py
--- a/pypy/module/math/test/test_direct.py
+++ b/pypy/module/math/test/test_direct.py
@@ -21,7 +21,7 @@
unary_math_functions = ['acos', 'asin', 'atan',
- 'cos', 'cosh', 'exp', 'fabs', 'floor',
+ 'cos', 'cosh', 'exp', 'fabs',
'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'log', 'log10',
'acosh', 'asinh', 'atanh', 'log1p', 'expm1']
binary_math_functions = ['atan2', 'fmod', 'hypot', 'pow']
@@ -94,8 +94,6 @@
('exp', (-INFINITY,), 0.0),
('fabs', (INFINITY,), positiveinf),
('fabs', (-INFINITY,), positiveinf),
- ('floor', (INFINITY,), positiveinf),
- ('floor', (-INFINITY,), negativeinf),
('sin', (INFINITY,), ValueError),
('sin', (-INFINITY,), ValueError),
('sinh', (INFINITY,), positiveinf),
diff --git a/pypy/module/math/test/test_math.py
b/pypy/module/math/test/test_math.py
--- a/pypy/module/math/test/test_math.py
+++ b/pypy/module/math/test/test_math.py
@@ -328,3 +328,45 @@
return "this is a string"
assert math.ceil(StrangeCeil()) == "this is a string"
+
+ def test_floor(self):
+ eps = 1E-05
+
+ def almost_equal(a, b):
+ return abs(a-b) <= eps
+ # adapted from the cpython test case
+ import math
+ raises(TypeError, math.floor)
+ assert type(math.floor(0.4)) is int
+ assert almost_equal(math.floor(0.5), 0)
+ assert almost_equal(math.floor(1.0), 1)
+ assert almost_equal(math.floor(1.5), 1)
+ assert almost_equal(math.floor(-0.5), -1)
+ assert almost_equal(math.floor(-1.0), -1)
+ assert almost_equal(math.floor(-1.5), -2)
+ #assert almost_equal(math.floor(1.23e167), 1.23e167)
+ #assert almost_equal(math.floor(-1.23e167), 1.23e167)
+
+ class TestFloor:
+ def __floor__(self):
+ return 42
+ class TestNoFloor:
+ pass
+ assert almost_equal(math.floor(TestFloor()), 42)
+ raises(TypeError, math.floor, TestNoFloor())
+
+ t = TestNoFloor()
+ t.__floor__ = lambda *args: args
+ raises(TypeError, math.floor, t)
+ raises(TypeError, math.floor, t, 0)
+
+ # observed in a cpython interactive shell
+ raises(OverflowError, math.floor, float("inf"))
+ raises(OverflowError, math.floor, float("-inf"))
+ raises(ValueError, math.floor, float("nan"))
+
+ class StrangeCeil:
+ def __floor__(self):
+ return "this is a string"
+
+ assert math.floor(StrangeCeil()) == "this is a string"
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit