Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions guppylang/src/guppylang/std/num.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,13 @@ def __rmod__(self: float, other: float) -> float: ...
@custom_function(checker=ReversingChecker(), unitary_flags=UnitaryFlags.Dagger)
def __rmul__(self: float, other: float) -> float: ...

@hugr_op(float_op("fround"), unitary_flags=UnitaryFlags.Dagger) # TODO
def __round__(self: float) -> float: ...
@hugr_op(float_op("froundeven"), unitary_flags=UnitaryFlags.Dagger) # TODO
def ___round__hugr(self: float) -> float: ...

@guppy
@no_type_check
def __round__(self: float) -> int:
return int(self.___round__hugr())

@custom_function(checker=ReversingChecker(), unitary_flags=UnitaryFlags.Dagger)
def __rpow__(self: float, other: float) -> float: ...
Expand Down
26 changes: 26 additions & 0 deletions tests/integration/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,29 @@ def rem() -> int:

run_int_fn(quot, -1)
run_int_fn(rem, 3)


def test_round(run_int_fn, run_float_fn_approx) -> None:
"""Asserts that the `round` prelude function behaves like a drop-in replacement for
the Python built-in variant."""

@guppy
def int_round() -> int:
return round(1) # noqa: RUF057

@guppy
def nat_round() -> int:
return round(nat(2))

@guppy
def float_round_tie_down() -> int:
return round(2.5)

@guppy
def float_round_tie_up() -> int:
return round(3.5)

run_int_fn(int_round, 1)
run_int_fn(nat_round, 2)
run_int_fn(float_round_tie_down, 2)
run_int_fn(float_round_tie_up, 4)
Loading