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

/**
 * Calculates the accuracy of an RSSI reading.
 *
 * The code was taken from {@link http://stackoverflow.com/questions/20416218/understanding-ibeacon-distancing}
 *
 * @param txPower the calibrated TX power of an iBeacon
 * @param rssi the RSSI value of the iBeacon
 * @return/*  w w  w.ja  va2 s . co m*/
 */
public static double calculateAccuracy(int txPower, double rssi) {
    if (rssi == 0) {
        return -1.0; // if we cannot determine accuracy, return -1.
    }

    double ratio = rssi * 1.0 / txPower;
    if (ratio < 1.0) {
        return Math.pow(ratio, 10);
    } else {
        final double accuracy = (0.89976) * Math.pow(ratio, 7.7095) + 0.111;
        return accuracy;
    }
}

From source file:Main.java

/**
 * Generates a list of colors. The result is always identical.
 * //from w  w  w. j a v  a  2 s .c om
 * @param amount The number of colors to calculate.
 * @return A list of colors. Never returns <code>null</code>.
 * @see https://stackoverflow.com/questions/3403826/how-to-dynamically-compute-a-list-of-colors
 */
private static List<Color> calculateUniqueColors(int amount) {
    final int lowerLimit = 0x10;
    final int upperLimit = 0xE0;
    final int colorStep = (int) ((upperLimit - lowerLimit) / Math.pow(amount, 1f / 3));

    final List<Color> colors = new ArrayList<>(amount);

    for (int R = lowerLimit; R < upperLimit; R += colorStep)
        for (int G = lowerLimit; G < upperLimit; G += colorStep)
            for (int B = lowerLimit; B < upperLimit; B += colorStep) {
                if (colors.size() >= amount) { // The calculated step is not very precise, so this safeguard is appropriate
                    return colors;
                } else {
                    int color = (R << 16) + (G << 8) + (B);
                    colors.add(new Color(color));
                }
            }
    return colors;
}

From source file:Main.java

/**
 * Converts String to boolean array/*from  w  w  w. j a va2  s  .c o  m*/
 * @param data
 * @param charsetSize
 * @return
 */
public static boolean[] data2Binary(String data, int charsetSize) {
    boolean[] bits = new boolean[data.length() * charsetSize];

    int index = 0;
    for (char c : data.toCharArray()) {
        int asciiVal = (int) c;
        System.out.print(c + ":" + asciiVal + " ");

        asciiVal <<= 1; // throw away most left bit. (is always a 0, cfr. ASCII-table)

        for (int i = 0; i < charsetSize; i++) {
            bits[index] = (asciiVal & (int) Math.pow(2, charsetSize)) == 0 ? false : true;
            System.out.print(bits[index] + " ");
            asciiVal <<= 1; // throw away left most bit
            index++;
        }
        System.out.println();
    }

    return bits;
}

From source file:Main.java

public static Bitmap Bytes2Bimap(byte[] bytes, int maxSize) {
    try {//from   w  ww .  jav  a2s .  c om
        if (bytes == null) {
            return null;
        }

        if (bytes.length != 0) {
            BitmapFactory.Options opt = new BitmapFactory.Options();
            opt.inJustDecodeBounds = true;
            BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opt);

            int scale = 1;

            if ((opt.outHeight > maxSize) || (opt.outWidth > maxSize)) {
                scale = (int) Math.pow(2, (int) Math.round(
                        Math.log(maxSize / (double) Math.max(opt.outHeight, opt.outWidth)) / Math.log(0.5)));
            }

            BitmapFactory.Options newOpt = new BitmapFactory.Options();
            newOpt.inSampleSize = scale;

            return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, newOpt);
        } else {
            return null;
        }
    } catch (Exception ex) {
        return null;
    }
}

From source file:Main.java

/**
 * Get the current device physical size in inches.
 *
 * @param context valid context./*ww  w. ja  v  a  2s  .co  m*/
 * @return <b>double</b> device inches
 */
public static double getDeviceDiagonalSizeInInches(final Context context) {
    final DisplayMetrics metrics = getDisplayMetrics(context);

    final double xInches = (double) metrics.widthPixels / metrics.xdpi;
    final double yInches = (double) metrics.heightPixels / metrics.ydpi;
    return Math.sqrt(Math.pow(xInches, 2) + Math.pow(yInches, 2));
}

From source file:Main.java

public static BigInteger getZ(ArrayList<byte[]> c1, ArrayList<byte[]> c2, BigInteger p) {
    BigInteger z = BigInteger.ZERO;

    //TODO: make sure c1 and c2 are of the same size
    int size = c1.size();
    if (size > c2.size()) {
        size = c2.size();/*from w  w  w  .j  a  v a  2 s  .c om*/
    }

    for (int i = 0; i < size; i++) {
        BigInteger c1BI = new BigInteger(1, c1.get(i));
        BigInteger c2BI = new BigInteger(1, c2.get(i));
        BigInteger exp = new BigInteger(1, ByteBuffer.allocate(8).putLong((long) Math.pow(2, i)).array());

        z = z.add((c1BI.multiply(c2BI)).modPow(exp, p));
        Log.d("CeCk", "z calculation " + i + "/" + size + " round");
    }
    return z.mod(p);
}

From source file:Main.java

public static String calculateSize(String value) {

    long bytes = Long.parseLong(value);

    int unit = 1024;
    if (bytes < unit)
        return bytes + " B";
    int exp = (int) (Math.log(bytes) / Math.log(unit));
    String pre = "KMGTPE".charAt(exp - 1) + "i";

    return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre).replace(",", ".");

}

From source file:Main.java

public static Double getDistance(double lat1, double lat2, double lon1, double lon2, double el1, double el2) {

    final int R = 6371; // Radius of the earth

    Double latDistance = Math.toRadians(lat2 - lat1);
    Double lonDistance = Math.toRadians(lon2 - lon1);
    Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
    Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double distance = R * c; // convert to meters

    double height = el1 - el2;

    distance = Math.pow(distance, 2) + Math.pow(height, 2);

    return Math.sqrt(distance);
}

From source file:Main.java

public static File downSample(Context context, Uri uri) throws Exception {
    Bitmap b = null;//  ww w  .j av a2s  .  c  om

    //Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;

    int scale = 1;
    if (o.outHeight > MAX_SIZE || o.outWidth > MAX_SIZE) {
        scale = (int) Math.pow(2, (int) Math
                .round(Math.log(MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
    }

    //Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    InputStream is = context.getContentResolver().openInputStream(uri);
    b = BitmapFactory.decodeStream(is, null, o2);
    is.close();

    File outputDir = context.getCacheDir();
    File outputFile = File.createTempFile("avatar", ".jpg", outputDir);
    FileOutputStream fos = new FileOutputStream(outputFile);
    b.compress(Bitmap.CompressFormat.JPEG, 80, fos);
    fos.close();

    return outputFile;
}

From source file:Main.java

public static double tile2lat(int y, int aZoom) {

    final double MerkElipsK = 0.0000001;
    final long sradiusa = 6378137;
    final long sradiusb = 6356752;
    final double FExct = (double) Math.sqrt(sradiusa * sradiusa - sradiusb * sradiusb) / sradiusa;
    final int TilesAtZoom = 1 << aZoom;
    double result = (y - TilesAtZoom / 2) / -(TilesAtZoom / (2 * Math.PI));
    result = (2 * Math.atan(Math.exp(result)) - Math.PI / 2) * 180 / Math.PI;
    double Zu = result / (180 / Math.PI);
    double yy = ((y) - TilesAtZoom / 2);

    double Zum1 = Zu;
    Zu = Math.asin(1 - ((1 + Math.sin(Zum1)) * Math.pow(1 - FExct * Math.sin(Zum1), FExct))
            / (Math.exp((2 * yy) / -(TilesAtZoom / (2 * Math.PI)))
                    * Math.pow(1 + FExct * Math.sin(Zum1), FExct)));
    while (Math.abs(Zum1 - Zu) >= MerkElipsK) {
        Zum1 = Zu;/* w  w w  .  j  a v a2s . c om*/
        Zu = Math.asin(1 - ((1 + Math.sin(Zum1)) * Math.pow(1 - FExct * Math.sin(Zum1), FExct))
                / (Math.exp((2 * yy) / -(TilesAtZoom / (2 * Math.PI)))
                        * Math.pow(1 + FExct * Math.sin(Zum1), FExct)));
    }

    result = Zu * 180 / Math.PI;

    return result;

}