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

/**
 * Returns the least common multiple between two integer values.
 * //from w  w w.j av  a2  s  . co m
 * @param a the first integer value.
 * @param b the second integer value.
 * @return the least common multiple between a and b.
 * @throws ArithmeticException if the lcm is too large to store as an int
 * @since 1.1
 */
public static int lcm(int a, int b) {
    return Math.abs(mulAndCheck(a / gcd(a, b), b));
}

From source file:Main.java

/**
 * /*from   ww  w  .  ja va  2 s  .  c om*/
 * Checks indentation (over a single line - multipline text nodes is not supported)
 * 
 * @param out
 * @param indentSize
 * @return
 * @throws Exception
 */

public static boolean isIndented(String out, int indentSize) throws Exception {
    BufferedReader reader = new BufferedReader(new StringReader(out));

    boolean indentated = false;

    int level = 0;
    int line = 0;

    String string = reader.readLine();
    while (string != null) {
        int newLevel = 0;
        while (newLevel < string.length()) {
            if (!Character.isWhitespace(string.charAt(newLevel))) {
                break;
            }
            newLevel++;
        }
        if ((newLevel % indentSize) != 0) {
            throw new IllegalArgumentException("Unexpected " + newLevel + " whitespace chars at line " + line);
        }
        if (Math.abs(level - newLevel) > indentSize) {
            throw new IllegalArgumentException("Unexpected jump from " + level + " to " + newLevel
                    + " whitespace chars at line " + line + " for indenting with " + indentSize + " chars");
        }
        level = newLevel;

        string = reader.readLine();
        line++;

        if (level > 0) {
            indentated = true;
        }
    }

    if (!indentated) {
        // see if a simple xml piece
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        XMLStreamReader parser = inputFactory.createXMLStreamReader(new StringReader(out));

        int elementMaxLevel = -1;
        int elementLevel = 0;
        do {
            int event = parser.next();
            if (event == XMLStreamConstants.START_ELEMENT) {
                elementLevel++;

                if (elementMaxLevel < elementLevel) {
                    elementMaxLevel = elementLevel;
                }
            } else if (event == XMLStreamConstants.END_ELEMENT) {
                elementLevel--;
            }
        } while (parser.hasNext());

        if (elementMaxLevel > 1) { // should be indentated
            return false;
        }
        return true;
    }

    return indentated;
}

From source file:Main.java

public static Size getOptimalVideoSnapshotPictureSize(List<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;

    Size optimalSize = null;/*from   ww w  .  j av  a 2  s  .c  o  m*/

    // Try to find a size matches aspect ratio and has the largest width
    for (Size size : sizes) {
        double ratio = (double) size.width / size.height;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
            continue;
        if (optimalSize == null || size.width > optimalSize.width) {
            optimalSize = size;
        }
    }

    // Cannot find one that matches the aspect ratio. This should not happen.
    // Ignore the requirement.
    if (optimalSize == null) {
        Log.w(TAG, "No picture size match the aspect ratio");
        for (Size size : sizes) {
            if (optimalSize == null || size.width > optimalSize.width) {
                optimalSize = size;
            }
        }
    }
    return optimalSize;
}

From source file:Main.java

public static float computeCurvatureHK2003(float x0, float y0, float x1, float y1, float x2, float y2) {

    double kappa = 0.0D;

    float lB = distanceBetween2Points(x1, y1, x0, y0);
    float lF = distanceBetween2Points(x1, y1, x2, y2);
    float thetaB = (float) Math.atan2(x0 - x1, y0 - y1);
    float thetaF = (float) Math.atan2(x2 - x1, y2 - y1);

    float delta = Math.abs(thetaB - thetaF) / 2;

    kappa = (1 / lB + 1 / lF) * delta / 2;
    return (float) kappa;
}

From source file:Main.java

private static String getDayOfWeek(String format, int calendarField) {
    String strDate = null;/*w  w  w .  ja v  a2  s. c om*/
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        int week = c.get(Calendar.DAY_OF_WEEK);
        if (week == calendarField) {
            strDate = mSimpleDateFormat.format(c.getTime());
        } else {
            int offectDay = calendarField - week;
            if (calendarField == Calendar.SUNDAY) {
                offectDay = 7 - Math.abs(offectDay);
            }
            c.add(Calendar.DATE, offectDay);
            strDate = mSimpleDateFormat.format(c.getTime());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return strDate;
}

From source file:Cache.Servidor.java

public static long hash(String hashKey) {
    int b = 378551;
    int a = 63689;
    long hash = 0;
    for (int i = 0; i < hashKey.length(); i++) {
        hash = hash * a + hashKey.charAt(i);
        a = a * b;//from  w w  w. j a v a 2  s.co m
    }
    return Math.abs(hash);
}

From source file:Main.java

/**
 * Finds the index where an object should be inserted in a collection based
 * on the following algorithm: if the list is empty the insertion index is 0 <br/>
 * If equal element exists, the insertion index is the index of the existing
 * element+1. If multiple equal elements exist, the insertion index is after
 * the elements.//from  w w  w  .  ja  va 2  s .  c o  m
 * 
 * @param <E>
 *            type of the object
 * @param list
 *            list where the element should be inserted.
 * @param value
 *            element that should be inserted.
 * @param comparator
 *            comparator used to compare objects
 * @return index (place) where the element should be inserted.
 */
public static <E> int findInsertionIndex(List<E> list, E object, Comparator<E> comparator) {
    // call binary search
    int binarySearchResult = Collections.binarySearch(list, object, comparator);

    int index;
    if (binarySearchResult < 0) {
        // if the result is negative turn it to positive and decrease it by
        // one (see binarySearch doc)
        index = Math.abs(binarySearchResult) - 1;
    } else {
        // if the result is positive, increase it by one
        index = binarySearchResult + 1;

        // if there are multiple equal elements iterate to find the latest
        while (index < list.size() && comparator.compare(list.get(index), object) == 0) {
            index++;
        }
    }

    return index;
}

From source file:Main.java

public static Camera.Size getBestPreviewSize(List<Camera.Size> sizes, int height, int width) {
    final double ASPECT_TOLERANCE = 0.2;
    double targetRatio = (double) height / width;
    if (sizes == null)
        return null;

    Size optimalSize = null;/*from   www  . ja v  a  2s  .  c  om*/
    double minDiff = Double.MAX_VALUE;

    int targetHeight = height;

    // Try to find an size match aspect ratio and size
    for (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, ignore the
    // requirement
    if (optimalSize == null) {
        minDiff = Double.MAX_VALUE;
        for (Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }
    return optimalSize;
}

From source file:com.opengamma.maths.lowlevelapi.functions.utilities.Abs.java

public static double[] stateless(double[] v) {
    Validate.notNull(v);/*from  w w  w  .  ja  v  a 2 s  .c  o  m*/
    final int n = v.length;
    double[] tmp = new double[n];
    for (int i = 0; i < n; i++) {
        tmp[i] = Math.abs(v[i]);
    }
    return tmp;
}

From source file:Main.java

/**
 * To check if the app has opened ever(most time).
 * //w  ww .j a  v a  2 s. c om
 * @param  context     context
 * @param  packageName package name
 * @return             true if the app has actived, otherwise return false
 */
public static boolean isAppActivated(Context context, String packageName) {
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
        if (packageInfo == null) {
            return false;
        }

        ApplicationInfo appInfo = packageInfo.applicationInfo;
        File file = new File(appInfo.dataDir);

        /* if the /data/data/<packagename> is not existed, return false */
        if (file == null || !file.exists()) {
            return false;
        }

        long lastUpdateTime = packageInfo.lastUpdateTime;
        long lastModifiedTime = file.lastModified();

        /* if the delta time is greater than 1.5s(most time), then the app has activated */
        if (Math.abs(lastModifiedTime - lastUpdateTime) >= 1500) {
            return true;
        }

        return false;
    } catch (NameNotFoundException e) {
        return false;
    }
}