[pypy-svn] r65620 - in pypy/branch/io-lang/pypy/lang/io: . test
david at codespeak.net
david at codespeak.net
Sat Jun 6 16:51:59 CEST 2009
Author: david
Date: Sat Jun 6 16:51:58 2009
New Revision: 65620
Modified:
pypy/branch/io-lang/pypy/lang/io/model.py
pypy/branch/io-lang/pypy/lang/io/number.py
pypy/branch/io-lang/pypy/lang/io/test/test_number.py
Log:
Added methods asNumber, divide and multiply to Number
Modified: pypy/branch/io-lang/pypy/lang/io/model.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/model.py (original)
+++ pypy/branch/io-lang/pypy/lang/io/model.py Sat Jun 6 16:51:58 2009
@@ -46,13 +46,16 @@
class W_Number(W_Object):
"""Number"""
- def __init__(self, space, value, protos = None):
+ def __init__(self, space, value, protos = None, nan=False, inf=False):
self.value = value
if protos is None:
pp = [space.w_number]
else:
pp = protos
W_Object.__init__(self, space, pp)
+ # TODO: operations on nan and inf instances
+ self.is_nan = nan
+ self.is_inf = inf
def clone(self):
cloned = W_Number(self.space, self.value)
Modified: pypy/branch/io-lang/pypy/lang/io/number.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/number.py (original)
+++ pypy/branch/io-lang/pypy/lang/io/number.py Sat Jun 6 16:51:58 2009
@@ -36,4 +36,25 @@
return W_Number(space, n)
-
+ at register_method('Number', 'asNumber', unwrap_spec=[object])
+def w_number_as_number(space, w_target):
+ return w_target
+
+ at register_method('Number', '/', unwrap_spec=[float, float])
+def w_number_divide(space, dividend, divisor):
+ nan = False
+ inf = False
+ try:
+ value = dividend/float(divisor)
+ except ZeroDivisionError, e:
+ value = 0
+ if dividend == 0:
+ nan = True
+ else:
+ inf = True
+ finally:
+ return W_Number(space, value, nan=nan, inf=inf)
+
+ at register_method('Number', '*', unwrap_spec=[float, float])
+def w_number_multiply(space, a, b):
+ return W_Number(space, a*b)
\ No newline at end of file
Modified: pypy/branch/io-lang/pypy/lang/io/test/test_number.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/test/test_number.py (original)
+++ pypy/branch/io-lang/pypy/lang/io/test/test_number.py Sat Jun 6 16:51:58 2009
@@ -118,4 +118,34 @@
inp = '-3.4 round'
res, space = interpret(inp)
- assert res.value == -3
\ No newline at end of file
+ assert res.value == -3
+
+def test_as_number():
+ inp = '234 asNumber'
+ res, _ = interpret(inp)
+ assert res.value == 234
+
+def test_divide():
+ inp = '3/2'
+ res, _ = interpret(inp)
+ assert res.value == 1.5
+
+def test_division_by_zero():
+ inp = '3/0'
+ res, _ = interpret(inp)
+ assert res.is_nan == False
+ assert res.is_inf == True
+ assert res.value == 0
+
+def test_division_zero_by_zero():
+ inp = '0/0'
+ res, _ = interpret(inp)
+ assert res.is_nan == True
+ assert res.is_inf == False
+ assert res.value == 0
+
+def test_multiply():
+ inp = '6*7'
+ res, _ = interpret(inp)
+ assert res.value == 42
+
\ No newline at end of file
More information about the Pypy-commit
mailing list