RFR: 8334755: Asymptotically faster implementation of square root algorithm [v23]

fabioromano1 duke at openjdk.org
Fri Jul 12 14:01:53 UTC 2024


On Fri, 12 Jul 2024 13:49:55 GMT, Raffaello Giulietti <rgiulietti at openjdk.org> wrote:

>> The proof has been made simply by exaustive experiment: for every long value `s` in [0, 2^32) such that `x == s * s`, it is true that `s == (long) Math.sqrt(x >= 0 ? x : x + 0x1p64)`. This can be verified directly by a Java program.
>> It means that Math.sqrt() returns the correct value for every perfect square, and so the value returned by Math.sqrt() for whatever long value in the range [0, 2^64) is either correct, or rounded up by one if the value is too high and too close to a perfect square.
>
> (Yes, I proved it to myself in this way.)
> 
> A similar explanation should be in a comment in the code, not here. The source of truth is the code, not the comments on GitHub ;-)

I would keep zero as the only special case, so as not to have to modify `x > 0` with `x >= 0` in `Math.sqrt(x > 0 ? x : x + 0x1p64)`.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/19710#discussion_r1675970940


More information about the core-libs-dev mailing list