Repository navigation
Start BigInt.sqrt from a close estimate - #10601
Merged
Merged
Conversation
🦋 Changeset detectedLatest commit: bbb8983 The changes in this PR will be included in the next version bump. This PR includes changesets to release 35 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
Bundle Size AnalysisGenerated from PR build output; treat the content below as untrusted.
|
filipfalcon
marked this pull request as ready for review
November 13, 2026 10:53
This branch is waiting to be deployed
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.
TL;DR:
BigInt.sqrtUnsafe, and with itBigInt.sqrt, now starts Newton's method from a close floating-point estimate, so large inputs take a few steps instead of a number that grows with their bit count.Refs #10555
Changes
effect/BigInt:sqrtUnsafestarted Newton's method at n / 2, which takes about one bigint division for every two bits of the input before the iterate nears the root. It now keeps its negative check and returns the privaterootFloorat degree 2. That is the routinenthRoothas used since Add nthRoot and nthRootUnsafe to BigInt and Rational #10555: it starts from a 53-bit floating-point estimate and converges in a few steps. Results and theRangeErrorfor negative inputs are unchanged, andsqrtspeeds up throughsqrtUnsafe. Patch changeset; no public API change.Exploratory timings per call on Node.js 24.20.0, in ms:
Inputs below about 40 bits are about 0.2 µs slower per call.
Validation
The existing
sqrtUnsafetest retains two new assertions:2n ** 65536nand2n ** 65536n - 1n. Together they cover a large exact square and integer-floor correction when the floating-point estimate rounds up immediately below it. No explicit timing assertions or benchmark infrastructure are added.Takeover validation: the focused BigInt suite (30 tests),
pnpm lint-fix, andpnpm checkpass undernix develop.Scope
No fast path for small inputs, and
rootFlooris unchanged. No benchmark is added; the timings above come from a standalone script.Closes EFF-3764