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

/** Attempts to find the camera preview size as close as possible to the given width and height. If the Android API
 * does not support retrieving available camera preview sizes, this method returns null. Otherwise, returns the
 * camera preview size that minimizes the sum of the differences between the actual and requested height and width.
 *//*from   w  w w .  j a va2s  . c  om*/
public static Camera.Size bestCameraSizeForWidthAndHeight(Camera.Parameters params, int width, int height) {
    List<Camera.Size> previewSizes = previewSizesForCameraParameters(params);
    if (previewSizes == null || previewSizes.size() == 0)
        return null;

    Camera.Size bestSize = null;
    int bestDiff = 0;
    // find the preview size that minimizes the difference between width and height
    for (Camera.Size size : previewSizes) {
        int diff = Math.abs(size.width - width) + Math.abs(size.height - height);
        if (bestSize == null || diff < bestDiff) {
            bestSize = size;
            bestDiff = diff;
        }
    }
    return bestSize;
}

From source file:com.hemou.android.util.StrUtils.java

/**
 * Get relative time for date/*from ww w .ja  v  a 2 s.co m*/
 * 
 * @param date
 * @return relative time
 */
public static CharSequence getRelativeTime(final Date date) {
    if (date == null)
        return "--";
    long now = System.currentTimeMillis();
    if (Math.abs(now - date.getTime()) > 60000)
        return DateUtils.getRelativeTimeSpanString(date.getTime(), now, MINUTE_IN_MILLIS,
                FORMAT_SHOW_DATE | FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE);
    else
        return "";
}

From source file:ColorHelper.java

public int getColorIndexFor(String ident) {
    return Math.abs(ident.hashCode()) % staticColors.size();
}

From source file:libra.core.kmersimilarity_r.KmerSimilarityPartitioner.java

@Override
public int getPartition(CompressedSequenceWritable key, CompressedIntArrayWritable value, int numReduceTasks) {
    return Math.abs(key.getSequence().hashCode()) % numReduceTasks;
}

From source file:com.cloudera.science.ml.parallel.covariance.CoMoment.java

private static boolean approx(double a, double b) {
    return Math.abs(a - b) < 1e-6;
}

From source file:com.opengamma.analytics.math.minimization.GoldenSectionMinimizer1D.java

public double minimize(final Function1D<Double, Double> f, final double lower, final double upper) {
    Validate.notNull(f, "function");
    double x0, x1, x2, x3, f1, f2, temp;
    int i = 0;//from   w ww  .  j  a  v  a  2s. c  om
    final double[] triplet = BRACKETER.getBracketedPoints(f, lower, upper);
    x0 = triplet[0];
    x3 = triplet[2];
    if (Math.abs(triplet[2] - triplet[1]) > Math.abs(triplet[1] - triplet[0])) {
        x1 = triplet[1];
        x2 = triplet[2] + GOLDEN * (triplet[1] - triplet[2]);
    } else {
        x2 = triplet[1];
        x1 = triplet[0] + GOLDEN * (triplet[1] - triplet[0]);
    }
    f1 = f.evaluate(x1);
    f2 = f.evaluate(x2);
    while (Math.abs(x3 - x0) > EPS * (Math.abs(x1) + Math.abs(x2))) {
        if (f2 < f1) {
            temp = GOLDEN * (x2 - x3) + x3;
            x0 = x1;
            x1 = x2;
            x2 = temp;
            f1 = f2;
            f2 = f.evaluate(temp);
        } else {
            temp = GOLDEN * (x1 - x0) + x0;
            x3 = x2;
            x2 = x1;
            x1 = temp;
            f2 = f1;
            f1 = f.evaluate(temp);
        }
        i++;
        if (i > MAX_ITER) {
            throw new MathException(
                    "Could not find minimum: this should not happen because minimum should have been successfully bracketed");
        }
    }
    if (f1 < f2) {
        return x1;
    }
    return x2;
}

From source file:com.opengamma.analytics.math.ComplexMathUtils.java

public static ComplexNumber divide(final ComplexNumber z1, final ComplexNumber z2) {
    ArgumentChecker.notNull(z1, "z1");
    ArgumentChecker.notNull(z2, "z2");
    final double a = z1.getReal();
    final double b = z1.getImaginary();
    final double c = z2.getReal();
    final double d = z2.getImaginary();
    if (Math.abs(c) > Math.abs(d)) {
        final double dOverC = d / c;
        final double denom = c + d * dOverC;
        return new ComplexNumber((a + b * dOverC) / denom, (b - a * dOverC) / denom);
    }/* w ww  .  ja  v  a2  s .  c  o m*/
    final double cOverD = c / d;
    final double denom = c * cOverD + d;
    return new ComplexNumber((a * cOverD + b) / denom, (b * cOverD - a) / denom);
}

From source file:com.opengamma.analytics.financial.timeseries.analysis.DifferenceSignIIDHypothesis.java

@Override
public boolean testIID(final DoubleTimeSeries<?> x) {
    Validate.notNull(x, "x");
    final double[] data = x.valuesArrayFast();
    final int n = data.length;
    int t = 0;/*  w  ww .  ja v  a  2s  .com*/
    for (int i = 1; i < n; i++) {
        if (data[i] > data[i - 1]) {
            t++;
        }
    }
    final double mean = (n - 1) / 2.;
    final double std = Math.sqrt((n + 1) / 12.);
    return Math.abs(t - mean) / std < _criticalValue;
}

From source file:com.liferay.mobile.android.ServiceContextTest.java

@Test
public void addBookmarkEntry() throws Exception {
    Random random = new Random();
    String uuid = String.valueOf(Math.abs(random.nextInt()));

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("uuid", uuid);
    jsonObject.put("addGroupPermissions", true);
    jsonObject.put("addGuestPermissions", true);
    JSONObjectWrapper serviceContext = new JSONObjectWrapper(jsonObject);

    JSONObject entry = addBookmarkEntry("test", serviceContext);

    assertEquals(uuid, entry.getString("uuid"));

    deleteBookmarkEntry(entry);/*from   www  .  j av  a  2s  .  c om*/
}

From source file:net.minecraftforge.client.model.pipeline.LightUtil.java

public static EnumFacing toSide(float x, float y, float z) {
    if (Math.abs(x) > Math.abs(y)) {
        if (Math.abs(x) > Math.abs(z)) {
            if (x < 0)
                return EnumFacing.WEST;
            return EnumFacing.EAST;
        } else {/*from ww w  .j a va 2s.c o  m*/
            if (z < 0)
                return EnumFacing.NORTH;
            return EnumFacing.SOUTH;
        }
    } else {
        if (Math.abs(y) > Math.abs(z)) {
            if (y < 0)
                return EnumFacing.DOWN;
            return EnumFacing.UP;
        } else {
            if (z < 0)
                return EnumFacing.NORTH;
            return EnumFacing.SOUTH;
        }
    }
}