[9] RFR of 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
Brian Burkhalter
brian.burkhalter at oracle.com
Tue Sep 29 15:02:19 UTC 2015
Hi Stephen,
On Sep 29, 2015, at 3:49 AM, Stephen Colebourne <scolebourne at joda.org> wrote:
> Good to see this happen.
Glad to hear it.
> I agree a test would be good to demonstrate edge cases.
I’ll add something.
> 1)
> I think the code for floorMod(long x, int y); cannot actually
> overflow. As such, the result could just be cast without the if and
> throw.
I’ll investigate further.
> 2)
> My preferred algorithm for floorMod is:
>
> return ((a % b) + b) % b;
>
> as it contains no Java-side branches, although tests would be needed
> to prove performance.
>
> This also allows an algorithm for floorDiv with no Java-side branches:
>
> int mod = ((a % b) + b) % b;
> return (a - mod) / b;
I tested the code which was in the original issue description and found some discrepancies. I’ll need to revisit this to see what happened.
>
> 3)
> While making changes, this code could be changed to avoid the local
> variable (just return):
> public static int floorMod(int x, int y) {
> int r = x - floorDiv(x, y) * y;
> return r;
> }
I agree. I saw that myself but left it as-is.
Thanks,
Brian
More information about the core-libs-dev
mailing list