Fix ordinal() suffix for negative numbers - #409
Closed
yu2971512385-ui wants to merge 1 commit into
Closed
yu2971512385-ui wants to merge 1 commit into
yu2971512385-ui wants to merge 1 commit into
Conversation
Python's % on a negative operand returns the remainder of the wrong digit (-1 % 10 == 9), so every negative value picked the "th" suffix: ordinal(-1) returned "-1th" and ordinal(-3) returned "-3th". Take the last digits of the magnitude instead, which keeps the 11/12/13 exception working for negative values too.
Member
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.
Problem
ordinal()gives every negative number thethsuffix:The docstring says the function "works for any integer or anything
int()will turn into an integer", and negative integers already reach the suffix table — they just index it with the wrong digit.Root cause
Python's
%returns a non-negative remainder for a negative left operand, and that remainder belongs to a different digit:-1 % 10 == 9,-3 % 10 == 7,-21 % 10 == 9. Every one of those lands on athentry, so the bug is invisible except that the answer is alwaysth.Fix
Index the table with the last digits of the magnitude. The 11/12/13 exception keeps working because it is applied to the same magnitude:
Positive values, non-numeric input and the non-finite handling are untouched.
This matches how the module already treats signs elsewhere —
intword(-1500000)→'-1.5 million'andfractional(-1.3)→'-1 3/10'both keep the sign on the number and format the magnitude.Tests
Nine negative cases added to the existing
test_ordinalparametrisation, covering the plain digits, the 11/12/13 exception and a value past 100. Verified they fail before the change (4 failed, 246 passed) and pass after (250 passed).