Is there any ceil for Math.floorDiv()
ceil
Math.floorDiv()
How to calculate it in the fastest way with what we have?
UPDATE
The code for floorDiv() follows:
floorDiv()
public static long floorDiv(long x, long y) { long r = x / y; // if the signs are different and modulo not zero, round down if ((x ^ y) < 0 && (r * y != x)) { r--; } return r; }
Is it possible to encode ceil same way?
UPDATE 2
I have seen this answer https://stackoverflow.com/a/166268/2126 , but there seem to be too many unnecessary operations.
There are none in the Math class, but you can easily compute it
Math
long ceilDiv(long x, long y){ return -Math.floorDiv(-x,y); }
For example, ceilDiv(1,2) = -floorDiv(-1,2) = -(-1) = 1 (the correct answer).
ceilDiv(1,2)
-floorDiv(-1,2)
-(-1)
I would also use floorMod negation, but if you are going to define your own function, you can simply adapt the code above:
public static int ceilDiv(int x, int y) { int r = x / y; // if the signs are the same and modulo not zero, round up if ((x ^ y) >= 0 && (r * y != x)) r++; return r; }
You can use the floorDiv function and play with it:
floorDiv
int ceilDiv(int x, int y) { return Math.floorDiv(x, y) + (x % y == 0 ? 0 : 1) }
Source: https://habr.com/ru/post/980136/More articles:Three column layouts: fixed-width center with fluid side columns - htmlwhy does 0.0f / 0.0f not generate any runtime error? - javaHow to center a heading based on a specific word - htmlStoring a binary tree in an array - javaCalling parseFrom () method for generic protobuffer class in java - javaAWS Lambda Java Compatibility Compatibility - javaGROUP BY WHERE range AND const ref without time - mysqlPython: setting cookie to another website - javascriptmongoose schema multi ref for one object - mongodbCannot run sample code mentioned in Flask docs - pythonAll Articles