Stop printing two minus signs in fractional for a negative mixed number#320
Open
c-tonneslan wants to merge 1 commit into
Open
Stop printing two minus signs in fractional for a negative mixed number#320c-tonneslan wants to merge 1 commit into
c-tonneslan wants to merge 1 commit into
Conversation
int() truncates toward zero, so int(-1.3) is -1 and (-1.3) - (-1) is -0.3, which Fraction turns into -3/10. Both the whole-number part and the numerator end up signed and the output reads as "-1 -3/10". The minus sign already rides on the whole-number part; absorb it from the numerator so the result is the conventional "-1 3/10". Added a few negative cases to test_fractional. Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #320 +/- ##
=======================================
Coverage 99.55% 99.55%
=======================================
Files 12 12
Lines 898 900 +2
=======================================
+ Hits 894 896 +2
Misses 4 4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
`fractional(-1.3)` currently returns `'-1 -3/10'`. Same shape for any negative number whose absolute value is at least 1: `fractional(-2.5)` returns `'-2 -1/2'`, etc.
`int()` truncates toward zero, so `int(-1.3)` is `-1` and `-1.3 - (-1)` is `-0.3`, which `Fraction` keeps as `-3/10`. Both the whole-number part and the numerator end up signed and the format string prints them both verbatim.
The minus sign already rides on the whole-number part, so absorb it from the numerator with `abs()`. The result is the conventional mixed-fraction form `'-1 3/10'` (= -1.3).
Added negative-input cases to `test_fractional`.