RFR: 8341402: BigDecimal's square root optimization
fabioromano1
duke at openjdk.org
Wed Oct 2 14:02:37 UTC 2024
On Wed, 2 Oct 2024 13:49:29 GMT, Raffaello Giulietti <rgiulietti at openjdk.org> wrote:
>> After changing `BigInteger.sqrt()` algorithm, this can be also used to speed up `BigDecimal.sqrt()` implementation. Here is how I made it.
>>
>> The main steps of the algorithm are as follows:
>> first argument reduce the value to an integer using the following relations:
>>
>> x = y * 10 ^ exp
>> sqrt(x) = sqrt(y) * 10^(exp / 2) if exp is even
>> sqrt(x) = sqrt(y*10) * 10^((exp-1)/2) is exp is odd
>>
>> Then use BigInteger.sqrt() on the reduced value to compute the numerical digits of the desired result.
>>
>> Finally, scale back to the desired exponent range and perform any adjustment to get the preferred scale in the representation.
>
> Looking at the code, the divisors are quite limited, so my hypothesis above and the suggestions are out of place.
@rgiulietti The fundamental problem is that `BigDecimal` numbers store the magnitude in binary, using `BigInteger`s objects. A solution could have been to store the magnitude in radix 10 rather than in binary, but obviously this would require to redesign the entire class `BigDecimal`, which is undesirable.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/21301#issuecomment-2388722321
More information about the core-libs-dev
mailing list