Example usage for java.lang Math abs

List of usage examples for java.lang Math abs

Introduction

In this page you can find the example usage for java.lang Math abs.

Prototype

@HotSpotIntrinsicCandidate
public static double abs(double a) 

Source Link

Document

Returns the absolute value of a double value.

Usage

From source file:Main.java

public static double fastDistanceMeters(double latRad1, double lngRad1, double latRad2, double lngRad2) {
    if ((Math.abs(latRad1 - latRad2) > RAD_PER_DEG) || (Math.abs(lngRad1 - lngRad2) > RAD_PER_DEG)) {
        return accurateDistanceMeters(latRad1, lngRad1, latRad2, lngRad2);
    }//from  www. j  a va 2s. c o m
    // Approximate sin(x) = x.
    double sineLat = (latRad1 - latRad2);

    // Approximate sin(x) = x.
    double sineLng = (lngRad1 - lngRad2);

    // Approximate cos(lat1) * cos(lat2) using
    // cos((lat1 + lat2)/2) ^ 2
    double cosTerms = Math.cos((latRad1 + latRad2) / 2.0);
    cosTerms = cosTerms * cosTerms;
    double trigTerm = sineLat * sineLat + cosTerms * sineLng * sineLng;
    trigTerm = Math.sqrt(trigTerm);

    // Approximate arcsin(x) = x
    return EARTH_RADIUS_METERS * trigTerm;
}

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 void getEulerAngles(float[] headView, float[] output) {
    float pitch = (float) Math.asin((double) headView[6]);
    float yaw;//from   w  w  w.  ja  v a2s . co  m
    float roll;
    if (Math.abs(headView[6]) < 0.9999999999D) {
        yaw = (float) Math.atan2((double) (-headView[2]), (double) headView[10]);
        roll = (float) Math.atan2((double) (-headView[4]), (double) headView[5]);
    } else {
        yaw = 0.0F;
        roll = (float) Math.atan2((double) headView[1], (double) headView[0]);
    }
    output[0] = -pitch;
    output[1] = -yaw;
    output[2] = -roll;
    float pitchAngle = (float) Math.toDegrees(output[0]);
    float yawAngle = (float) Math.toDegrees(output[1]);
    float rollAngle = (float) Math.toDegrees(output[2]);

    Log.e(TAG, String.format("pitchAngle=%f, yawAngle=%f, rollAngle=%f", pitchAngle, yawAngle, rollAngle));
}

From source file:Main.java

static String formatLatLon(double latLon, char positiveDirection, char negativeDirection) {
    boolean isPositive = (latLon >= 0.0);
    double absLatLon = Math.abs(latLon);
    if (absLatLon > 180.0) {
        return "?";
    }/*from  ww  w  . j  av  a2s  .  com*/
    double degrees = Math.floor(absLatLon);
    double degreeRemainder = absLatLon - degrees;
    double minutesAndSeconds = 60.0 * degreeRemainder;
    double minutes = Math.floor(minutesAndSeconds);
    double minuteRemainder = minutesAndSeconds - minutes;
    double seconds = 60.0 * minuteRemainder;

    return String.format(Locale.US, "%.0f\u00b0 %.0f\' %.3f\" %c", degrees, minutes, seconds,
            isPositive ? positiveDirection : negativeDirection);

}

From source file:Main.java

public static void abs(double[] input) {
    for (int i = 0; i < input.length; i++) {
        input[i] += Math.abs(input[i]);
    }//  www  . j  a  v a2s . c om
}

From source file:Main.java

public static int HSLtoRGB(float[] hsl) {
    final float h = hsl[0];
    final float s = hsl[1];
    final float l = hsl[2];

    final float c = (1f - Math.abs(2 * l - 1f)) * s;
    final float m = l - 0.5f * c;
    final float x = c * (1f - Math.abs((h / 60f % 2f) - 1f));

    final int hueSegment = (int) h / 60;

    int r = 0, g = 0, b = 0;

    switch (hueSegment) {
    case 0://  w  w  w  .jav  a2s.  c o  m
        r = Math.round(255 * (c + m));
        g = Math.round(255 * (x + m));
        b = Math.round(255 * m);
        break;
    case 1:
        r = Math.round(255 * (x + m));
        g = Math.round(255 * (c + m));
        b = Math.round(255 * m);
        break;
    case 2:
        r = Math.round(255 * m);
        g = Math.round(255 * (c + m));
        b = Math.round(255 * (x + m));
        break;
    case 3:
        r = Math.round(255 * m);
        g = Math.round(255 * (x + m));
        b = Math.round(255 * (c + m));
        break;
    case 4:
        r = Math.round(255 * (x + m));
        g = Math.round(255 * m);
        b = Math.round(255 * (c + m));
        break;
    case 5:
    case 6:
        r = Math.round(255 * (c + m));
        g = Math.round(255 * m);
        b = Math.round(255 * (x + m));
        break;
    }

    r = Math.max(0, Math.min(255, r));
    g = Math.max(0, Math.min(255, g));
    b = Math.max(0, Math.min(255, b));

    return Color.rgb(r, g, b);
}

From source file:MathUtils.java

public static int pgcd(int u, int v) {
    if (u * v == 0) {
        return (Math.abs(u) + Math.abs(v));
    }/*  w w w. ja v a 2  s  . c  o  m*/
    // keep u and v negative, as negative integers range down to
    // -2^31, while positive numbers can only be as large as 2^31-1
    // (i.e. we can't necessarily negate a negative number without
    // overflow)
    /* assert u!=0 && v!=0; */
    if (u > 0) {
        u = -u;
    } // make u negative
    if (v > 0) {
        v = -v;
    } // make v negative
      // B1. [Find power of 2]
    int k = 0;
    while ((u & 1) == 0 && (v & 1) == 0 && k < 31) { // while u and v are
        // both even...
        u /= 2;
        v /= 2;
        k++; // cast out twos.
    }
    if (k == 31) {
        throw new ArithmeticException("overflow: gcd is 2^31");
    }
    // B2. Initialize: u and v have been divided by 2^k and at least
    // one is odd.
    int t = ((u & 1) == 1) ? v : -(u / 2)/* B3 */;
    // t negative: u was odd, v may be even (t replaces v)
    // t positive: u was even, v is odd (t replaces u)
    do {
        /* assert u<0 && v<0; */
        // B4/B3: cast out twos from t.
        while ((t & 1) == 0) { // while t is even..
            t /= 2; // cast out twos
        }
        // B5 [reset max(u,v)]
        if (t > 0) {
            u = -t;
        } else {
            v = t;
        }
        // B6/B3. at this point both u and v should be odd.
        t = (v - u) / 2;
        // |u| larger: t positive (replace u)
        // |v| larger: t negative (replace v)
    } while (t != 0);
    return -u * (1 << k); // gcd is u*2^k
}

From source file:Main.java

public static long getNumberOfDaysPassed(long date1, long date2) {
    Time sThenTime = new Time();
    sThenTime.set(date1);/*from www. j a v  a  2  s. c o m*/
    int day1 = Time.getJulianDay(date1, sThenTime.gmtoff);
    sThenTime.set(date2);
    int day2 = Time.getJulianDay(date2, sThenTime.gmtoff);
    return Math.abs(day2 - day1);
}

From source file:Main.java

public static Date getNearestDate(List<Date> dates, Date currentDate) {
    long minDiff = -1;
    long currentTime = currentDate.getTime();
    Date minDate = null;//from  w ww .  java2s  .c  om
    for (Date date : dates) {
        long diff = Math.abs(currentTime - date.getTime());
        if ((minDiff == -1) || (diff < minDiff)) {
            minDiff = diff;
            minDate = date;
        }
    }
    return minDate;
}

From source file:Main.java

public static int compareSpan(int colorOld, int colorLast) {
    int or = Color.red(colorOld);
    int og = Color.green(colorOld);
    int ob = Color.blue(colorOld);

    int lr = Color.red(colorLast);
    int lg = Color.green(colorLast);
    int lb = Color.blue(colorLast);
    return (Math.abs(or - lr) + Math.abs(og - lg) + Math.abs(ob - lb)) / 3;
}