Skip to content
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 BigInt#% for unsigned integers #12773

Merged

Conversation

straight-shoota
Copy link
Member

Fixes #12770

@jzakiya
Copy link

jzakiya commented Nov 21, 2022

Thank you!

@asterite
Copy link
Member

Thank you! There's another such occurrence in the // method.

@straight-shoota
Copy link
Member Author

That's uncritical because unsigned types won't reach this def (there's a //(other : Int::Unsigned) overload).

@straight-shoota
Copy link
Member Author

But there is indeed a common pattern of this form:

if other < 0
  (-self).unsafe_op(-other)
else
  unsafe_op(other)
end

It would be more concise to express this as something like (other.sign * self).unsafe_op(other.abs).
Performance should probably be similar in release mode.

Copy link
Contributor

@HertzDevil HertzDevil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still breaks if other is e.g. Int32::MIN, but we can worry about that later because it's not just this method

@straight-shoota straight-shoota added this to the 1.7.0 milestone Dec 2, 2022
@straight-shoota straight-shoota merged commit 5a33430 into crystal-lang:master Dec 4, 2022
@straight-shoota straight-shoota deleted the fix/bigint-mod branch December 4, 2022 22:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BigInt#% does not work with unsigned integers
4 participants