Example usage for java.lang Math pow

List of usage examples for java.lang Math pow

Introduction

In this page you can find the example usage for java.lang Math pow.

Prototype

@HotSpotIntrinsicCandidate
public static double pow(double a, double b) 

Source Link

Document

Returns the value of the first argument raised to the power of the second argument.

Usage

From source file:Main.java

public static int ConvertFromHexadecimalToDecimal(String num) {
    int a;//from  w  w w .j a v a2  s  .c  o m
    int ctr = 0;
    double prod = 0;

    for (int i = num.length(); i > 0; i--) {

        if (num.charAt(i - 1) == 'a' || num.charAt(i - 1) == 'A')
            a = 10;

        else if (num.charAt(i - 1) == 'b' || num.charAt(i - 1) == 'B')
            a = 11;

        else if (num.charAt(i - 1) == 'c' || num.charAt(i - 1) == 'C')
            a = 12;

        else if (num.charAt(i - 1) == 'd' || num.charAt(i - 1) == 'D')
            a = 13;

        else if (num.charAt(i - 1) == 'e' || num.charAt(i - 1) == 'E')
            a = 14;

        else if (num.charAt(i - 1) == 'f' || num.charAt(i - 1) == 'F')
            a = 15;

        else
            a = Character.getNumericValue(num.charAt(i - 1));
        prod = prod + (a * Math.pow(16, ctr));
        ctr++;
    }
    return (int) prod;
}

From source file:com.galenframework.specs.RangeValue.java

private static int convertValue(double value, int precision) {
    if (value > 0) {
        return (int) Math.floor(value * Math.pow(10, precision));
    } else {/*www .ja  v  a 2  s  . co m*/
        return (int) Math.ceil(value * Math.pow(10, precision));
    }
}

From source file:cc.redberry.core.number.Exponentiation.java

public static Real exponentiateIfPossible(Real base, Real power) {
    if (base.isZero())
        if (power.isInfinite())
            return Numeric.NaN;
        else//  www  .  java  2 s  .  co  m
            return Rational.ZERO;

    if (base.isNumeric() || power.isNumeric()) { // Bease or power are numeric
        return new Numeric(
                Math.pow(base.getNumericValue().doubleValue(), power.getNumericValue().doubleValue()));
    }

    //<-- Power and Base are rational

    if (power.isInteger())
        return new Rational(((Rational) base).getBigFraction().pow(((Rational) power).getNumerator())); //Using BigFraction pow method.

    //<-- Power is not integer

    BigInteger powerNum = ((Rational) power).getNumerator();
    BigInteger powerDen = ((Rational) power).getDenominator();

    BigInteger baseNum = ((Rational) base).getNumerator();
    BigInteger baseDen = ((Rational) base).getDenominator();

    baseNum = findIntegerRoot(baseNum, powerDen);
    baseDen = findIntegerRoot(baseDen, powerDen);

    if (baseNum == null || baseDen == null) //Result is irrational
        return null;

    return exponentiateIfPossible(new Rational(baseNum, baseDen), new Rational(powerNum));
}

From source file:Methods.CalculusBisection.java

public static void bisection1(double xlower, double xupper, double decPoint) {//method used calculate root point acording the paramethers that enter the method and store the data in a global Stack

    S = new BidimensionalArrayStack();//Declaring a new Stack beeing sure is clear before using it
    Xlower = new BidimensionalArrayStack();
    Xupper = new BidimensionalArrayStack();
    double xnew, fxlower, fxupper, fxnew, diff;
    int iteration;

    fxlower = xlower - Math.pow(xlower, 2);
    fxupper = xupper - Math.pow(xupper, 2);
    iteration = 0;/*from   w w  w  .  j av a 2 s .  c o m*/

    do {
        iteration += 1;
        // determine xnew and f (xnew)
        xnew = (xlower + xupper) / 2.0;
        fxnew = xnew - Math.pow(xnew, 2);
        System.out.println("Approx for iteration{}" + iteration + " is " + xnew);
        diff = Math.abs(xupper - xlower) / 2.0;
        Xlower.push(xlower, fxlower);//Inserting data in the Stack
        Xupper.push(xupper, fxupper);

        if (fxlower * fxnew > 0) {
            xlower = xnew;
            fxlower = fxnew;
        } else if (fxupper * fxnew > 0) {
            xupper = xnew;
            fxupper = fxnew;
        }
        S.push(xnew, fxnew);
    } while (diff > decPoint);
    System.out.println("root to six decimal places is " + xnew);
}

From source file:com.vsthost.rnd.SandboxStrategyTest.java

/**
 * Defines a silly formula to be used in the objectives.
 *
 * @param x X/*from   w  ww. j  a v  a 2s  .  c o m*/
 * @param y Y
 * @return Some silly result.
 */
public static double SillyFormula(double x, double y) {
    final double z = Math.sqrt(x * y == 0 ? 1 : Math.abs(x * y));
    final double t = Math.pow(x <= 0 ? 1 : x, y);
    return x * x * x * +3 * x * x + 2 * x * y + 3 * y * y + y * y * y + 5 * x * y + z + t;
}

From source file:Methods.CalculusSecant.java

public static void secant1(double xold1, double xold2, double decPoint) {//method used calculate root point acording the paramethers that enter the method and store the data in a global Stack

    double xnew, fxold1, fxold2, fxnew, diff;
    int iteration = 0;
    S = new BidimensionalArrayStack();//Declaring a new Stack beeing sure is clear before using it
    Xold1 = new BidimensionalArrayStack();
    Xold2 = new BidimensionalArrayStack();

    do {/*w  w  w. j a va2 s.c om*/

        iteration += 1;
        // determine f(xold1) and f(xold2)
        fxold1 = xold1 - Math.pow(xold1, 2);
        fxold2 = xold2 - Math.pow(xold2, 2);
        Xold1.push(xold1, fxold1);//Inserting data in the Stack
        Xold2.push(xold2, fxold2);
        xnew = xold1 - (fxold1 * (xold1 - xold2)) / (fxold1 - fxold2);
        System.out.println("Approx for iteration{}" + iteration + " is " + xnew);
        diff = Math.abs(xnew - xold1);
        xold2 = xold1;
        xold1 = xnew;
        fxnew = xnew - Math.pow(xnew, 2);
        S.push(xnew, fxnew);
    } while (diff > decPoint);

    System.out.println("root to six decimal places is " + xnew);

}

From source file:com.golemgame.util.LinearUtils.java

public static PolynomialFunction generateTranslatedFunction(double[] coefficients, double translation) {

    if (coefficients.length == 1 || translation == 0) {
        return new PolynomialFunction(coefficients);
    }/*from ww w. jav a2  s.  com*/
    translation = -translation;
    for (int power = 1; power < coefficients.length; power++) {
        if (coefficients[power] != 0) {
            double[] expandedCoefficients = getBinomialTerms(power, 1, translation);
            for (int i = 0; i < power; i++) {
                expandedCoefficients[i] *= coefficients[power];
                coefficients[i] += expandedCoefficients[i] * Math.pow(translation, power - i);
            }
        }
    }

    return new PolynomialFunction(coefficients);
}

From source file:edu.byu.nlp.stats.GammaDistribution.java

/**
 * self-contained gamma generator. Multiply result with scale parameter (or
 * divide by rate parameter). After Teh (npbayes).
 * // ww w  . j a  v  a  2s.  c  om
 * Taken From knowceans.
 */
public static double sample(double shape, RandomGenerator rnd) {
    Preconditions.checkArgument(shape > 0.0);
    Preconditions.checkNotNull(rnd);

    if (shape == 1.0) {
        /* Exponential */
        return -Math.log(rnd.nextDouble());
    } else if (shape < 1.0) {
        /* Use Johnk's generator */
        double cc = 1.0 / shape;
        double dd = 1.0 / (1.0 - shape);
        while (true) {
            double xx = Math.pow(rnd.nextDouble(), cc);
            double yy = xx + Math.pow(rnd.nextDouble(), dd);
            if (yy <= 1.0) {
                // FIXME: assertion error for rr = 0.010817814317923407
                // assert yy != 0 && xx / yy > 0 : "rr = " + rr;
                // INFO: this if is a hack
                if (yy != 0 && xx / yy > 0) {
                    return -Math.log(rnd.nextDouble()) * xx / yy;
                }
            }
        }
    } else { /* rr > 1.0 */
        /* Use bests algorithm */
        double bb = shape - 1.0;
        double cc = 3.0 * shape - 0.75;
        while (true) {
            double uu = rnd.nextDouble();
            double vv = rnd.nextDouble();
            double ww = uu * (1.0 - uu);
            double yy = Math.sqrt(cc / ww) * (uu - 0.5);
            double xx = bb + yy;
            if (xx >= 0) {
                double zz = 64.0 * ww * ww * ww * vv * vv;
                assert zz > 0 && bb != 0 && xx / bb > 0;
                if ((zz <= (1.0 - 2.0 * yy * yy / xx))
                        || (Math.log(zz) <= 2.0 * (bb * Math.log(xx / bb) - yy))) {
                    return xx;
                }
            }
        }
    }
}

From source file:Main.java

public static String getHumanReadableByteCount(long bytes, boolean si) {
    int unit = si ? 1000 : 1024;
    if (bytes < unit)
        return bytes + " B";
    int exp = (int) (Math.log(bytes) / Math.log(unit));
    String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
    return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}

From source file:com.opengamma.analytics.financial.timeseries.returns.SimplyCompoundedGeometricMeanReturnCalculator.java

@Override
public Double evaluate(final double[] x) {
    Validate.notNull(x, "x");
    ArgumentChecker.notEmpty(x, "x");
    final int n = x.length;
    double mult = 1 + x[0];
    for (int i = 1; i < n; i++) {
        mult *= 1 + x[i];//from  ww w.ja va  2s  .  com
    }
    return Math.pow(mult, 1. / n) - 1;
}