RFR: 8366478: BigDecimal roots [v9]
Raffaello Giulietti
rgiulietti at openjdk.org
Fri Oct 24 14:30:50 UTC 2025
On Fri, 24 Oct 2025 10:35:20 GMT, fabioromano1 <duke at openjdk.org> wrote:
>> An implementation of `BigDecimal.nthRoot(int, MathContext)`.
>
> fabioromano1 has updated the pull request incrementally with one additional commit since the last revision:
>
> Correct call to rootn() in sqrt()
Here's a way to aim at the correctly rounded result, maybe only upon request because of the additional costs.
Let y = x^(1/n) (mathematically exact), yp = rootn(x, n) (approximation), and assume the existence of nextHalfUp(decimal) and nextHalfDown(decimal).
pseudo code for any rounding-to-nearest mode and for n > 0 (monotonically increasing function)
cmp = compare(yp^n, x)
if cmp = 0 then return yp
elsif cmp < 0 then
while nextHalfUp(yp)^n ≤ x do yp := nextUp(yp) od
return yp or nextDown(yp), depending on specific rounding mode
else
while nextHalfDown(vp)^n ≥ x do yp := nextDown(yp) od
return yp or nextUp(yp), depending on specific rounding mode
fi
There are various ways to improve this sketchy code.
Similar corrective actions could be defined for the other rounding modes and for n < 0 (monotonically decreasing function).
@fabioromano1 @jddarcy WDYT?
Is it worth adding correct rounding upon request?
-------------
PR Comment: https://git.openjdk.org/jdk/pull/27148#issuecomment-3443466218
More information about the core-libs-dev
mailing list