Skip to content

Commit 3ed4747

Browse files
authored
[mypyc] Implement str-to-float primitive (#9685)
related mypyc/mypyc#644 Implement builtins.float conversion from strings.
1 parent c1fa1ad commit 3ed4747

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

mypyc/primitives/float_ops.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Primitive float ops."""
2+
3+
from mypyc.ir.ops import ERR_MAGIC
4+
from mypyc.ir.rtypes import (
5+
str_rprimitive, float_rprimitive
6+
)
7+
from mypyc.primitives.registry import (
8+
c_function_op
9+
)
10+
11+
# float(str)
12+
c_function_op(
13+
name='builtins.float',
14+
arg_types=[str_rprimitive],
15+
return_type=float_rprimitive,
16+
c_function_name='PyFloat_FromString',
17+
error_kind=ERR_MAGIC)

mypyc/primitives/registry.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,4 @@ def load_address_op(name: str,
345345
import mypyc.primitives.dict_ops # noqa
346346
import mypyc.primitives.tuple_ops # noqa
347347
import mypyc.primitives.misc_ops # noqa
348+
import mypyc.primitives.float_ops # noqa

mypyc/test-data/run-floats.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[case testStrToFloat]
2+
def str_to_float(x: str) -> float:
3+
return float(x)
4+
5+
[file driver.py]
6+
from native import str_to_float
7+
8+
assert str_to_float("1") == 1.0
9+
assert str_to_float("1.234567") == 1.234567
10+
assert str_to_float("44324") == 44324.0
11+
assert str_to_float("23.4") == 23.4
12+
assert str_to_float("-43.44e-4") == -43.44e-4

mypyc/test/test_run.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
'run-misc.test',
3434
'run-functions.test',
3535
'run-integers.test',
36+
'run-floats.test',
3637
'run-bools.test',
3738
'run-strings.test',
3839
'run-tuples.test',

0 commit comments

Comments
 (0)