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

Return nan when dividing negative number by zero #106

Merged
merged 1 commit into from
Dec 14, 2024
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
3 changes: 1 addition & 2 deletions src/tea_tasting/aggr.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ def with_zero_div(self) -> Aggregates:
"""Return aggregates that do not raise an error on division by zero.

Division by zero returns:
- `nan` if numerator is equal to `0`,
- `inf` if numerator is greater than `0`,
- `-inf` if numerator is less than `0`.
- `nan` if numerator is equal to or less than `0`.
"""
return Aggregates(
count_=None if self.count_ is None else tea_tasting.utils.Int(self.count_),
Expand Down
7 changes: 2 additions & 5 deletions src/tea_tasting/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,17 +370,14 @@ def div(

If `fill_zero_div` is equal `"auto"`, return:

- `nan` if numerator is equal to `0`,
- `inf` if numerator is greater than `0`,
- `-inf` if numerator is less than `0`.
- `nan` if numerator is equal to or less than `0`.
"""
if denom != 0:
return numer / denom
if fill_zero_div != "auto":
return fill_zero_div
if numer == 0:
return float("nan")
return float("inf") if numer > 0 else float("-inf")
return float("inf") if numer > 0 else float("nan")


class _NumericBase:
Expand Down
12 changes: 6 additions & 6 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@ def __init__(self, *args: int) -> None:
def test_div():
assert tea_tasting.utils.div(1, 2) == 0.5
assert tea_tasting.utils.div(1, 0, 3) == 3
assert math.isnan(tea_tasting.utils.div(0, 0))
assert tea_tasting.utils.div(1, 0) == float("inf")
assert tea_tasting.utils.div(-1, 0) == float("-inf")
assert math.isnan(tea_tasting.utils.div(0, 0))
assert math.isnan(tea_tasting.utils.div(-1, 0))


def test_float():
Expand All @@ -263,8 +263,8 @@ def test_float():
assert math.isnan(0 / typ(0))
assert typ(1) / 0 == float("inf")
assert 1 / typ(0) == float("inf")
assert typ(-1) / 0 == float("-inf")
assert -1 / typ(0) == float("-inf")
assert math.isnan(typ(-1) / 0)
assert math.isnan(-1 / typ(0))
assert typ(5) // 2 == typ(2)
assert 5 // typ(2) == typ(2)
assert typ(5) % 2 == typ(1)
Expand Down Expand Up @@ -296,8 +296,8 @@ def test_int():
assert math.isnan(0 / typ(0))
assert typ(1) / 0 == float("inf")
assert 1 / typ(0) == float("inf")
assert typ(-1) / 0 == float("-inf")
assert -1 / typ(0) == float("-inf")
assert math.isnan(typ(-1) / 0)
assert math.isnan(-1 / typ(0))
assert typ(5) // 2 == typ(2)
assert 5 // typ(2) == typ(2)
assert typ(5) % 2 == typ(1)
Expand Down
Loading