Skip to content

Commit

Permalink
[mypyc] Implement str-to-float primitive (#9685)
Browse files Browse the repository at this point in the history
related mypyc/mypyc#644

Implement builtins.float conversion from strings.
  • Loading branch information
TH3CHARLie committed Nov 2, 2020
1 parent c1fa1ad commit 3ed4747
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions mypyc/primitives/float_ops.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions mypyc/primitives/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions mypyc/test-data/run-floats.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[case testStrToFloat]
def str_to_float(x: str) -> float:
return float(x)

[file driver.py]
from native import str_to_float

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
1 change: 1 addition & 0 deletions mypyc/test/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 3ed4747

Please sign in to comment.