RFR: 8366478: BigDecimal roots [v9]
fabioromano1
duke at openjdk.org
Fri Oct 24 15:39:15 UTC 2025
On Fri, 24 Oct 2025 14:28:31 GMT, Raffaello Giulietti <rgiulietti at openjdk.org> wrote:
> 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?
I had the same idea, quite tricky to implement though.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/27148#issuecomment-3443754980
More information about the core-libs-dev
mailing list