RFR: 8341470: BigDecimal.stripTrailingZeros() optimization [v33]
Raffaello Giulietti
rgiulietti at openjdk.org
Fri Oct 11 17:32:13 UTC 2024
On Thu, 10 Oct 2024 21:54:27 GMT, Raffaello Giulietti <rgiulietti at openjdk.org> wrote:
>> Could make sense using Math.round() instead of Math.ceil() to get better upper bound?
>
> I'll have to check...
> ... tomorrow ;-)
IIUC, the code assumes that the floating-point computation `Math.ceil(intVal.bitLength() * LOG_5_OF_2)` has the same value as the mathematical ⌈ `intVal.bitLength()` × log5(2) ⌉.
I don't think this is safe, as it might happen that the computed and mathematical values are off by ±1.
To ensure 5^`maxPowsOf5` >= `intVal` (that is, maxPowsOf5 >= log5(intVal)) it would be more prudent to have
long maxPowsOf5 = (long) Math.ceil(intVal.bitLength() * LOG_5_OF_2) + 1;
But I think what you really want is maybe to meet 5^`maxPowsOf5` <= `intVal` < 5^(`maxPowsOf5` + 1) instead?
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/21323#discussion_r1797246974
More information about the core-libs-dev
mailing list