Fix fractional() emitting degenerate output when the fraction rounds to a whole - #354
Fix fractional() emitting degenerate output when the fraction rounds to a whole#354semx wants to merge 1 commit into
Conversation
…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".
|
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. |
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.