Commits

Romain Guillebert committed 8b0616c

numpy.rint(int) should return a float

Comments (0)

Files changed (2)

pypy/module/micronumpy/test/test_ufuncs.py

         assert rint(complex(inf, 1.5)) == complex(inf, 2.)
         assert rint(complex(0.5, inf)) == complex(0., inf)
 
-        assert rint(sys.maxint) == sys.maxint
+        assert rint(sys.maxint) > 0.0
 
     def test_sign(self):
         from numpypy import array, sign, dtype

pypy/module/micronumpy/types.py

         return min(v1, v2)
 
     @simple_unary_op
-    def rint(self, v):
-        if isfinite(float(v)):
-            return rfloat.round_double(float(v), 0, half_even=True)
-        else:
-            return v
-
-    @simple_unary_op
     def ones_like(self, v):
         return 1
 
     def zeros_like(self, v):
         return 0
 
+    @raw_unary_op
+    def rint(self, v):
+        float64 = Float64()
+        return float64.rint(float64.box(v))
 
 class NonNativePrimitive(Primitive):
     _mixin_ = True
         else:
             return v1 + v2
 
+    @simple_unary_op
+    def rint(self, v):
+        x = float(v)
+        if isfinite(x):
+            import math
+            y = math.floor(x)
+            r = x - y
+
+            if r > 0.5:
+                y += 1.0
+
+            if r == 0.5:
+                r = y - 2.0 * math.floor(0.5 * y)
+                if r == 1.0:
+                    y += 1.0
+            return y
+        else:
+            return x
+
 class NonNativeFloat(NonNativePrimitive, Float):
     _mixin_ = True