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

Method pow

output/java_guava/1.4.19/IntMath.java:230–264  ·  view source on GitHub ↗

Returns b to the kth power. Even if the result overflows, it will be equal to BigInteger.valueOf(b).pow(k).intValue(). This implementation runs in O(log k) time. Compare #checkedPow, which throws an ArithmeticException upon overflow. @throws Illeg

(int b, int k)

Source from the content-addressed store, hash-verified

228 */
229
230 @GwtIncompatible // failing tests
231 public static int pow(int b, int k) {
232 checkNonNegative("exponent", k);
233 switch (b) {
234 case 0:
235 return (k == 0) ? 1 : 0;
236 case 1:
237 return 1;
238 case (-1):
239 return ((k & 1) == 0) ? 1 : -1;
240 case 2:
241 return (k < Integer.SIZE) ? (1 << k) : 0;
242 case (-2):
243 if (k < Integer.SIZE) {
244 return ((k & 1) == 0) ? (1 << k) : - (1 << k);
245 } else {
246 return 0;
247 }
248
249 default:
250 // continue below to handle the general case
251
252 }
253 for (int accum = 1; ; k >>= 1) {
254 switch (k) {
255 case 0:
256 return accum;
257 case 1:
258 return b * accum;
259 default:
260 accum *= ((k & 1) == 0) ? 1 : b;
261 b *= b;
262 }
263 }
264 }
265
266 /**
267 * Returns the square root of {@code x}, rounded with the specified rounding mode.

Callers 4

log2Method · 0.45
log10Method · 0.45
sqrtMethod · 0.45
expectedFppMethod · 0.45

Calls 1

checkNonNegativeMethod · 0.45

Tested by

no test coverage detected