Skip to content

Implement Integer.sqrt for large integers #3872

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tompng
Copy link

@tompng tompng commented May 17, 2025

Implement Integer.sqrt algorithm used in CRuby (Newton's method)
ruby/ruby#10274

This spec now passes

Integer.sqrt(10**400).should == 10**200

Calculation

(x + n / x) / 2.0 is always greater or equal to square_root(n) if there is no calculation error.
Integer arithmetic (x + n / x) / 2 is greater than square_root(n) OR equal to square_root(n).floor
After x -= 1 while x*x > n, x will be the expected return value of Integer.sqrt(n)
Newton's method ensures this while loop is executed only a few times. Experimentally, it's executed only once.

Implement Integer.sqrt algorithm used in CRuby (Newton's method)
ruby/ruby#10274
Copy link

Thank you for your pull request and welcome to our community! To contribute, please sign the Oracle Contributor Agreement (OCA).
The following contributors of this PR have not signed the OCA:

To sign the OCA, please create an Oracle account and sign the OCA in Oracle's Contributor Agreement Application.

When signing the OCA, please provide your GitHub username. After signing the OCA and getting an OCA approval from Oracle, this PR will be automatically updated.

If you are an Oracle employee, please make sure that you are a member of the main Oracle GitHub organization, and your membership in this organization is public.

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Required At least one contributor does not have an approved Oracle Contributor Agreement. label May 17, 2025
Copy link

Thank you for signing the OCA.

@oracle-contributor-agreement oracle-contributor-agreement bot added OCA Verified All contributors have signed the Oracle Contributor Agreement. and removed OCA Required At least one contributor does not have an approved Oracle Contributor Agreement. labels May 19, 2025
@eregon eregon requested a review from andrykonchin May 19, 2025 14:24
@eregon
Copy link
Member

eregon commented May 19, 2025

Very nice, thank you for porting this optimization and correctness fix to TruffleRuby!
Ruby code looks a lot cleaner for this compared to C code :)

@eregon
Copy link
Member

eregon commented May 19, 2025

@andrykonchin Could you integrate this and look if there are some CRuby tests for this and we could remove excludes for them?
Also please add a CHANGELOG entry.

xx -= 2 * x - 1
x -= 1
end
x
Copy link
Member

Choose a reason for hiding this comment

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

Wondering how the constant 0xfffffffffffff was chosen. Is it based on benchmarking results?

Copy link
Author

Choose a reason for hiding this comment

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

Larger is better as long as Math.sqrt(n).floor returns correct value.
0xfffffffffffff is 52bit integer and double has 53bit precision.

If n is larger than 0xfffffffffffff == 0x4000000**2-1, Math.sqrt(n).floor might not return correct value.
Integer.sqrt(0x4000001**2-1) should return 0x4000001 - 1
Math.sqrt(0x4000001**2-1).floor is 0x4000001

@andrykonchin andrykonchin self-assigned this May 20, 2025
@andrykonchin andrykonchin added the in-ci The PR is being tested in CI. Do not push new commits. label May 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in-ci The PR is being tested in CI. Do not push new commits. OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants