Fix fractional() emitting degenerate output when the fraction rounds to a whole - #354
Conversation
|
Gentle ping. This fixes |
|
Ran this on Windows 11, CPython 3.14.7, fresh clone. On main ( On this branch ( Applying the new test rows to main's One cross-reference for the maintainers: this change also covers what #351 and #374 fix ( Limits: I tested on 3.14.7, not the full CI matrix, and didn't run lint/mypy. |
|
Thanks for the careful cross-check @MohammedAlkindi, that's really helpful. Your numbers match what I see here too: on Good call on the overlap with #351 and #374 as well: both only address the @hugovk it's a clean cherry-pick onto current |
0fe540d to
1b879cc
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #354 +/- ##
=======================================
Coverage 99.56% 99.56%
=======================================
Files 12 12
Lines 916 916
=======================================
Hits 912 912
Misses 4 4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…to a whole When limit_denominator(1000) reduces the fractional part to a whole number (denominator == 1), fold it into the integer part instead of printing a degenerate "N/1". For example fractional(2.9999999) returned "2 1/1" instead of "3", fractional(0.9999999) returned "1/1" instead of "1", and fractional(0) returned "0/1" instead of "0".
7ea0b1f to
828dd24
Compare
|
Thanks @hugovk! And a nod once more to @Sreekant13, whose #351 spotted the fractional(0) half of this first. |
Noticed while probing edge cases:
fractional()emits degenerate output when the fractional part rounds to a whole number.Cause
After
frac = Fraction(number - whole_number).limit_denominator(1000), when the fractional part reduces to a whole number the denominator is1(numerator ∈ {-1, 0, 1}). The existing special case only handlednumerator == 0(a plain integer input like1.0), so:numerator == 1(the fraction rounded up to1/1) fell through to the mixed-fraction branch →"2 1/1";fractional(0)produced"0/1".Changes proposed in this pull request:
denominator == 1) into the integer part, so the result reads as a normal integer.0,0.0,2.9999999,0.9999999,-2.9999999.All existing tests pass (
705 passed);ruffandblackclean.