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 float pow(float a, float b) {
    return (float) Math.pow(a, b);
}

From source file:Main.java

/**
 * Function calculates the distance from a given RSSI value.
 *      RSSI                = -(10 * n)log_10(distance) - A     | + A
 *      RSSI - A            = -10n * log_10(distance)           | / -10n
 *      (RSSI - A) / -10N   = log_10(distance)
 *      10 ^(RSSI - A) / -10N) = distance
 *
 * @param currentRSSI   live value from the receiver.
 * @param A             Fix value at 1m distance.
 * @param n             Signal propagation constant.
 * @return              Distance in m.//from   ww  w  . ja  va2s.  com
 */
public static double distance(double currentRSSI, double A, double n) {
    double exp = (currentRSSI - A) / (-10 * n);

    return Math.pow(10, exp);
}

From source file:Main.java

public static Bitmap decodeFile(File f, int maxSize) {
    Bitmap b = null;/*from   w  ww . jav a 2  s . co m*/
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        FileInputStream fis = new FileInputStream(f);
        BitmapFactory.decodeStream(fis, null, o);
        fis.close();

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

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        fis = new FileInputStream(f);
        b = BitmapFactory.decodeStream(fis, null, o2);
        fis.close();
    } catch (IOException e) {
    }
    return b;
}

From source file:Main.java

/**
 * Calculates the accuracy of an RSSI reading.
 * <p/>/*from  www .  j a v a  2s.c  o m*/
 * The code was taken from <a href="http://stackoverflow.com/questions/20416218/understanding-ibeacon-distancing" /a>
 *
 * @param txPower the calibrated TX power of an iBeacon
 * @param rssi    the RSSI value of the iBeacon
 * @return the calculated Accuracy
 */
public static double calculateAccuracy(final int txPower, final double rssi) {
    if (rssi == 0) {
        return -1.0; // if we cannot determine accuracy, return -1.
    }

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

From source file:Main.java

public static String regLargerThanInt(int num, boolean include) {
    String front = "";
    String back = "";
    if (include) {
        front = "(";
        back = ")";
    } else {//from  w w w  .  j a  v  a2  s  . c om
        front = "(?<=\\D|\\b|0)(";
        back = ")(?=\\D|\\b)";
    }
    int cou = String.valueOf(num).length();
    String reg = "";
    String longer = "[1-9]\\d{" + cou + ",}";
    reg += longer + "|";

    if (cou > 1) {
        int k1 = (int) ((num % Math.pow(10, cou)) / Math.pow(10, cou - 1));
        String largerThanK = null;
        if (k1 == 8) {
            largerThanK = "9" + "\\d{" + (cou - 1) + "}";
        } else if (k1 < 8) {
            largerThanK = "[" + (k1 + 1) + "-9]" + "\\d{" + (cou - 1) + "}";
        }

        if (largerThanK != null) {
            reg += largerThanK + "|";
        }
        for (int i = cou - 1; i > 0; i--) {
            int head = (int) (num / Math.pow(10, i));
            int tail = (int) ((num % Math.pow(10, i)) / Math.pow(10, i - 1));
            String top = null;
            if (tail == 8) {
                top = head + "9" + (i == 1 ? "" : "\\d{" + (i - 1) + "}");
            } else if (tail < 8) {
                top = head + "[" + (tail + 1) + "-9]" + (i == 1 ? "" : "\\d{" + (i - 1) + "}");
            }
            if (top != null) {
                reg += top + "|";
            }
        }
    } else {
        String lar = "";
        if (num == 8) {
            lar = "9";
        } else if (num < 8) {
            lar = "[" + (num + 1) + "-9]";
        }
        reg += lar;
    }

    if (reg.lastIndexOf("|") == reg.length() - 1) {
        reg = reg.substring(0, reg.length() - 1);
    }
    reg = front + reg + back;
    return reg;
}

From source file:Main.java

public static Float getFloatValue(byte[] value, int format, int position) {
    if (value == null)
        return null;
    if (position + (format & 0xF) > value.length)
        return null;
    int i;//  w ww.j  av  a  2s  .  c  o m
    int mantissa;
    int exponent;
    switch (format) {
    case FORMAT_SFLOAT:
        i = value[(position + 1)];
        position = value[position];
        mantissa = signed((position & 0xFF) + ((i & 0xFF & 0xF) << 8), 12);
        exponent = signed((i & 0xFF) >> 4, 4);
        return Float.valueOf((float) (mantissa * Math.pow(10.0D, exponent)));
    case FORMAT_FLOAT:
        exponent = value[(position + 3)];
        mantissa = value[(position + 2)];
        i = value[(position + 1)];
        position = value[position];
        return Float.valueOf(
                (float) ((format = signed((position & 0xFF) + ((i & 0xFF) << 8) + ((mantissa & 0xFF) << 16),
                        24)) * Math.pow(10.0D, exponent)));
    }
    return null;
}

From source file:Main.java

public static String getReadableSize(long bytes) {
    if (bytes <= 0)
        return "0";
    final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
    int digitGroups = (int) (Math.log10(bytes) / Math.log10(1024));
    return new DecimalFormat("#,##0.#").format(bytes / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}

From source file:Main.java

/**
 * Rounds a number to a given number of significant decimal digits.
 * Note that the number will be left with *only* this number of
 * significant digits regardless of magnitude, e.g. 12345 to 3 digits
 * will be 12300, whereas 0.12345 will be 0.123.
 *
 * @param value the value to round off.//from  w  w  w  .ja  v a 2 s.com
 * @param n     the number of significant decimal digits desired.
 * @return a rounded off number.
 */
public static double roundToSignificantDigits(double value, int n) {
    if (value == 0.0) {
        return 0.0;
    }

    final double d = Math.ceil(Math.log10(value < 0.0 ? -value : value));
    final int power = n - (int) d;

    final double magnitude = Math.pow(10.0, power);
    final long shifted = Math.round(value * magnitude);
    return shifted / magnitude;
}

From source file:lsafunctions.LSA.java

public static double cosinSim(int v1, int v2, RealMatrix Vt) {
    double sim = 0.0;
    double sumNum = 0.0;
    double fdenom = 0.0;
    double sdenom = 0.0;

    for (int j = 0; j < Vt.getRowDimension(); j++) {
        sumNum += Vt.getEntry(j, v1) * Vt.getEntry(j, v2);
        fdenom += Math.pow(Vt.getEntry(j, v1), 2);
        sdenom += Math.pow(Vt.getEntry(j, v2), 2);
    }/*from   w w w.  j  a v a 2s . com*/
    sim = sumNum / (Math.sqrt(fdenom) * Math.sqrt(sdenom));

    return sim;
}

From source file:Main.java

public static float getDistanceBetween2Points(PointF p0, PointF p1) {
    float distance = (float) Math.sqrt(Math.pow(p0.y - p1.y, 2) + Math.pow(p0.x - p1.x, 2));
    return distance;
}