RFR: 8077587: BigInteger Roots [v20]

Hans Boehm hboehm at google.com
Mon Jul 14 16:20:35 UTC 2025


On Fri, Jul 11, 2025 at 11:44 AM fabioromano1 <duke at openjdk.org> wrote:
>
> On Fri, 11 Jul 2025 18:07:31 GMT, fabioromano1 <duke at openjdk.org> wrote:
>
> >> This PR implements nth root computation for BigIntegers using Newton
method.
> >
> > fabioromano1 has updated the pull request incrementally with one
additional commit since the last revision:
> >
> >   Avoid to discard fraction bits in the initial estimate
>
> The current explanation for the exception of negative `n` is justified by
consistency with the explanation for negative `exponent` in
`BigInteger.pow(int)`, so changing this explanation would cause an
inconsistency in the documentation of `BigInteger` operations.
>
But the mathematics are completely different. The comment makes sense for
pow(), which naturally produces integers for non-negative arguments.

It makes no sense at all for nthRoot, which almost never naturally produces
an integer. We make it produce an integer via suitable truncation, as the
spec says. We could clearly do the same thing for negative n, though I tend
to agree we probably don't want to.

Let's please not fake consistency here where there inherently is none. the
integers are closed under positive powers, and not under positive nthRoots.
We can't change that.

Hans
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20250714/9ef8b7b3/attachment.htm>


More information about the core-libs-dev mailing list