On Fri, 11 Feb 2022 12:25:08 GMT, XenoAmess <d...@openjdk.java.net> wrote:

> FWIW, (int) Math.ceil(expected / 0.75) and (int) ((expected * 4L + 2L) / 3L) 
> would be equivalent.

No, they are not equivalent. If `expected` exceeds a certain value around 
1.6bn, then the intermediate result using the second expression will be greater 
than Integer.MAX_VALUE. Casting this to `int` can result in a negative number.

In the first expression, a double value that exceeds the range of `int` will 
saturate at Integer.MAX_VALUE.


jshell> (int) ((1700000000 * 4L + 2L) / 3L)
$24 ==> -2028300629

jshell> (int) Math.ceil(1700000000 / 0.75)
$25 ==> 2147483647


Let's stick with the `Math.ceil` expression please.

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

PR: https://git.openjdk.java.net/jdk/pull/7431

Reply via email to