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:de.tudarmstadt.ukp.dkpro.keyphrases.bookindexing.evaluation.matchingstrategy.MatchingSmallLevenshtein.java

/**
 * Consumes two strings and returns true if the strings are within a tolerable
 * small Levenshtein-distance.//from  w ww  .j a va  2  s.  c o  m
 *
 * @param s1 string 1
 * @param s2 string 2
 * @return True, if s1 has a tolerable small Levenshtein distance to s2.
 */
public static boolean smallLevenshtein(String s1, String s2) {
    s1 = normalizeString(s1);
    s2 = normalizeString(s2);

    if (Math.abs(s1.length() - s2.length()) > 2) {
        return false;
    }

    String difference = org.apache.commons.lang.StringUtils.difference(s1, s2);

    switch (difference.length()) {
    case 0:
        return true;
    case 1:
        // for english plural
        return difference.equals("s");
    default:
        return false;
    }
}

From source file:Main.java

static String formatDec(float val, int dec) {
    int factor = (int) Math.pow(10, dec);

    int front = (int) (val);
    int back = (int) Math.abs(val * (factor)) % factor;

    return front + "." + back;
}

From source file:Main.java

@SuppressWarnings("unused")
public static int getMatrixRotateDegrees(Matrix matrix) {
    synchronized (MATRIX_VALUES) {
        matrix.getValues(MATRIX_VALUES);
        final float skewX = MATRIX_VALUES[Matrix.MSKEW_X];
        final float scaleX = MATRIX_VALUES[Matrix.MSCALE_X];
        //noinspection SuspiciousNameCombination
        final int degrees = (int) Math.round(Math.atan2(skewX, scaleX) * (180 / Math.PI));
        if (degrees < 0) {
            return Math.abs(degrees);
        } else if (degrees > 0) {
            return 360 - degrees;
        } else {/*  w  w  w . j a v a 2 s  .c  o  m*/
            return 0;
        }
    }
}

From source file:Main.java

public static float blendDifference(float cS, float cB) {
    return Math.abs(cB - cS);
}

From source file:Main.java

/**
 * Determines if the specified coordinate is in the target touch zone for a
 * corner handle./*from  w  w  w  . j a va 2  s.  c o m*/
 * 
 * @param x the x-coordinate of the touch point
 * @param y the y-coordinate of the touch point
 * @param handleX the x-coordinate of the corner handle
 * @param handleY the y-coordinate of the corner handle
 * @param targetRadius the target radius in pixels
 * @return true if the touch point is in the target touch zone; false
 *         otherwise
 */
private static boolean isInCornerTargetZone(float x, float y, float handleX, float handleY,
        float targetRadius) {

    if (Math.abs(x - handleX) <= targetRadius && Math.abs(y - handleY) <= targetRadius) {
        return true;
    }
    return false;
}

From source file:gov.nih.nci.cabig.caaers.utils.DurationUtils.java

public static String formatDuration(long actualDuration, String format) {
    String msgPrefix = "Due in ";
    String msgSuffix = "overdue";
    if (actualDuration < 0) {
        msgPrefix = "";
    } else {// w  w w  .  j  a  va  2  s  .  c o  m
        msgSuffix = "";
    }

    long duration = Math.abs(actualDuration);

    int years = (format.contains("y")) ? Integer.parseInt(DurationFormatUtils.formatDuration(duration, "y"))
            : 0;
    duration -= (years * YEAR);
    int months = (format.contains("M")) ? Integer.parseInt(DurationFormatUtils.formatDuration(duration, "M"))
            : 0;
    duration -= (months * MONTH);
    int days = (format.contains("d")) ? Integer.parseInt(DurationFormatUtils.formatDuration(duration, "d")) : 0;
    duration -= (days * DAY);
    int hours = (format.contains("H")) ? Integer.parseInt(DurationFormatUtils.formatDuration(duration, "H"))
            : 0;
    duration -= (hours * HOUR);
    int minutes = (format.contains("m")) ? Integer.parseInt(DurationFormatUtils.formatDuration(duration, "m"))
            : 0;
    duration -= (minutes * MINUTE);
    int seconds = (format.contains("s")) ? Integer.parseInt(DurationFormatUtils.formatDuration(duration, "s"))
            : 0;

    //apply rounding to days / Hours / minutes
    if (format.contains("d") && format.contains("H")) {
        if (hours > 12) {
            days += 1;
        }
        hours = 0;
    }

    //apply rounding of hours
    if (format.contains("m") && format.contains("H")) {
        if (minutes > 30) {
            hours += 1;
        }
        minutes = 0;
    }

    //apply rounding of minutes
    if (format.contains("m") && format.contains("s")) {
        if (seconds > 30) {
            minutes += 1;
        }
        seconds = 0;
    }

    StringBuffer sb = new StringBuffer(msgPrefix);
    if (years > 0) {
        sb.append(years).append(years > 1 ? " years" : " year").append(" ");
    }
    if (months > 0) {
        sb.append(months).append(months > 1 ? " months" : " month").append(" ");
    }
    if (days > 0) {
        sb.append(days).append(days > 1 ? " days" : " day").append(" ");
    }
    if (hours > 0) {
        sb.append(hours).append(hours > 1 ? " hours" : " hour").append(" ");
    }
    if (minutes > 0) {
        sb.append(minutes).append(minutes > 1 ? " minutes" : " minute").append(" ");
    }
    if (seconds > 0) {
        sb.append(seconds).append(seconds > 1 ? " seconds" : " second").append(" ");
    }
    sb.append(msgSuffix);
    return sb.toString();

}

From source file:Main.java

public static int[] histogram(float[] dataPoints) {

    int[] buckets = { 0, 0, 0, 0, 0 }; // 5 buckets
    float minVal = 1000000.0F;
    float maxVal = -minVal;
    int numOfDataPoints = dataPoints.length;

    // Get the min and max values of the data points
    for (int i = 0; i < numOfDataPoints; i++) {

        minVal = Math.min(minVal, Math.abs(dataPoints[i]));
        maxVal = Math.max(maxVal, Math.abs(dataPoints[i]));
    }/*from   w ww . ja  v a2  s  .  c om*/

    float bucketSize = (maxVal - minVal) / 5;
    // float bucketSize = 0.002F;

    // Count the number of points for each bucket
    for (int i = 0; i < numOfDataPoints; i++) {

        float val = Math.abs(dataPoints[i]);
        if (val <= minVal + bucketSize)
            buckets[0] = buckets[0] + 1; // between minVal and minVal + bucketSize
        else if (val <= minVal + 2 * bucketSize)
            buckets[1] = buckets[1] + 1; // between minVal and minVal + 2* bucketSize
        else if (val <= minVal + 3 * bucketSize)
            buckets[2] = buckets[2] + 1; // between minVal and minVal + 3* bucketSize
        else if (val <= minVal + 4 * bucketSize)
            buckets[3] = buckets[3] + 1; // between minVal and minVal + 4* bucketSize
        else
            buckets[4] = buckets[4] + 1; // greater than minVal + 4* bucketSize
    }
    return buckets;
}

From source file:Main.java

private static double getRawAmplitude(byte[] data, int len) {
    if (len <= 0 || data == null || data.length <= 0) {
        return 0;
    }/*  ww w  . ja  v a  2  s.  c o m*/

    double sum = 0;
    for (int i = 0; i < len; i++) {
        sum += Math.abs(data[i]);
    }
    return sum / len;
}

From source file:Main.java

public static Camera.Size getOptimalPreviewSize(Activity currentActivity, List<Camera.Size> sizes,
        double targetRatio) {
    // Use a very small tolerance because we want an exact match.
    final double ASPECT_TOLERANCE = 0.001;
    if (sizes == null)
        return null;

    Camera.Size optimalSize = null;/* www .  j  a  va2  s  .  c  o  m*/
    double minDiff = Double.MAX_VALUE;

    // Because of bugs of overlay and layout, we sometimes will try to
    // layout the viewfinder in the portrait orientation and thus get the
    // wrong size of preview surface. When we change the preview size, the
    // new overlay will be created before the old one closed, which causes
    // an exception. For now, just get the screen size.
    Point point = getDefaultDisplaySize(currentActivity, new Point());
    int targetHeight = Math.min(point.x, point.y);
    // Try to find an size match aspect ratio and size
    for (Camera.Size size : sizes) {
        double ratio = (double) size.width / size.height;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
            continue;
        if (Math.abs(size.height - targetHeight) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }
    // Cannot find the one match the aspect ratio. This should not happen.
    // Ignore the requirement.
    if (optimalSize == null) {
        minDiff = Double.MAX_VALUE;
        for (Camera.Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }
    return optimalSize;
}

From source file:FloatCmp.java

/** Compare two doubles within a given epsilon */
public static boolean equals(double a, double b, double eps) {
    if (a == b)// ww w .  j  a  va 2 s .  c  o m
        return true;
    // If the difference is less than epsilon, treat as equal.
    return Math.abs(a - b) < eps;
}