From b663b434de7eaf8cdc1d24f7e212dbb889e2015b Mon Sep 17 00:00:00 2001 From: Xuanda Yang Date: Mon, 2 Nov 2020 02:25:57 +0800 Subject: [PATCH 1/5] str-to-float primitive --- mypyc/primitives/float_ops.py | 17 +++++++++++++++++ mypyc/primitives/registry.py | 1 + 2 files changed, 18 insertions(+) create mode 100644 mypyc/primitives/float_ops.py diff --git a/mypyc/primitives/float_ops.py b/mypyc/primitives/float_ops.py new file mode 100644 index 0000000000000..17bbdfbfe69e8 --- /dev/null +++ b/mypyc/primitives/float_ops.py @@ -0,0 +1,17 @@ +"""Primitive float ops.""" + +from mypyc.ir.ops import ERR_MAGIC +from mypyc.ir.rtypes import ( + str_rprimitive, float_rprimitive +) +from mypyc.primitives.registry import ( + c_function_op +) + +# float(str) +c_function_op( + name='builtins.float', + arg_types=[str_rprimitive], + return_type=float_rprimitive, + c_function_name='PyFloat_FromString', + error_kind=ERR_MAGIC) diff --git a/mypyc/primitives/registry.py b/mypyc/primitives/registry.py index 454e7f1f6db4c..1503341ecb865 100644 --- a/mypyc/primitives/registry.py +++ b/mypyc/primitives/registry.py @@ -345,3 +345,4 @@ def load_address_op(name: str, import mypyc.primitives.dict_ops # noqa import mypyc.primitives.tuple_ops # noqa import mypyc.primitives.misc_ops # noqa +import mypyc.primitives.float_ops # noqa From d11eb1243c91fca86d76b3032a76e724fc950e6e Mon Sep 17 00:00:00 2001 From: Xuanda Yang Date: Tue, 3 Nov 2020 02:46:03 +0800 Subject: [PATCH 2/5] add testcase --- mypyc/test-data/fixtures/testutil.py | 3 +++ mypyc/test-data/run-floats.test | 14 ++++++++++++++ mypyc/test/test_run.py | 1 + 3 files changed, 18 insertions(+) create mode 100644 mypyc/test-data/run-floats.test diff --git a/mypyc/test-data/fixtures/testutil.py b/mypyc/test-data/fixtures/testutil.py index ad53e474c8bf5..c2a332357ff90 100644 --- a/mypyc/test-data/fixtures/testutil.py +++ b/mypyc/test-data/fixtures/testutil.py @@ -37,3 +37,6 @@ def run_generator(gen: Generator[T, V, U], print(val) res.append(val) i += 1 + +def is_close(x: float, y: float) -> bool: + return 0.999999 <= x / y <= 1.000001 diff --git a/mypyc/test-data/run-floats.test b/mypyc/test-data/run-floats.test new file mode 100644 index 0000000000000..af75dacea8216 --- /dev/null +++ b/mypyc/test-data/run-floats.test @@ -0,0 +1,14 @@ +[case testStrToFloat] +from typing import Any +def str_to_float(x: str) -> float: + return float(x) + +[file driver.py] +from native import str_to_float +from testutil import is_close + +assert is_close(str_to_float("1"), 1.0) +assert is_close(str_to_float("1.234567"), 1.234567) +assert is_close(str_to_float("44324"), 44324.0) +assert is_close(str_to_float("23.4"), 23.4) +assert is_close(str_to_float("-43.44e-4"), -43.44e-4) diff --git a/mypyc/test/test_run.py b/mypyc/test/test_run.py index 82a288e0d293b..938bdeb7c9950 100644 --- a/mypyc/test/test_run.py +++ b/mypyc/test/test_run.py @@ -33,6 +33,7 @@ 'run-misc.test', 'run-functions.test', 'run-integers.test', + 'run-floats.test', 'run-bools.test', 'run-strings.test', 'run-tuples.test', From c74b2922443dcd55dc43f65a639b9d217955b61b Mon Sep 17 00:00:00 2001 From: Xuanda Yang Date: Tue, 3 Nov 2020 03:31:31 +0800 Subject: [PATCH 3/5] try fix --- mypyc/test-data/fixtures/testutil.py | 2 +- mypyc/test-data/run-floats.test | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/mypyc/test-data/fixtures/testutil.py b/mypyc/test-data/fixtures/testutil.py index c2a332357ff90..afce959ff82be 100644 --- a/mypyc/test-data/fixtures/testutil.py +++ b/mypyc/test-data/fixtures/testutil.py @@ -39,4 +39,4 @@ def run_generator(gen: Generator[T, V, U], i += 1 def is_close(x: float, y: float) -> bool: - return 0.999999 <= x / y <= 1.000001 + return x / y >= 0.999999 and x / y <= 1.000001 diff --git a/mypyc/test-data/run-floats.test b/mypyc/test-data/run-floats.test index af75dacea8216..072aabba0ba74 100644 --- a/mypyc/test-data/run-floats.test +++ b/mypyc/test-data/run-floats.test @@ -1,5 +1,4 @@ [case testStrToFloat] -from typing import Any def str_to_float(x: str) -> float: return float(x) From d0a2a600743390307cb32d3487c984de0a7b2a27 Mon Sep 17 00:00:00 2001 From: Xuanda Yang Date: Tue, 3 Nov 2020 03:50:15 +0800 Subject: [PATCH 4/5] try fix --- mypyc/test-data/fixtures/testutil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypyc/test-data/fixtures/testutil.py b/mypyc/test-data/fixtures/testutil.py index afce959ff82be..5d52b816b7d46 100644 --- a/mypyc/test-data/fixtures/testutil.py +++ b/mypyc/test-data/fixtures/testutil.py @@ -39,4 +39,4 @@ def run_generator(gen: Generator[T, V, U], i += 1 def is_close(x: float, y: float) -> bool: - return x / y >= 0.999999 and x / y <= 1.000001 + return x / y > 0.999999 and x / y < 1.000001 From 6ca0a07a7af2d89c05175f7857f8bb76f9c4b961 Mon Sep 17 00:00:00 2001 From: Xuanda Yang Date: Tue, 3 Nov 2020 04:14:42 +0800 Subject: [PATCH 5/5] use assert instead --- mypyc/test-data/fixtures/testutil.py | 3 --- mypyc/test-data/run-floats.test | 11 +++++------ 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/mypyc/test-data/fixtures/testutil.py b/mypyc/test-data/fixtures/testutil.py index 5d52b816b7d46..ad53e474c8bf5 100644 --- a/mypyc/test-data/fixtures/testutil.py +++ b/mypyc/test-data/fixtures/testutil.py @@ -37,6 +37,3 @@ def run_generator(gen: Generator[T, V, U], print(val) res.append(val) i += 1 - -def is_close(x: float, y: float) -> bool: - return x / y > 0.999999 and x / y < 1.000001 diff --git a/mypyc/test-data/run-floats.test b/mypyc/test-data/run-floats.test index 072aabba0ba74..e2ab4b2288610 100644 --- a/mypyc/test-data/run-floats.test +++ b/mypyc/test-data/run-floats.test @@ -4,10 +4,9 @@ def str_to_float(x: str) -> float: [file driver.py] from native import str_to_float -from testutil import is_close -assert is_close(str_to_float("1"), 1.0) -assert is_close(str_to_float("1.234567"), 1.234567) -assert is_close(str_to_float("44324"), 44324.0) -assert is_close(str_to_float("23.4"), 23.4) -assert is_close(str_to_float("-43.44e-4"), -43.44e-4) +assert str_to_float("1") == 1.0 +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