-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mypyc] Implement str-to-float primitive (#9685)
related mypyc/mypyc#644 Implement builtins.float conversion from strings.
- Loading branch information
1 parent
c1fa1ad
commit 3ed4747
Showing
4 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters