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 String formatDegToHms(double degrees, double min, double max) {
    String s = "";
    // normalize values within the given range
    double hours = normalizeAngle(degrees, min, max) / 15.0;
    // take sign into account
    if (hours < 0) {
        hours = Math.abs(hours);
        s += "-";
    }//  w ww  .j a v a2  s .  c o m
    // calculate minutes and seconds;
    double minutes = (hours - (int) hours) * 60.0;
    double seconds = (minutes - (int) minutes) * 60.0;
    s += new DecimalFormat("00").format((int) hours) + "h" + new DecimalFormat("00").format((int) minutes) + "m"
            + new DecimalFormat("00.000").format(seconds) + "s";
    return s;
}

From source file:Main.java

/**
 * Returns the azimuthal difference angle between view and sun directions.
 *
 * @param vaa the view azimuth angle (degree).
 * @param saa the sun azimuth angle (degree).
 *
 * @return the azimuthal difference angle (degree).
 *//*from  w  ww .j  a v a 2  s.  c  o m*/
public static double getAzimuthalDifferenceAngle(double vaa, double saa) {
    final double ada = Math.abs(vaa - saa);

    if (ada > 180.0) {
        return 360.0 - ada;
    }

    return ada;
}

From source file:com.dhenton9000.selenium.generic.UtilMethods.java

public static String generateRandomName(String rootName) {
    Random r = new Random();
    String randomStr = Long.toString(Math.abs(r.nextLong()), 21);
    String randomName = rootName + "_" + randomStr.substring(0, 4);
    return randomName;
}

From source file:Main.java

/**
 * Computes a reasonable number of labels for a data range.
 * /*from w w  w . ja v a  2 s  .com*/
 * @param start start value
 * @param end final value
 * @param approxNumLabels desired number of labels
 * @return double[] array containing {start value, end value, increment}
 */
private static double[] computeLabels(final double start, final double end, final int approxNumLabels,
        double minimumJump) {
    if (Math.abs(start - end) < 0.0000001f) {
        return new double[] { start, start, 0 };
    }
    double s = start;
    double e = end;
    boolean switched = false;
    if (s > e) {
        switched = true;
        double tmp = s;
        s = e;
        e = tmp;
    }
    double xStep = roundUp(Math.max(Math.abs(s - e) / approxNumLabels, minimumJump));
    // Compute x starting point so it is a multiple of xStep.
    double xStart = xStep * Math.ceil(s / xStep);
    double xEnd = xStep * Math.floor(e / xStep);
    if (switched) {
        return new double[] { xEnd, xStart, -1.0 * xStep };
    }
    return new double[] { xStart, xEnd, xStep };
}

From source file:com.ms.commons.test.tool.UnitTestTools.java

public static Long nextLong() {
    return Math.abs(RandomUtils.nextLong());
}

From source file:Main.java

/**
 * Generates a random word.//from  w  ww .j  av a  2s.  c  o  m
 */
public static String generateWord(final Random random, final int[] codePointSet) {
    StringBuilder builder = new StringBuilder();
    // 8 * 4 = 32 chars max, but we do it the following way so as to bias the random toward
    // longer words. This should be closer to natural language, and more importantly, it will
    // exercise the algorithms in dicttool much more.
    final int count = 1 + (Math.abs(random.nextInt()) % 5) + (Math.abs(random.nextInt()) % 5)
            + (Math.abs(random.nextInt()) % 5) + (Math.abs(random.nextInt()) % 5)
            + (Math.abs(random.nextInt()) % 5) + (Math.abs(random.nextInt()) % 5)
            + (Math.abs(random.nextInt()) % 5) + (Math.abs(random.nextInt()) % 5);
    while (builder.length() < count) {
        builder.appendCodePoint(codePointSet[Math.abs(random.nextInt()) % codePointSet.length]);
    }
    return builder.toString();
}

From source file:StringUtils.java

public static String displayBytesSize(final long n) {
    final String size;
    final long abs = Math.abs(n);
    if (abs < KBYTES) {
        size = n + " bytes";
    } else if (abs < MBYTES) {
        size = String.format("%.2f", n / KBYTES) + " kB";
    } else if (abs < GBYTES) {
        size = String.format("%.2f", n / MBYTES) + " MB";
    } else {/* w  w  w .j  a  va2  s .  c  o  m*/
        return String.format("%.2f", n / GBYTES) + " GB";
    }
    return size;
}

From source file:Main.java

/**
 * Special crop of bitmap rotated by not stright angle, in this case the original crop bitmap contains parts
 * beyond the required crop area, this method crops the already cropped and rotated bitmap to the final
 * rectangle.<br>/*www  .j a  v  a  2s  .c om*/
 * Note: rotating by 0, 90, 180 or 270 degrees doesn't require extra cropping.
 */
private static Bitmap cropForRotatedImage(Bitmap bitmap, float[] points, Rect rect, int degreesRotated,
        boolean fixAspectRatio, int aspectRatioX, int aspectRatioY) {
    if (degreesRotated % 90 != 0) {

        int adjLeft = 0, adjTop = 0, width = 0, height = 0;
        double rads = Math.toRadians(degreesRotated);
        int compareTo = degreesRotated < 90 || (degreesRotated > 180 && degreesRotated < 270) ? rect.left
                : rect.right;
        for (int i = 0; i < points.length; i += 2) {
            if (points[i] >= compareTo - 1 && points[i] <= compareTo + 1) {
                adjLeft = (int) Math.abs(Math.sin(rads) * (rect.bottom - points[i + 1]));
                adjTop = (int) Math.abs(Math.cos(rads) * (points[i + 1] - rect.top));
                width = (int) Math.abs((points[i + 1] - rect.top) / Math.sin(rads));
                height = (int) Math.abs((rect.bottom - points[i + 1]) / Math.cos(rads));
                break;
            }
        }

        rect.set(adjLeft, adjTop, adjLeft + width, adjTop + height);
        if (fixAspectRatio) {
            fixRectForAspectRatio(rect, aspectRatioX, aspectRatioY);
        }

        Bitmap bitmapTmp = bitmap;
        bitmap = Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height());
        if (bitmapTmp != bitmap) {
            bitmapTmp.recycle();
        }
    }
    return bitmap;
}

From source file:Main.java

public static char[] createSequence(char begin, char end) {
    int sign = end > begin ? 1 : -1;
    int len = Math.abs(end - begin) + 1;
    char[] seq = new char[len];
    for (int i = 0; i < len; i++) {
        seq[i] = (char) (begin + i * sign);
    }//from   w w w  .  j  av  a  2  s.  c  o  m
    return seq;
}

From source file:Main.java

public static String getMyDeviceId(Context context) {
    try {/*from   w ww  .  j  a  v a2 s.  c om*/
        WifiManager wifiMan = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiMan.getConnectionInfo();

        return Integer.toString(Math.abs(wifiInfo.getMacAddress().hashCode()));
    } catch (Exception e) {
        return "000000000000000000000";
    }

}