Fix fractional() formatting 0 as "0/1" instead of "0" - #374
Conversation
fractional(0) returned "0/1" because the whole-number branch was gated on `whole_number` being truthy, which excludes 0. Every other integer (1, 2, -2, and so on) already returns a bare number, so 0 was the odd one out. The branch really just needs to check that no fractional part remains (numerator == 0), which holds for any whole number including 0. When the numerator is 0 the fraction is always 0/1, so the denominator == 1 check was redundant and is dropped. Adds test cases for 0, 0.0 and "0". Co-authored-by: eeshsaxena <eeshsaxena@gmail.com>
Mukller
left a comment
There was a problem hiding this comment.
Verified locally on the PR branch (Python 3.13, editable install):
Behavior matrix — release 4.16.0 vs this branch:
| input | release | branch |
|---|---|---|
fractional(0) |
"0/1" |
"0" |
fractional(0.0) |
"0/1" |
"0" |
fractional("0") |
"0/1" |
"0" |
fractional(1.5) |
"1 1/2" |
unchanged |
fractional(2.25) |
"2 1/4" |
unchanged |
fractional(1.0) / (3) |
"1" / "3" |
unchanged |
pytest tests/test_number.py -k fractional — 23/23 green, including the three new parametrized zero cases.
The root cause is real: the old guard if whole_number and not numerator and denominator == 1 treated whole_number=0 as falsy, so an all-zero input fell through to the fraction formatter and rendered as 0/1. The new condition if not numerator covers every integer-valued input including zero, and non-zero cases are provably untouched by the matrix above.
Small and correct. Approving.
|
Duplicate of #351. Check for duplicates before opening PRs, otherwise you're wasting everyone's time. |
This comment was marked as low quality.
This comment was marked as low quality.
And you should read the text you're blindly copy/pasting, this PR was closed 4 days ago. Blocking for continued time wasting. |
Bug
fractional(0)returns"0/1", while every other whole number returns a bare number:Cause
The whole-number branch is gated on
whole_numberbeing truthy:whole_numberis0(falsy) for the value0, so it falls through to theif not whole_number:branch and is rendered asnumerator/denominator=0/1.Fix
The branch only needs to check that no fractional part remains, i.e.
numerator == 0, which is true for any whole number including0. When the numerator is0the fraction is always0/1, so thedenominator == 1check was redundant and is dropped.Verified against all existing
test_fractionalcases (no changes) plus new cases for0,0.0and"0".