[pypy-svn] r39680 - in pypy/dist/pypy: rlib rlib/test rpython/test
arigo at codespeak.net
arigo at codespeak.net
Fri Mar 2 15:10:44 CET 2007
Author: arigo
Date: Fri Mar 2 15:10:42 2007
New Revision: 39680
Modified:
pypy/dist/pypy/rlib/rarithmetic.py
pypy/dist/pypy/rlib/test/test_rarithmetic.py
pypy/dist/pypy/rpython/test/test_rfloat.py
Log:
Support for direct casts between float and r_uint.
Actually worked already during translation, but not on top of CPython.
Modified: pypy/dist/pypy/rlib/rarithmetic.py
==============================================================================
--- pypy/dist/pypy/rlib/rarithmetic.py (original)
+++ pypy/dist/pypy/rlib/rarithmetic.py Fri Mar 2 15:10:42 2007
@@ -267,6 +267,8 @@
class signed_int(base_int):
SIGNED = True
def __new__(klass, val=0):
+ if type(val) is float:
+ val = long(val)
if val > klass.MASK>>1 or val < -(klass.MASK>>1)-1:
raise OverflowError("%s does not fit in signed %d-bit integer"%(val, klass.BITS))
if val < 0:
@@ -277,6 +279,8 @@
class unsigned_int(base_int):
SIGNED = False
def __new__(klass, val=0):
+ if type(val) is float:
+ val = long(val)
return super(unsigned_int, klass).__new__(klass, val & klass.MASK)
typemap = {}
Modified: pypy/dist/pypy/rlib/test/test_rarithmetic.py
==============================================================================
--- pypy/dist/pypy/rlib/test/test_rarithmetic.py (original)
+++ pypy/dist/pypy/rlib/test/test_rarithmetic.py Fri Mar 2 15:10:42 2007
@@ -139,6 +139,15 @@
res = res & mask
assert res == cmp
+ def test_from_float(self):
+ assert r_uint(2.3) == 2
+ assert r_uint(sys.maxint * 1.234) == long(sys.maxint * 1.234)
+
+ def test_to_float(self):
+ assert float(r_uint(2)) == 2.0
+ val = long(sys.maxint * 1.234)
+ assert float(r_uint(val)) == float(val)
+
def test_mixed_types():
types = [r_uint, r_ulonglong]
for left in types:
Modified: pypy/dist/pypy/rpython/test/test_rfloat.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rfloat.py (original)
+++ pypy/dist/pypy/rpython/test/test_rfloat.py Fri Mar 2 15:10:42 2007
@@ -1,6 +1,8 @@
+import sys
from pypy.translator.translator import TranslationContext
from pypy.rpython.test import snippet
from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
+from pypy.rlib.rarithmetic import r_uint
class TestSnippet(object):
@@ -58,6 +60,25 @@
res = self.interpret(fn, [2.34])
assert res == fn(2.34)
+ def test_to_r_uint(self):
+ def fn(x):
+ return r_uint(x)
+
+ res = self.interpret(fn, [12.34])
+ assert res == 12
+ bigval = sys.maxint * 1.234
+ res = self.interpret(fn, [bigval])
+ assert long(res) == long(bigval)
+
+ def test_from_r_uint(self):
+ def fn(n):
+ return float(r_uint(n)) / 2
+
+ res = self.interpret(fn, [41])
+ assert res == 20.5
+ res = self.interpret(fn, [-9])
+ assert res == 0.5 * ((sys.maxint+1)*2 - 9)
+
class TestLLtype(BaseTestRfloat, LLRtypeMixin):
More information about the Pypy-commit
mailing list