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

/** Rounding errors might make equal floating point numbers seem to differ. */
public static boolean approxEquals(double a, double b) {
    return (Math.abs(a - b) < APPROX_EQUALS_PRECISION);
}

From source file:Main.java

private static TimeDiff getDiff(final Calendar calOne, final Calendar calTwo) {
    return new TimeDiff(Math.abs(calOne.getTimeInMillis() - calTwo.getTimeInMillis()));
}

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

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

From source file:Main.java

public static Camera.Size getOptimalCameraPreviewSize(Camera camera, int width, int height) {
    if (camera == null) {
        return null;
    }/*from   www .j av a2  s . c  o m*/

    double targetRatio = (double) height / width;

    Camera.Parameters params = camera.getParameters();
    List<Camera.Size> sizes = params.getSupportedPreviewSizes();

    Camera.Size optimalSize = null;

    // selecting optimal camera preview size
    int minDiff = Integer.MAX_VALUE;
    for (Camera.Size size : sizes) {
        double ratio = (double) size.height / size.width;
        if (Math.abs(ratio - targetRatio) > RATIO_TOLERANCE)
            continue;
        if (Math.abs(size.height - height) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - height);
        }
    }

    if (optimalSize == null) {
        minDiff = Integer.MAX_VALUE;
        for (Camera.Size size : sizes) {
            if (Math.abs(size.height - height) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - height);
            }
        }
    }
    return optimalSize;
}

From source file:ReversePolishNotation.java

/**
 * This method tests whether the calculated answer is within the acceptable
 * bounds of the actual answer to be correct.
 * //from w  ww .j a  va 2  s  . c  o  m
 * @param actAnswer
 *            is the correct answer to the RPN equation
 * @param calcAnswer
 *            is the calculated answer to the RPN from calcRPN
 */
public static void checkPrecision(Double actAnswer, Double calcAnswer) {
    if (Math.abs(calcAnswer - actAnswer) < 0.000001) {
        return;
    } else {
        System.out.println("The calculated answer was not within 0.000001 of the actual answer.");
    }
}

From source file:Main.java

/**
 * Returns a 'unique' pseudo random material color for given object
 *
 * @param key the key for which the color should be generated
 * @return the color related to the key//from   www .j a va 2  s .  c  o  m
 * @link http://stackoverflow.com/a/29549014
 */
public static int getMaterialColor(Object key) {
    return materialColors.get(Math.abs(key.hashCode()) % materialColors.size());
}

From source file:Main.java

public static Timestamp subtractTimeZoneOffset(Timestamp timestamp, TimeZone timeZone) {
    final long millisecondsToSubtract = Math.abs(timeZone.getOffset(timestamp.getTime()));
    return new Timestamp(timestamp.getTime() - millisecondsToSubtract);
}

From source file:Main.java

private static double transformLon(double x, double y) {
    double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
    ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
    ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;

    return ret;/*from   w w  w  .j a v  a2 s . co m*/
}

From source file:Main.java

private static double transformLat(double x, double y) {
    double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
    ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
    ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;

    return ret;/*w  w w . ja  v a2  s  . c o m*/
}

From source file:Main.java

public static boolean setOptimalCameraPreviewSize(Camera camera, int width, int height) {
    if (camera == null) {
        return false;
    }/*w w  w . j  ava  2  s  .co  m*/

    double targetRatio = (double) height / width;

    Camera.Parameters params = camera.getParameters();
    List<Camera.Size> sizes = params.getSupportedPreviewSizes();

    Camera.Size optimalSize = null;

    // selecting optimal camera preview size
    int minDiff = Integer.MAX_VALUE;
    for (Camera.Size size : sizes) {
        double ratio = (double) size.height / size.width;
        if (Math.abs(ratio - targetRatio) > RATIO_TOLERANCE)
            continue;
        if (Math.abs(size.height - height) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - height);
        }
    }

    if (optimalSize == null) {
        minDiff = Integer.MAX_VALUE;
        for (Camera.Size size : sizes) {
            if (Math.abs(size.height - height) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - height);
            }
        }
    }

    params.setPreviewSize(optimalSize.width, optimalSize.height);

    List<String> focusModes = params.getSupportedFocusModes();
    if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
    }

    // for Mi3, only support ImageFormat.NV21
    // for most cameras, ImageFormat.NV21 are support worldwide
    // so use default preview format
    // params.setPreviewFormat(ImageFormat.JPEG);
    camera.setParameters(params);
    return true;
}