JDK-8164433 : Add ceilDiv() to java.lang.Math
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 9
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2016-08-18
  • Updated: 2022-04-27
  • Resolved: 2022-04-27
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
In the java.lang.Math class, there are several methods for working with the floor function (floor(), floorDiv(), floorMod()), but not for the ceil function. (a plain ceil() method exists)

As a Software Developer, I would expect to find corresponding methods for the ceil() function (namely, ceilDiv() and ceilMod()). However, neither such method exists as of Java 8. [1] Apparently, these methods were not added when floorMod() and floorDiv() were added due to the JDK not needing them internally. [2]

From my point of view, especially the ceilDiv() function is helpful, but I could not find a use case for ceilMod(). I am not sure if it makes sense to implement ceilMod(). Wikipedia [3] mentions floored division as a method to calculate the modulus of two numbers, but makes no (explicit) mention of ceil division, so I assume that it is not practicable or widely used or does not make sense. Hence, this request is only for a ceilDiv() method, although it could make sense to also implement ceilMod() if that is indeed practicable and an implementation is possible.

---
[1] https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html
[2] http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-August/042692.html
[3] https://en.wikipedia.org/wiki/Modulo_operation#Remainder_calculation_for_the_modulo_operation

JUSTIFICATION :
As a developer, I would reasonably expect the Math class to have utilities for both the floor and the ceil function. I do not see any way that it would hurt the class to have these two methods that would be expected there.

If I wanted to have a ceiling division in an application, I would have to write my own implementations in a separate class, when that method clearly belongs to the Math class - if it didn't belong, the floor methods wouldn't be there either. Furthermore, the implementation does not seem to be complex enough to justify that methods missing. [4]

Such utility method ist just as useful as the floorDiv() method, to avoid unnecessary casts and make intent clearer. That effectively leads to cleaner code that is easier to read. Additionally, a new Java(tm) programmer would see the different code needed in applications for floor and ceil and wonder why one uses java.lang.Math, while the other uses a custom implementation.

----
[4] http://stackoverflow.com/a/27643634/1117552

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The method Math#ceilDiv(int/long x, int/long y) should exist in the java.lang.Math class, behaving exactly like its floor counterpart, just for the ceil function instead of the floor function. Namely, it should provide the smallest (closest to negative infinity) int value that is greater than or equal to the algebraic quotient. As such, it should respect different signs of the arguments, like the ceilDiv() method does.
ACTUAL -
Said method does not exist.

---------- BEGIN SOURCE ----------
//given
int dividend = -4;
int divisor = 3; 
//when
int result = Math.ceilDiv(dividend, divisor); // ** compiler error - no such method
//then
assert result == -1; //algebraic quotient is -1.333.. -> smallest int greater than or equal to that is -1
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Any of the answers to [4], implemented in a custom utility class.

-----
[4] http://stackoverflow.com/a/27643634/1117552


Comments
Alternatively, div/mod methods that took a java.math.RoundingMode could be written.
22-08-2016