-
Notifications
You must be signed in to change notification settings - Fork 188
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
Implement integer modular exponentiation using BigInteger#mod_pow
#2006
Conversation
Hello Dávid Halász, thanks for contributing a PR to our project! We use the Oracle Contributor Agreement to make the copyright of contributions clear. We don't have a record of you having signed this yet, based on your email address skateman -(at)- skateman -(dot)- eu. You can sign it at that link. If you think you've already signed it, please comment below and we'll check. |
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.
Could you check if this passes the specs?
Run them with:
jt test spec/ruby/core/integer/pow_spec.rb
It would be good to add specs for the new exception case that was missing, and other combinations of pow
arguments with "Bignums".
Regarding performance, it would be interesting to run this benchmark on MRI using |
And I should have said this first: thanks for the PRs! |
Dávid Halász has signed the Oracle Contributor Agreement (based on email address skateman -(at)- skateman -(dot)- eu) so can contribute to this repository. |
@eregon I am unable to build the latest master, so I can't really rebase against it to fix the merge conflict. I implemented the # TruffleRuby with modPow
3.pow(2, -14)
=> ZeroDivisionError
3 ** 2 % -14
=> -5
# MRI
3.pow(2, -14)
=> -5
3 ** 2 % -14
=> -5 How should I handle this corner-case? Should I create a specialization that falls back to ``** %`? |
The "Conflicting files: CHANGELOG.md" shown on GitHub is actually not a conflict, it's a bug in GitHub UI not considering Line 3 in 3fb1262
What's the issue? It might be easier to discuss on Slack for that. Regarding the negative modulo you can look at what JRuby does: |
31b0595
to
9793dd0
Compare
src/main/java/org/truffleruby/core/cast/BigIntegerCastNode.java
Outdated
Show resolved
Hide resolved
@eregon I addressed all the issues. |
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.
Thanks! I'll merge this.
As @eregon suggested here I replaced the modular exponentiation in
Integer#pow
with the more effectiveBigInteger#mod_pow
using a primitive.I also found out that the
Integer#pow
wasn't raising aTypeError
when the 1st argument was not an integer, which results with the following in MRI:Note that this message is not grammatically correct, but I copy-pasted it from MRI. If it's not good, please let me know how it should look like.
After this change the 2nd example from here runs faster (
2.934s
for me), but still significantly slower than in MRI. However, the profiling shows that the bottleneck is no longer thepow
:Fixes #1999