Fix fractional() formatting 0 as "0/1" instead of "0" - #374
Open
Arunendra21 wants to merge 1 commit into
Open
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>
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.
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".