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

@SuppressLint("DefaultLocale")
public static String formatByteString(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:Main.java

/**
 * Calculates the distance between two locations in KM
 *///from   w  w w .  j  av a  2s.c  o  m
public static double checkDistance(double lat1, double lng1, double lat2, double lng2) {
    double earthRadius = 6371;

    double dLat = Math.toRadians(lat2 - lat1);
    double dLng = Math.toRadians(lng2 - lng1);

    double sindLat = Math.sin(dLat / 2);
    double sindLng = Math.sin(dLng / 2);

    double a = Math.pow(sindLat, 2)
            + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));

    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

    double dist = earthRadius * c * 1000;

    return dist; // in meter
}

From source file:Main.java

public static int hexStringToAlgorism(String hex) {
    hex = hex.toUpperCase();/*from  ww w  . ja v a 2 s. c o  m*/
    int max = hex.length();
    int result = 0;
    for (int i = max; i > 0; i--) {
        char c = hex.charAt(i - 1);
        int algorism = 0;
        if (c >= '0' && c <= '9') {
            algorism = c - '0';
        } else {
            algorism = c - 55;
        }
        result += Math.pow(16, max - i) * algorism;
    }
    return result;
}

From source file:Main.java

public static double distanceFrom(double lat1, double lng1, double lat2, double lng2) {
    // Implementation of the Haversine distance formula
    lat1 = Math.toRadians(lat1);/* w  w w  .j a  v a2 s  .c  om*/
    lng1 = Math.toRadians(lng1);
    lat2 = Math.toRadians(lat2);
    lng2 = Math.toRadians(lng2);

    double dlon = lng2 - lng1;
    double dlat = lat2 - lat1;

    double a = Math.pow((Math.sin(dlat / 2)), 2)
            + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon / 2), 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    return 3958.75 * c; // 3958: Earth radius in miles
}

From source file:Main.java

public static int getUPSRuntime(float totalWatts, int vah, int ptare, float bCurve_scale, float bCurve_expo,
        float bCurve_rSlope, float bCurve_comp, int max_runtime) {
    if ((vah <= 0) || (totalWatts < 0) || (ptare < 0) || (totalWatts + ptare == 0) || (bCurve_scale <= 0)
            || (bCurve_expo <= 0) || (bCurve_rSlope < 0)) {
        return 0;
    } else {//from  w ww. j  a  v  a2s .  co  m
        return (int) Math.round(60 * bCurve_scale * (Math.pow(vah / (totalWatts + ptare), bCurve_expo)
                - bCurve_rSlope + bCurve_comp * (totalWatts + ptare) / vah));
    }
}

From source file:Main.java

/**Calculates the estimated distance between the access point based on frequency and signal strength in db. 
 * Based on the Free-space path loss equation at http://en.wikipedia.org/wiki/FSPL
 * //from   w  w  w. j a  va  2 s  .  co  m
 * @param levelInDb
 * @param freqInMHz
 * @return
 */
public static double calculateDistance(double levelInDb, double freqInMHz) {
    double exp = (27.55 - (20 * Math.log10(freqInMHz)) + Math.abs(levelInDb)) / 20.0;
    return Math.pow(10.0, exp);
}

From source file:Main.java

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

From source file:Main.java

public static double relativeLuminance(int rgb) {
    //http://www.w3.org/TR/2007/WD-WCAG20-TECHS-20070517/Overview.html#G18
    double RsRGB = (double) ((rgb >>> 16) & 0xff) / 255.0, GsRGB = (double) ((rgb >>> 8) & 0xff) / 255.0,
            BsRGB = (double) (rgb & 0xff) / 255.0, R, G, B;
    if (RsRGB <= 0.03928)
        R = RsRGB / 12.92;// w w  w  . jav a 2s  .  c o  m
    else
        R = Math.pow((RsRGB + 0.055) / 1.055, 2.4);
    if (GsRGB <= 0.03928)
        G = GsRGB / 12.92;
    else
        G = Math.pow((GsRGB + 0.055) / 1.055, 2.4);
    if (BsRGB <= 0.03928)
        B = BsRGB / 12.92;
    else
        B = Math.pow((BsRGB + 0.055) / 1.055, 2.4);
    return (0.2126 * R) + (0.7152 * G) + (0.0722 * B);
}

From source file:Main.java

public static String converSizeToString(long size) {
    String ext[] = { "B", "KB", "MB", "G", "T", "P" };
    int i = 0;//  w  w  w .  ja  v  a 2s.com
    long duration = size;
    while (duration >= 1024) {
        System.out.println("duration=" + duration + "  i=" + i);
        duration = duration >> 10;
        i++;
        System.out.println("duration=" + duration + "  i=" + i);
    }
    double d = Math.pow(2, 10 * (i));
    java.text.DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(2);
    return df.format(size / d) + ext[i];
}

From source file:Main.java

public static double pointDistance(PointF a, PointF b) {
    return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
}