Java Utililty Methods Number Power

List of utility methods to do Number Power

Description

The list of methods to do Number Power are organized into topic(s).

Method

doublepowerMean(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);
longpowerMod(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;
intpowerOf(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;
...
longpowerOf10(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;
intpowerOf2Log2(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;
...
intpowerOf31(int n)
Returns 31^n.
return POWERS_OF_31.length <= n ? synchronizedPowerOf31(n) : POWERS_OF_31[n];
DoublepowerOfN(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) {
...
longpowerOfTen(int exp)
power Of Ten
assert exp >= 0;
long fact = 1;
for (int i = 0; i < exp; i++) {
    fact *= 10;
return fact;
intpowerOfTwo(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);
intpowerOfTwo(int current, int desired)
power Of Two
int capacity = current == 0 ? 1 : current;
while (capacity < desired)
    capacity <<= 1;
return capacity;