[pypy-svn] rev 2500 - in pypy/trunk/src/pypy: module objspace/std objspace/std/test

jacob at codespeak.net jacob at codespeak.net
Thu Dec 18 14:43:40 CET 2003


Author: jacob
Date: Thu Dec 18 14:43:39 2003
New Revision: 2500

Modified:
   pypy/trunk/src/pypy/module/builtin.py
   pypy/trunk/src/pypy/objspace/std/floatobject.py
   pypy/trunk/src/pypy/objspace/std/test/test_floatobject.py
Log:
Implementation and test of round().

Modified: pypy/trunk/src/pypy/module/builtin.py
==============================================================================
--- pypy/trunk/src/pypy/module/builtin.py	(original)
+++ pypy/trunk/src/pypy/module/builtin.py	Thu Dec 18 14:43:39 2003
@@ -287,12 +287,17 @@
     def hex(self, w_val):
         return self.space.hex(w_val)
 
+    def round(self, w_val, w_n=None):
+        if w_n is None:
+            w_n = self.space.wrap(0)
+        return self.space.round(w_val, w_n)
+    
     def id(self, w_object):
         return self.space.id(w_object)
 
-    def app_sum(self,sequence,total=0):
+    def app_sum(self, sequence, total=0):
         for item in sequence:
-            total = total+item
+            total = total + item
         return total
 
     # This function was not in the original builtins,

Modified: pypy/trunk/src/pypy/objspace/std/floatobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/floatobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/floatobject.py	Thu Dec 18 14:43:39 2003
@@ -79,7 +79,40 @@
     i = w_float1.floatval
     j = w_float2.floatval
     return space.newbool( i >= j )
+
+def _floor(f):
+    return f - (f % 1.0)
+
+def _ceil(f):
+    if f - (f % 1.0) == f:
+        return f
+    return f + 1.0 - (f % 1.0)
+
+def round__Float_Int(space, w_float, w_int):
+    x = w_float.floatval
+    ndigits = w_int.intval
     
+    # Algortithm copied directly from CPython
+    f = 1.0;
+    i = abs(ndigits);
+    
+    while  i > 0:
+        f = f*10.0
+        i -= 1
+    if ndigits < 0:
+        x /= f
+    else:
+        x *= f
+    if x >= 0.0:
+        x = _floor(x + 0.5)
+    else:
+        x = _ceil(x - 0.5)
+    if ndigits < 0:
+        x *= f
+    else:
+        x /= f
+    return W_FloatObject(space, x)
+
 def hash__Float(space,w_value):
     ## %reimplement%
     # real Implementation should be taken from _Py_HashDouble in object.c

Modified: pypy/trunk/src/pypy/objspace/std/test/test_floatobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/test/test_floatobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/test/test_floatobject.py	Thu Dec 18 14:43:39 2003
@@ -49,8 +49,20 @@
 
     def test_float_string(self):
         self.assertEquals(42.0, float("42"))
-        
 
+    def test_round(self):
+        self.assertEquals(1.0, round(1.0))
+        self.assertEquals(1.0, round(1.1))
+        self.assertEquals(2.0, round(1.9))
+        self.assertEquals(2.0, round(1.5))
+        self.assertEquals(-2.0, round(-1.5))
+        self.assertEquals(-2.0, round(-1.5))
+        self.assertEquals(-2.0, round(-1.5, 0))
+        self.assertEquals(-2.0, round(-1.5, 0))
+        self.assertEquals(22.2, round(22.222222, 1))
+        self.assertEquals(20.0, round(22.22222, -1))
+        self.assertEquals(0.0, round(22.22222, -2))
+        
 if __name__ == '__main__':
     test.main()
 


More information about the Pypy-commit mailing list