未验证 提交 f246ebba 编写于 作者: N Nyakku Shigure 提交者: GitHub

remove compat.round (#46923)

上级 f4a5fe95
......@@ -13,7 +13,6 @@
# limitations under the License.
import six
import math
__all__ = []
......@@ -197,30 +196,3 @@ def _to_bytes(obj, encoding):
return obj
else:
return six.b(obj)
# math related functions
def round(x, d=0):
"""
Compatible round which act the same behaviour in Python3.
Args:
x(float) : The number to round halfway.
Returns:
round result of x
"""
if six.PY3:
# The official walkaround of round in Python3 is incorrect
# we implement according this answer: https://www.techforgeek.info/round_python.html
if x > 0.0:
p = 10**d
return float(math.floor((x * p) + math.copysign(0.5, x))) / p
elif x < 0.0:
p = 10**d
return float(math.ceil((x * p) + math.copysign(0.5, x))) / p
else:
return math.copysign(0.0, x)
else:
import __builtin__
return __builtin__.round(x, d)
......@@ -236,18 +236,6 @@ class TestCompatible(unittest.TestCase):
for i in l2:
self.assertTrue(isinstance(i, bytes))
def test_round(self):
self.assertEqual(3.0, cpt.round(3.4))
self.assertEqual(4.0, cpt.round(3.5))
self.assertEqual(0.0, cpt.round(0.1))
self.assertEqual(0.0, cpt.round(0.0))
self.assertEqual(-0.0, cpt.round(-0.0))
self.assertEqual(-0.0, cpt.round(-0.1))
self.assertEqual(-3.0, cpt.round(-3.4))
self.assertEqual(-4.0, cpt.round(-3.5))
self.assertEqual(5.0, cpt.round(5))
self.assertRaises(TypeError, cpt.round, None)
if __name__ == "__main__":
unittest.main()
......@@ -17,10 +17,20 @@ import unittest
import numpy as np
import math
import sys
import paddle.compat as cpt
from op_test import OpTest
import paddle.fluid as fluid
from decimal import Decimal, ROUND_HALF_UP
def _round(x):
"""In Python3 round function rounds to the nearest even number,
we use this function to make the result always round up when the
remainder is 0.5. See more at:
https://stackoverflow.com/questions/33019698/how-to-properly-round-up-half-float-numbers
"""
return Decimal(x).to_integral_value(rounding=ROUND_HALF_UP)
class TestROIPoolOp(OpTest):
......@@ -67,10 +77,10 @@ class TestROIPoolOp(OpTest):
for i in range(self.rois_num):
roi = self.rois[i]
roi_batch_id = int(roi[0])
roi_start_w = int(cpt.round(roi[1] * self.spatial_scale))
roi_start_h = int(cpt.round(roi[2] * self.spatial_scale))
roi_end_w = int(cpt.round(roi[3] * self.spatial_scale))
roi_end_h = int(cpt.round(roi[4] * self.spatial_scale))
roi_start_w = int(_round(roi[1] * self.spatial_scale))
roi_start_h = int(_round(roi[2] * self.spatial_scale))
roi_end_w = int(_round(roi[3] * self.spatial_scale))
roi_end_h = int(_round(roi[4] * self.spatial_scale))
roi_height = int(max(roi_end_h - roi_start_h + 1, 1))
roi_width = int(max(roi_end_w - roi_start_w + 1, 1))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册