MCPcopy Create free account
hub / github.com/antlr/codebuff / divide

Method divide

output/java_guava/1.4.18/UnsignedLongs.java:199–223  ·  view source on GitHub ↗

Returns dividend / divisor, where the dividend and divisor are treated as unsigned 64-bit quantities. @param dividend the dividend (numerator) @param divisor the divisor (denominator) @throws ArithmeticException if divisor is 0

(long dividend, long divisor)

Source from the content-addressed store, hash-verified

197
198
199 public static long divide(long dividend, long divisor) {
200 if (divisor < 0) { // i.e., divisor >= 2^63:
201 if (compare(dividend, divisor) < 0) {
202 return 0; // dividend < divisor
203 } else {
204 return 1; // dividend >= divisor
205 }
206 }
207
208 // Optimization - use signed division if dividend < 2^63
209 if (dividend >= 0) {
210 return dividend / divisor;
211 }
212
213 /*
214 * Otherwise, approximate the quotient, check, and correct if necessary. Our approximation is
215 * guaranteed to be either exact or one less than the correct value. This follows from fact that
216 * floor(floor(x)/i) == floor(x/i) for any real x and integer i != 0. The proof is not quite
217 * trivial.
218 */
219
220 long quotient = ((dividend >>> 1) / divisor) << 1;
221 long rem = dividend - quotient * divisor;
222 return quotient + (compare(rem, divisor) >= 0 ? 1 : 0);
223 }
224
225 /**
226 * Returns dividend % divisor, where the dividend and divisor are treated as unsigned 64-bit

Callers 3

toStringMethod · 0.95
UnsignedLongsClass · 0.95
dividedByMethod · 0.95

Calls 1

compareMethod · 0.95

Tested by

no test coverage detected