Skip to content

Commit 613c0ce

Browse files
authored
[mypyc] Implement float abs primitive (#9695)
Related to mypyc/mypyc#644, also part of the plan to speed up rest of the slow microbenmark ops Implement builtins.abs on float.
1 parent 34dc670 commit 613c0ce

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

mypyc/primitives/float_ops.py

+8
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@
1515
return_type=float_rprimitive,
1616
c_function_name='PyFloat_FromString',
1717
error_kind=ERR_MAGIC)
18+
19+
# abs(float)
20+
c_function_op(
21+
name='builtins.abs',
22+
arg_types=[float_rprimitive],
23+
return_type=float_rprimitive,
24+
c_function_name='PyNumber_Absolute',
25+
error_kind=ERR_MAGIC)

mypyc/test-data/fixtures/ir.py

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def __add__(self, n: float) -> float: pass
8282
def __sub__(self, n: float) -> float: pass
8383
def __mul__(self, n: float) -> float: pass
8484
def __truediv__(self, n: float) -> float: pass
85+
def __neg__(self) -> float: pass
8586

8687
class complex:
8788
def __init__(self, x: object, y: object = None) -> None: pass
@@ -251,6 +252,7 @@ def zip(x: Iterable[T], y: Iterable[S]) -> Iterator[Tuple[T, S]]: ...
251252
@overload
252253
def zip(x: Iterable[T], y: Iterable[S], z: Iterable[V]) -> Iterator[Tuple[T, S, V]]: ...
253254
def eval(e: str) -> Any: ...
255+
def abs(x: float) -> float: ...
254256

255257
# Dummy definitions.
256258
class classmethod: pass

mypyc/test-data/run-floats.test

+8
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,11 @@ assert str_to_float("1.234567") == 1.234567
1010
assert str_to_float("44324") == 44324.0
1111
assert str_to_float("23.4") == 23.4
1212
assert str_to_float("-43.44e-4") == -43.44e-4
13+
14+
[case testFloatAbs]
15+
def test_abs() -> None:
16+
assert abs(0.0) == 0.0
17+
assert abs(-1.234567) == 1.234567
18+
assert abs(44324.732) == 44324.732
19+
assert abs(-23.4) == 23.4
20+
assert abs(-43.44e-4) == 43.44e-4

0 commit comments

Comments
 (0)