ph-math/src/main/java/com/helger/numbercruncher/mathutils/BigFunctions.java
public static BigDecimal intPower (@Nonnull final BigDecimal px, final long pexponent, final int scale)
  {
    BigDecimal x = px;
    long exponent = pexponent;

    // If the exponent is negative, compute 1/(x^-exponent).
    if (exponent < 0)
    {
      return BigDecimal.ONE.divide (intPower (x, -exponent, scale), scale, RoundingMode.HALF_EVEN);
    }

    BigDecimal power = BigDecimal.ONE;

    // Loop to compute value^exponent.
    while (exponent > 0)
    {

      // Is the rightmost bit a 1?
      if ((exponent & 1) == 1)
      {
        power = power.multiply (x).setScale (scale, RoundingMode.HALF_EVEN);
      }

      // Square x and shift exponent 1 bit to the right.
      x = x.multiply (x).setScale (scale, RoundingMode.HALF_EVEN);
      exponent >>= 1;

      Thread.yield ();
    }

    return power;
  }
public static BigDecimal intPower (@Nonnull final BigDecimal px, final long pexponent, final int scale)
  {
    BigDecimal x = px;
    long exponent = pexponent;

    // If the exponent is negative, compute 1/(x^-exponent).
    if (exponent < 0)
    {
      return BigDecimal.ONE.divide (intPower (x, -exponent, scale), scale, RoundingMode.HALF_EVEN);
    }

    BigDecimal power = BigDecimal.ONE;

    // Loop to compute value^exponent.
    while (exponent > 0)
    {

      // Is the rightmost bit a 1?
      if ((exponent & 1) == 1)
      {
        power = power.multiply (x).setScale (scale, RoundingMode.HALF_EVEN);
      }

      // Square x and shift exponent 1 bit to the right.
      x = x.multiply (x).setScale (scale, RoundingMode.HALF_EVEN);
      exponent >>= 1;

      Thread.yield ();
    }

    return power;
  }
public static BigDecimal intPower ( @ Nonnull final BigDecimal px , final long pexponent , final int scale ) { BigDecimal x = px ; long exponent = pexponent ; // If the exponent is negative, compute 1/(x^-exponent). if ( exponent < 0 ) { return BigDecimal . ONE . divide ( intPower ( x , - exponent , scale ) , scale , RoundingMode . HALF_EVEN ) ; } BigDecimal power = BigDecimal . ONE ; // Loop to compute value^exponent. while ( exponent > 0 ) { // Is the rightmost bit a 1? if ( ( exponent & 1 ) == 1 ) { power = power . multiply ( x ) . setScale ( scale , RoundingMode . HALF_EVEN ) ; } // Square x and shift exponent 1 bit to the right. x = x . multiply ( x ) . setScale ( scale , RoundingMode . HALF_EVEN ) ; exponent >>= 1 ; Thread . yield ( ) ; } return power ; }
Compute x^exponent to a given scale. Uses the same algorithm as class
numbercruncher.mathutils.IntPower.

@param px
the value x
@param pexponent
the exponent value
@param scale
the desired scale of the result
@return the result value
Compute x^exponent to a given scale . Uses the same algorithm as class numbercruncher . mathutils . IntPower .