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

/**
 * Fills the array with random doubles.  Values will be between min (inclusive) and
 * max (inclusive)./*from  w  w  w. ja  v a 2 s . co  m*/
 */
public static void genRandomDoubles(long seed, double min, double max, double array[],
        boolean includeExtremes) {
    Random r = new Random(seed);
    int minExponent = Math.min(Math.getExponent(min), 0);
    int maxExponent = Math.max(Math.getExponent(max), 0);
    if (minExponent < -6 || maxExponent > 6) {
        // Use an exponential distribution
        int exponentDiff = maxExponent - minExponent;
        for (int i = 0; i < array.length; i++) {
            double mantissa = r.nextDouble();
            int exponent = minExponent + r.nextInt(maxExponent - minExponent);
            int sign = (min >= 0) ? 1 : 1 - r.nextInt(2) * 2; // -1 or 1
            double rand = sign * mantissa * Math.pow(2.0, exponent);
            if (rand < min || rand > max) {
                continue;
            }
            array[i] = rand;
        }
    } else {
        // Use a linear distribution
        for (int i = 0; i < array.length; i++) {
            double rand = r.nextDouble();
            array[i] = min + rand * (max - min);
        }
    }
    // Seed a few special numbers we want to be sure to test.
    for (int i = 0; i < sInterestingDoubles.length; i++) {
        double d = sInterestingDoubles[i];
        if (min <= d && d <= max) {
            array[r.nextInt(array.length)] = d;
        }
    }
    array[r.nextInt(array.length)] = min;
    array[r.nextInt(array.length)] = max;
    if (includeExtremes) {
        array[r.nextInt(array.length)] = Double.NaN;
        array[r.nextInt(array.length)] = Double.POSITIVE_INFINITY;
        array[r.nextInt(array.length)] = Double.NEGATIVE_INFINITY;
        array[r.nextInt(array.length)] = Double.MIN_VALUE;
        array[r.nextInt(array.length)] = Double.MIN_NORMAL;
        array[r.nextInt(array.length)] = Double.MAX_VALUE;
        array[r.nextInt(array.length)] = -Double.MIN_VALUE;
        array[r.nextInt(array.length)] = -Double.MIN_NORMAL;
        array[r.nextInt(array.length)] = -Double.MAX_VALUE;
    }
}

From source file:Main.java

public static void getDisplayDpi(Context ctx) {
    DisplayMetrics dm = new DisplayMetrics();
    WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(dm);
    double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
    double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
    double screenInches = Math.sqrt(x + y);
    int screenInch = (int) Math.round(screenInches);
    Log.d("screeninch", String.valueOf(screenInch));
    int dapi = dm.densityDpi;
    Log.d("dapi", String.valueOf(dapi));
    try {/*from  w ww .j  a  v a2s .  c  o  m*/
        switch (dm.densityDpi) {

        case DisplayMetrics.DENSITY_LOW:
            UI_DENSITY = 120;
            if (screenInch <= 7) {
                UI_SIZE = 4;

            } else {
                UI_SIZE = 10;
            }
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            UI_DENSITY = 160;
            if (screenInch <= 7) {
                UI_SIZE = 4;
            } else {
                UI_SIZE = 10;
            }
            break;
        case DisplayMetrics.DENSITY_HIGH:
            UI_DENSITY = 240;
            if (screenInch <= 7) {
                UI_SIZE = 4;
            } else {
                UI_SIZE = 10;
            }
            break;

        default:
            break;
        }
    } catch (Exception e) {
        // Caught exception here
    }
}

From source file:ChartThread.java

public ChartThread(IEvaluableEnTiempo evaluable, ChartPanel panel) {
    this.evaluable = evaluable;
    this.panel = panel;
    this.frames = 225;
    this.time = 150;
    frameActual = 0;/*from  w w  w .j a  v a 2  s  .c o  m*/
    unidad = evaluarUnidad();
    if (unidad >= Math.pow(10, 11))
        this.frames = 1000;

}

From source file:Main.java

public static int itoa(int iSrc, byte[] buffer, int offset) {
    int positionalNumber = getPositionalNumber(iSrc);
    int length = positionalNumber;

    int i = offset;
    while (positionalNumber > 0) {
        int jesu = (int) Math.pow(10, positionalNumber - 1);
        int quotiont = iSrc / jesu;

        buffer[i] = (byte) (quotiont + '0');

        int remainder = iSrc % jesu;

        positionalNumber--;//www.j  a v  a2s. c  o  m
        i++;
        iSrc = remainder;
    }
    return length;
}

From source file:Main.java

/**
 * Returns the closest power-of-two number less than or equal to x.
 *
 * @param x input value//from w ww  . j  a v a  2 s. co m
 *
 * @return the closest power-of-two number less then or equal to x
 */
public static int prevPow2(int x) {
    if (x < 1) {
        throw new IllegalArgumentException("x must be greater or equal 1");
    }
    return (int) Math.pow(2, Math.floor(Math.log(x) / Math.log(2)));
}

From source file:Main.java

/**
 * Creates an approximated cubic gradient using a multi-stop linear gradient. See
 * <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
 * details.//from  w  w w  .  ja  va  2s  .  c o m
 */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {
    numStops = Math.max(numStops, 2);

    PaintDrawable paintDrawable = new PaintDrawable();
    paintDrawable.setShape(new RectShape());

    final int[] stopColors = new int[numStops];

    int red = Color.red(baseColor);
    int green = Color.green(baseColor);
    int blue = Color.blue(baseColor);
    int alpha = Color.alpha(baseColor);

    for (int i = 0; i < numStops; i++) {
        float x = i * 1f / (numStops - 1);
        float opacity = constrain(0, 1, (float) Math.pow(x, 3));
        stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);
    }

    final float x0, x1, y0, y1;
    switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    case Gravity.START:
        x0 = 1;
        x1 = 0;
        break;
    case Gravity.END:
        x0 = 0;
        x1 = 1;
        break;
    default:
        x0 = 0;
        x1 = 0;
        break;
    }
    switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.TOP:
        y0 = 1;
        y1 = 0;
        break;
    case Gravity.BOTTOM:
        y0 = 0;
        y1 = 1;
        break;
    default:
        y0 = 0;
        y1 = 0;
        break;
    }

    paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            return new LinearGradient(width * x0, height * y0, width * x1, height * y1, stopColors, null,
                    Shader.TileMode.CLAMP);
        }
    });

    return paintDrawable;
}

From source file:StringUtils.java

/**
 * Format a percentage for presentation to the user.
 * @param done the percentage to format (0.0 to 1.0)
 * @param digits the number of digits past the decimal point
 * @return a string representation of the percentage
 *//*from   w  w w  .j a v a 2s  . c o m*/
public static String formatPercent(double done, int digits) {
    DecimalFormat percentFormat = new DecimalFormat("0.00%");
    double scale = Math.pow(10.0, digits + 2);
    double rounded = Math.floor(done * scale);
    percentFormat.setDecimalSeparatorAlwaysShown(false);
    percentFormat.setMinimumFractionDigits(digits);
    percentFormat.setMaximumFractionDigits(digits);
    return percentFormat.format(rounded / scale);
}

From source file:Main.java

public static Bitmap loadMpoBitmapFromFile(File file, long offset, int maxWidth, int maxHeight)
        throws IOException {
    // First, decode the width and height of the image, so that we know how much to scale
    // it down when loading it into our ImageView (so we don't need to do a huge allocation).
    BitmapFactory.Options opts = new BitmapFactory.Options();
    InputStream fs = null;/*from ww w  .  ja  v a2  s  .co  m*/
    try {
        fs = new BufferedInputStream(new FileInputStream(file));
        fs.skip(offset);
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(fs, null, opts);
    } finally {
        if (fs != null) {
            try {
                fs.close();
            } catch (IOException e) {
                // don't worry
            }
        }
    }
    int scale = 1;
    if (opts.outHeight > maxHeight || opts.outWidth > maxWidth) {
        scale = (int) Math.pow(2, (int) Math
                .round(Math.log(maxWidth / (double) Math.max(opts.outHeight, opts.outWidth)) / Math.log(0.5)));
    }
    if ((opts.outHeight <= 0) || (opts.outWidth <= 0)) {
        return null;
    }
    // Decode the image for real, but now with a sampleSize.
    // We have to reopen the file stream, and re-skip to the correct offset, since
    // FileInputStream doesn't support reset().
    Bitmap bmp = null;
    fs = null;
    try {
        fs = new BufferedInputStream(new FileInputStream(file));
        fs.skip(offset);
        BitmapFactory.Options opts2 = new BitmapFactory.Options();
        opts2.inSampleSize = scale;
        bmp = BitmapFactory.decodeStream(fs, null, opts2);
    } finally {
        if (fs != null) {
            try {
                fs.close();
            } catch (IOException e) {
                // don't worry
            }
        }
    }
    return bmp;
}

From source file:com.cloudera.hts.utils.math.MyFunc2.java

public double value(double t, double... parameters) {

    final double a = parameters[0];
    final double b = parameters[1];
    final double c = parameters[2];

    return a * Math.pow(t, 2) + b * t + c;

}

From source file:Main.java

/**
 * Creates an approximated cubic gradient using a multi-stop linear gradient. See
 * <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
 * details.//w w  w .  j a v a  2  s  . co  m
 */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {

    // Generate a cache key by hashing together the inputs, based on the method described in the Effective Java book
    int cacheKeyHash = baseColor;
    cacheKeyHash = 31 * cacheKeyHash + numStops;
    cacheKeyHash = 31 * cacheKeyHash + gravity;

    Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
    if (cachedGradient != null) {
        return cachedGradient;
    }

    numStops = Math.max(numStops, 2);

    PaintDrawable paintDrawable = new PaintDrawable();
    paintDrawable.setShape(new RectShape());

    final int[] stopColors = new int[numStops];

    int red = Color.red(baseColor);
    int green = Color.green(baseColor);
    int blue = Color.blue(baseColor);
    int alpha = Color.alpha(baseColor);

    for (int i = 0; i < numStops; i++) {
        float x = i * 1f / (numStops - 1);
        float opacity = Math.max(0, Math.min(1, (float) Math.pow(x, 3)));
        stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);
    }

    final float x0, x1, y0, y1;
    switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    case Gravity.START:
        x0 = 1;
        x1 = 0;
        break;
    case Gravity.END:
        x0 = 0;
        x1 = 1;
        break;
    default:
        x0 = 0;
        x1 = 0;
        break;
    }
    switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.TOP:
        y0 = 1;
        y1 = 0;
        break;
    case Gravity.BOTTOM:
        y0 = 0;
        y1 = 1;
        break;
    default:
        y0 = 0;
        y1 = 0;
        break;
    }

    paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            return new LinearGradient(width * x0, height * y0, width * x1, height * y1, stopColors, null,
                    Shader.TileMode.CLAMP);
        }
    });

    cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
    return paintDrawable;
}