-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #divmod #3426
Fix #divmod #3426
Conversation
The first element of the tuple should be an integer (quotient rounded down), rather than a float containing the quotient itself.
|
||
assert { 1.4.divmod(0.3)[0].should eq(4) } | ||
assert { 1.4.divmod(0.3)[1].should be_close(0.2, 0.00001) } | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the results this code will get for negative numbers considered? As it stands, it doesn't look like that, considering that the commit message claims the numbers are rounded down (which is not true - they are rounded towards 0) and that there are no tests for the negative case.
As a comparison, Numeric#divmod in Ruby is defined to round towards -infinity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, good point—I did not have that in mind. I’ll add some more test cases and fix the implementation.
@@ -158,7 +158,7 @@ struct Number | |||
# 11.divmod(-3) # => {-3, 2} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interestingly enough, in Ruby this gives:
11.divmod -3
=> [-4, -1]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure Python's behavior completely matches Ruby's behavior. They should be closely followed.
I’ve updated the The behavior of |
That is not true, only the example in the documentation was wrong. The PR doesn't change this behavior.
No, it shouldn't be an actual I completely agree with this pull request now, it doesn't change any behavior that was correct, and only fixes the output for |
Correct, my bad! I’ll update the description. |
@ddfreyne Thank you!! |
Looks like the second example in the documentation was missed: pythondivmod(11, -3) # => (-4, -1) ruby11.divmod(-3) # => [-4, -1] crystal11.divmod(-3) # => {-4, -1} documentation# 11.divmod(-3) # => {-4, 1} |
Two changes:
Before:
After: