Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mypyc] Implement float abs primitive #9695

Merged
merged 4 commits into from Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions mypyc/primitives/float_ops.py
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
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
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) == 44324.0
TH3CHARLie marked this conversation as resolved.
Show resolved Hide resolved
assert abs(-23.4) == 23.4
assert abs(-43.44e-4) == 43.44e-4