From 8c8bcb4e6bf8aefb801d9a4e7fd5005b13fd8d87 Mon Sep 17 00:00:00 2001 From: Xuanda Yang Date: Tue, 3 Nov 2020 19:57:36 +0800 Subject: [PATCH 1/4] float abs primitive --- mypyc/primitives/float_ops.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mypyc/primitives/float_ops.py b/mypyc/primitives/float_ops.py index 17bbdfbfe69e..fa5bbb018688 100644 --- a/mypyc/primitives/float_ops.py +++ b/mypyc/primitives/float_ops.py @@ -15,3 +15,11 @@ return_type=float_rprimitive, c_function_name='PyFloat_FromString', error_kind=ERR_MAGIC) + +# abs(float) +c_function_op( + name='builtins.abs', + arg_types=[float_rprimitive], + return_type=float_rprimitive, + c_function_name='PyNumber_Absolute', + error_kind=ERR_MAGIC) From ea8ad137e19cf12ccade788654047d509b7e84ee Mon Sep 17 00:00:00 2001 From: Xuanda Yang Date: Wed, 4 Nov 2020 23:04:21 +0800 Subject: [PATCH 2/4] add testcase --- mypyc/test-data/fixtures/ir.py | 1 + mypyc/test-data/run-floats.test | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/mypyc/test-data/fixtures/ir.py b/mypyc/test-data/fixtures/ir.py index 4ffefb7432de..1b8604b0b832 100644 --- a/mypyc/test-data/fixtures/ir.py +++ b/mypyc/test-data/fixtures/ir.py @@ -251,6 +251,7 @@ def zip(x: Iterable[T], y: Iterable[S]) -> Iterator[Tuple[T, S]]: ... @overload def zip(x: Iterable[T], y: Iterable[S], z: Iterable[V]) -> Iterator[Tuple[T, S, V]]: ... def eval(e: str) -> Any: ... +def abs(x: float) -> float: ... # Dummy definitions. class classmethod: pass diff --git a/mypyc/test-data/run-floats.test b/mypyc/test-data/run-floats.test index e2ab4b228861..5823dfd770a2 100644 --- a/mypyc/test-data/run-floats.test +++ b/mypyc/test-data/run-floats.test @@ -10,3 +10,11 @@ assert str_to_float("1.234567") == 1.234567 assert str_to_float("44324") == 44324.0 assert str_to_float("23.4") == 23.4 assert str_to_float("-43.44e-4") == -43.44e-4 + +[case testFloatAbs] +[file driver.py] +assert abs(0) == 0.0 +assert abs(-1.234567) == 1.234567 +assert abs(44324) == 44324.0 +assert abs(-23.4) == 23.4 +assert abs(-43.44e-4) == 43.44e-4 From 32f408d7eb7502d47f548118e0988303a066ed3f Mon Sep 17 00:00:00 2001 From: Xuanda Yang Date: Wed, 4 Nov 2020 23:50:28 +0800 Subject: [PATCH 3/4] make sure testcase is compiled --- mypyc/test-data/fixtures/ir.py | 1 + mypyc/test-data/run-floats.test | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/mypyc/test-data/fixtures/ir.py b/mypyc/test-data/fixtures/ir.py index 1b8604b0b832..da8e6986ce05 100644 --- a/mypyc/test-data/fixtures/ir.py +++ b/mypyc/test-data/fixtures/ir.py @@ -82,6 +82,7 @@ def __add__(self, n: float) -> float: pass def __sub__(self, n: float) -> float: pass def __mul__(self, n: float) -> float: pass def __truediv__(self, n: float) -> float: pass + def __neg__(self) -> float: pass class complex: def __init__(self, x: object, y: object = None) -> None: pass diff --git a/mypyc/test-data/run-floats.test b/mypyc/test-data/run-floats.test index 5823dfd770a2..e7adb0bd4011 100644 --- a/mypyc/test-data/run-floats.test +++ b/mypyc/test-data/run-floats.test @@ -12,9 +12,9 @@ assert str_to_float("23.4") == 23.4 assert str_to_float("-43.44e-4") == -43.44e-4 [case testFloatAbs] -[file driver.py] -assert abs(0) == 0.0 -assert abs(-1.234567) == 1.234567 -assert abs(44324) == 44324.0 -assert abs(-23.4) == 23.4 -assert abs(-43.44e-4) == 43.44e-4 +def test_abs() -> None: + assert abs(0.0) == 0.0 + assert abs(-1.234567) == 1.234567 + assert abs(44324) == 44324.0 + assert abs(-23.4) == 23.4 + assert abs(-43.44e-4) == 43.44e-4 From 72bb9bd6936687102f9d3790718d246d79582bbd Mon Sep 17 00:00:00 2001 From: Xuanda Yang Date: Thu, 5 Nov 2020 00:53:51 +0800 Subject: [PATCH 4/4] include a positive case in test --- mypyc/test-data/run-floats.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypyc/test-data/run-floats.test b/mypyc/test-data/run-floats.test index e7adb0bd4011..e716949d69c4 100644 --- a/mypyc/test-data/run-floats.test +++ b/mypyc/test-data/run-floats.test @@ -15,6 +15,6 @@ assert str_to_float("-43.44e-4") == -43.44e-4 def test_abs() -> None: assert abs(0.0) == 0.0 assert abs(-1.234567) == 1.234567 - assert abs(44324) == 44324.0 + assert abs(44324.732) == 44324.732 assert abs(-23.4) == 23.4 assert abs(-43.44e-4) == 43.44e-4