public static long pow ( long l , int exp ) { BigInteger bi = BigInteger . valueOf ( l ) . pow ( exp ) ; if ( bi . compareTo ( BigInteger . valueOf ( Long . MAX_VALUE ) ) > 0 || bi . compareTo ( BigInteger . valueOf ( Long . MIN_VALUE ) ) < 0 ) { throw new IllegalArgumentException ( "pow(" + l + "," + exp + ") exceeds Long.MAX_VALUE" ) ; } return bi . longValue ( ) ; }
Java . lang . pow only works on doubles ; this fixes that . Safe version of pow that relies on BigInteger that can raise a long to an int exponential . Uses BigInteger internally to ensure this is done correctly and does a check on the value to ensure it can safely be converted into a long