List of utility methods to do Number Power
double | powerMean(final double p, final double[] x) Compute the power mean of an array of doubles for a given power (p). double sum = 0; for (int i = 0; i < x.length; ++i) { sum += Math.pow(x[i], p); return Math.pow(sum / x.length, 1.0 / p); |
long | powerMod(long base, long exp, long mod) Modular power. long result = 1; base %= mod; while (exp > 0) { if ((exp & 1) != 0) result = (result * base) % mod; exp >>= 1; base = base * base % mod; return result < 0 ? result + mod : result; |
int | powerOf(final int value, final int powerOf) Power of method for integer math. if (powerOf == 0) { return 0; int r = value; for (int x = 1; x < powerOf; x++) { r = r * value; return r; ... |
long | powerOf10(int number) Calculate 10 to the power of a number Could call Math.pow, but that would be a waste of CPU resources. long result = 1; final int ten = 10; for (int i = 1; i <= number; i++) { result *= ten; return result; |
int | powerOf2Log2(int n) power Of Log assert isPowerOf2(n) : n + " is not a power of 2"; for (int i = 0; i < 31; i++) { if ((n & 1) == 1) { return i; n >>= 1; return 0; ... |
int | powerOf31(int n) Returns 31^n. return POWERS_OF_31.length <= n ? synchronizedPowerOf31(n) : POWERS_OF_31[n];
|
Double | powerOfN(Double v, int n) power Of N Double value = 1d; if (n == 0) { return 1d; } else if (n > 0) { for (int i = 0; i < n; i++) { value = value * v; } else if (n < 0) { ... |
long | powerOfTen(int exp) power Of Ten assert exp >= 0; long fact = 1; for (int i = 0; i < exp; i++) { fact *= 10; return fact; |
int | powerOfTwo(final int i) Returns a power of two, bigger or equal to the input. if (i < MIN_ARRAY_SIZE) { return MIN_ARRAY_SIZE; if (2 * i == (i ^ (i - 1) + 1)) { return i; return 1 << (Integer.highestOneBit(i) + 1); |
int | powerOfTwo(int current, int desired) power Of Two int capacity = current == 0 ? 1 : current; while (capacity < desired) capacity <<= 1; return capacity; |