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 int getFreeMemory(String path) {
    StatFs statFs = new StatFs(path);
    int free = (statFs.getAvailableBlocks() * statFs.getBlockSize());
    return Math.abs(free);
}

From source file:Points.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;

    g2d.setColor(Color.red);//from w ww. ja  va  2  s  .c  om

    for (int i = 0; i <= 100000; i++) {
        Dimension size = getSize();
        int w = size.width;
        int h = size.height;

        Random r = new Random();
        int x = Math.abs(r.nextInt()) % w;
        int y = Math.abs(r.nextInt()) % h;
        g2d.drawLine(x, y, x, y);
    }
}

From source file:Main.java

public static float distanceBetween2Points(float x1, float y1, float x2, float y2) {

    return PointF.length(Math.abs(x1 - x2), Math.abs(y1 - y2));
}

From source file:FloatCmp.java

/** Compare two doubles, using default epsilon */
public static boolean equals(double a, double b) {
    if (a == b)/*from w ww.  jav  a2  s . c o m*/
        return true;
    // If the difference is less than epsilon, treat as equal.
    return Math.abs(a - b) < EPSILON * Math.max(Math.abs(a), Math.abs(b));
}

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

public static void inPlace(int[] v) {
    Validate.notNull(v);/* w ww . j a  va  2 s  . co  m*/
    final int n = v.length;
    for (int i = 0; i < n; i++) {
        v[i] = Math.abs(v[i]);
    }
}

From source file:PiInterrupt.java

private void calcPi(double accuracy) throws InterruptedException {

    latestPiEstimate = 0.0;//from  w  ww.  j  ava2  s.  c  om
    long iteration = 0;
    int sign = -1;

    while (Math.abs(latestPiEstimate - Math.PI) > accuracy) {

        if (Thread.interrupted()) {
            throw new InterruptedException();
        }

        iteration++;
        sign = -sign;
        latestPiEstimate += sign * 4.0 / ((2 * iteration) - 1);
    }
}

From source file:com.vsthost.rnd.SandboxStrategyTest.java

/**
 * Defines a silly formula to be used in the objectives.
 *
 * @param x X/*www . j  a  v  a2s. c om*/
 * @param y Y
 * @return Some silly result.
 */
public static double SillyFormula(double x, double y) {
    final double z = Math.sqrt(x * y == 0 ? 1 : Math.abs(x * y));
    final double t = Math.pow(x <= 0 ? 1 : x, y);
    return x * x * x * +3 * x * x + 2 * x * y + 3 * y * y + y * y * y + 5 * x * y + z + t;
}

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;//from  ww  w  .  j a va2 s . c o m
}

From source file:com.jivesoftware.os.jive.utils.id.IdTest.java

@Test
public void idStringTest() throws Exception {
    final Random r = new Random(1234);
    for (int i = 0; i < 100; i++) {
        long id = Math.abs(r.nextLong());
        Id wid = new Id(id);
        Id rid = MAPPER.readValue(MAPPER.writeValueAsString(wid), Id.class);

        Assert.assertEquals(rid, wid, "Failed to map Id through String: " + id);
    }/*from  ww w . j  av a 2  s.  co m*/
}

From source file:Main.java

public static Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
    final double ASPECT_TOLERANCE = 0.1;
    double targetRatio = (double) w / h;
    if (sizes == null) {
        return null;
    }//from w  w  w  .ja  v a  2s . co  m
    Camera.Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;
    int targetHeight = h;
    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);
        }
    }
    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;
}