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 String readableFileSize(long size) {
    if (size <= 0)
        return "0";
    final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
    int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
    return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}

From source file:Main.java

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

From source file:Main.java

public static String formatBytes(long bytes) {
    int unit = 1024;
    if (bytes < unit)
        return bytes + " B";
    int exp = (int) (Math.log(bytes) / Math.log(unit));
    String pre = ("KMGTPE").charAt(exp - 1) + "";
    return String.format(Locale.ENGLISH, "%.1f %sB", bytes / Math.pow(unit, exp), pre);
}

From source file:Main.java

public static double haversine(double lat1, double lon1, double lat2, double lon2) {
    final double R = 6372.8; // In kilometers

    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lon2 - lon1);
    lat1 = Math.toRadians(lat1);//from  ww w .  j a va 2  s. c  o m
    lat2 = Math.toRadians(lat2);

    double a = Math.pow(Math.sin(dLat / 2), 2)
            + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2);
    double c = 2 * Math.asin(Math.sqrt(a));
    return R * c;
}

From source file:Main.java

public static float round(float sfFloat, int digits) {
    // Avoiding pow calcs.
    int multi;/*w w w  .  j av  a2 s.  c  o  m*/
    if (digits < s_arPows.length) {
        multi = s_arPows[digits];
    } else {
        multi = (int) Math.pow(10, digits);
    }

    return (float) Math.round(sfFloat * multi) / multi;
}

From source file:Main.java

public static double getDistance(double lat1, double lng1, double lat2, double lng2) {
    double radLat1 = rad(lat1);
    double radLat2 = rad(lat2);
    double a = radLat1 - radLat2;
    double b = rad(lng1) - rad(lng2);
    double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((double) a / 2), 2)
            + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * EARTH_RADIUS;//  w  w  w. j a v  a 2 s .c om
    return s;
}

From source file:Main.java

public static boolean equalsTo(double obj, double value, int precision) {
    return Math.abs(obj - value) <= Math.pow(0.1, precision);
}

From source file:Main.java

public static double GetDistance(double lat1, double lng1, double lat2, double lng2) {
    double radLat1 = rad(lat1);
    double radLat2 = rad(lat2);
    double a = radLat1 - radLat2;
    double b = rad(lng1) - rad(lng2);

    double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
            + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * EARTH_RADIUS;// w w w .  j a  v a  2 s .  c o m
    s = Math.round(s * 10000) / 10000;
    return s;
}

From source file:Main.java

/**
 * rounds the given number to the next significant number
 *
 * @param number//  ww  w.  j  a  va2s . com
 * @return
 */
public static float roundToNextSignificant(double number) {
    final float d = (float) Math.ceil((float) Math.log10(number < 0 ? -number : number));
    final int pw = 1 - (int) d;
    final float magnitude = (float) Math.pow(10, pw);
    final long shifted = Math.round(number * magnitude);
    return shifted / magnitude;
}

From source file:Main.java

public static double round(double dbl, int decimals) {
    if (decimals > 8)
        decimals = 8;/*  w w w .j  a  v a  2  s.c o m*/
    double base = Math.pow(10, decimals);
    int ix = (int) (Math.round(dbl * base));
    return ix / base;
}