[9] RFR of 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math

Stephen Colebourne scolebourne at joda.org
Tue Sep 29 15:05:45 UTC 2015


On 29 September 2015 at 16:02, Brian Burkhalter
<brian.burkhalter at oracle.com> wrote:
> 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.

Yes, the code in the issue for floorDiv() fails when the divisor is
negative. The one in my email today works though.
Stephen



More information about the core-libs-dev mailing list