Fix fractional() emitting degenerate output when the fraction rounds to a whole#354
Open
semx wants to merge 1 commit into
Open
Fix fractional() emitting degenerate output when the fraction rounds to a whole#354semx wants to merge 1 commit into
semx 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".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.