On Wed, 16 Jul 2025 10:32:41 GMT, fabioromano1 <d...@openjdk.org> wrote:

>> src/java.base/share/classes/java/math/BigInteger.java line 2755:
>> 
>>> 2753:      * is even and this BigInteger is negative, an {@code 
>>> ArithmeticException} will be
>>> 2754:      * thrown.
>>> 2755:      *
>> 
>> I think this specification is a bit confusing.
>> For example, when x = -10 and n = 2, then y = -3 has the same sign of x and 
>> its magnitude r = 3 is the largest integer meeting r^n ≤ |x|. However, y is 
>> not a solution. Sure, there's the note about negative x and even n, but that 
>> should emerge as a property from the definition, not as an additional 
>> condition.
>
> I don't know other ways to define it that don't involve the clause `!(x < 0 
> && n % 2 == 0)`. If the first definition is confusing, the only solution I 
> see is to remove it, and leave only the formula `(x.signum() * 
> floor(abs(nthRoot(x, n))))`.

I think this is probably what you are searching for.

If x ≥ 0 there's at most one integer y ≥ 0 meeting y^n ≤ x < (y+1)^n.
If x < 0 there's at most one integer y ≤ 0 meeting y^n ≥ x > (y-1)^n.
The method returns y if it exists, and throws otherwise.
As a consequence, it throws when n ≤ 0, or when x < 0 and n is even, and 
returns a result otherwise.

Can you please check?

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/24898#discussion_r2210634845

Reply via email to