Skip to content

Commit

Permalink
[mypyc] Implement float abs primitive (#9695)
Browse files Browse the repository at this point in the history
Related to mypyc/mypyc#644, also part of the plan to speed up rest of 
the slow microbenmark ops

Implement builtins.abs on float.
  • Loading branch information
TH3CHARLie authored Nov 4, 2020
1 parent 34dc670 commit 613c0ce
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions mypyc/primitives/float_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions mypyc/test-data/fixtures/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -251,6 +252,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
Expand Down
8 changes: 8 additions & 0 deletions mypyc/test-data/run-floats.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
def test_abs() -> None:
assert abs(0.0) == 0.0
assert abs(-1.234567) == 1.234567
assert abs(44324.732) == 44324.732
assert abs(-23.4) == 23.4
assert abs(-43.44e-4) == 43.44e-4

0 comments on commit 613c0ce

Please sign in to comment.