RFR: 8366478: BigDecimal roots [v9]

fabioromano1 duke at openjdk.org
Fri Oct 24 16:55:08 UTC 2025


On Fri, 24 Oct 2025 15:36:58 GMT, fabioromano1 <duke at openjdk.org> wrote:

> 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).

Since the case `n > 0` is already correct and optimized, I would use this scheme only for `n < 0`.

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

PR Comment: https://git.openjdk.org/jdk/pull/27148#issuecomment-3444069397


More information about the core-libs-dev mailing list