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 size of an ulp of the argument.  An ulp of a
 * <code>double</code> value is the positive distance between this
 * floating-point value and the <code>double</code> value next
 * larger in magnitude.  Note that for non-NaN <i>x</i>,
 * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
 *
 * <p>Special Cases:/*from  w  ww .j a  va2 s.  c  o  m*/
 * <ul>
 * <li> If the argument is NaN, then the result is NaN.
 * <li> If the argument is positive or negative infinity, then the
 * result is positive infinity.
 * <li> If the argument is positive or negative zero, then the result is
 * <code>Double.MIN_VALUE</code>.
 * <li> If the argument is &plusmn;<code>Double.MAX_VALUE</code>, then
 * the result is equal to 2<sup>971</sup>.
 * </ul>
 *
 * @param d the floating-point value whose ulp is to be returned
 * @return the size of an ulp of the argument
 * @author Joseph D. Darcy
 * @since 1.5
 */
public static double ulp(double d) {
    int exp = getExponent(d);

    switch (exp) {
    case DoubleConsts.MAX_EXPONENT + 1: // NaN or infinity
        return Math.abs(d);
    // break;

    case DoubleConsts.MIN_EXPONENT - 1: // zero or subnormal
        return Double.MIN_VALUE;
    // break

    default:
        assert exp <= DoubleConsts.MAX_EXPONENT && exp >= DoubleConsts.MIN_EXPONENT;

        // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
        exp = exp - (DoubleConsts.SIGNIFICAND_WIDTH - 1);
        if (exp >= DoubleConsts.MIN_EXPONENT) {
            return powerOfTwoD(exp);
        } else {
            // return a subnormal result; left shift integer
            // representation of Double.MIN_VALUE appropriate
            // number of positions
            return Double.longBitsToDouble(
                    1L << (exp - (DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH - 1))));
        }
        // break
    }
}

From source file:de.tuberlin.uebb.jdae.diff.partial.PDNumberTest.java

public static boolean approximate(final PDNumber number, final PDNumber testee) {
    if (testee.values.length != number.values.length)
        return false;

    for (int i = 0; i < testee.values.length; i++)
        if (Math.abs(testee.values[i] - number.values[i]) > TOLERANCE)
            return false;

    return true;/*  w  w w  .j  a  va  2 s . c  om*/
}

From source file:com.algoTrader.service.PositionServiceImpl.java

@Override
protected void handleClosePosition(int positionId) throws Exception {

    Position position = getPositionDao().load(positionId);

    reducePosition(positionId, Math.abs(position.getQuantity()));
}

From source file:net.duckling.ddl.web.agent.util.AuthUtil.java

private static boolean notExpired(Date d) {
    long now = System.currentTimeMillis();
    long dd = d.getTime();
    //??30/* ww w .j  ava2  s .  com*/
    if (Math.abs((now - dd)) < (1000 * 60 * 30)) {
        return true;
    }
    return false;
}

From source file:org.mwc.debrief.track_shift.views.CachedTickDateAxis.java

@SuppressWarnings("rawtypes")
@Override/*from w w  w  .ja  v a 2 s .  co  m*/
protected List refreshTicksVertical(final Graphics2D g2, final Rectangle2D dataArea, final RectangleEdge edge) {
    // do we have a height?
    if (_lastHeight != null) {
        // is the new height different to our last one?
        if (Math.abs(dataArea.getHeight() - _lastHeight.doubleValue()) > 60) {
            // yes - ditch the ticks
            _myTicks = null;
        }
    }

    // do we have any ticks?
    if (_myTicks == null) {
        // nope, better create some
        _myTicks = super.refreshTicksVertical(g2, dataArea, edge);
        _lastHeight = dataArea.getHeight();
    }

    return _myTicks;
}

From source file:ImageDuplicity.java

private void createOffscreenImage() {
    Dimension d = getSize();//from   ww  w .  j  a v  a  2  s.co  m
    int width = d.width;
    int height = d.height;
    image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    Graphics2D g2 = image.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    try {
        String filename = "largeJava2sLogo.jpg";
        InputStream in = getClass().getResourceAsStream(filename);
        JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
        BufferedImage image = decoder.decodeAsBufferedImage();
        in.close();
        g2.drawImage(image, 0, 0, width, height, null);
    } catch (Exception e) {
        System.out.print(e);
    }

    g2.setStroke(new BasicStroke(2));
    Color[] colors = { Color.red, Color.blue, Color.green };
    for (int i = -32; i < 40; i += 8) {
        g2.setPaint(colors[Math.abs(i) % 3]);
        g2.drawOval(i, i, width - i * 2, height - i * 2);
    }
}

From source file:edu.illinois.cs.cogcomp.saul.test.TestReal.java

public TestReal(Learner classifier, Classifier oracle, Parser parser) {
    int examples = 0;
    double totalDifference = 0;
    double[] actuals = {};
    double[] predictions = {};

    classifier.write(System.out);

    for (Object example = parser.next(); example != null; example = parser.next()) {
        double prediction = classifier.realValue(example);

        predictions = Arrays.copyOf(predictions, predictions.length + 1);
        predictions[predictions.length - 1] = prediction;

        double value = oracle.realValue(example);

        actuals = Arrays.copyOf(actuals, actuals.length + 1);
        actuals[actuals.length - 1] = value;

        double difference = Math.abs(prediction - value);
        totalDifference += difference;//from w  w  w.ja va2 s .  c  o  m
        classifier.classify(example);
        ++examples;

        System.out.println(
                "Example " + examples + " difference: " + difference + " (prediction: " + prediction + ")");
    }
    System.out.println("test examples number: " + examples);
    double avg = totalDifference / examples;
    System.out.println("Average difference: " + avg);
    double p = getPearsonCorrelation(predictions, actuals);
    System.out.println("Pearson correlation:" + p);
    SpearmansCorrelation e = new SpearmansCorrelation();
    double sp = e.correlation(predictions, actuals);
    System.out.println("Spearman correlation:" + sp);

}

From source file:com.opengamma.analytics.math.interpolation.RationalFunctionInterpolator1D.java

@Override
public Double interpolate(final Interpolator1DDataBundle data, final Double value) {
    Validate.notNull(value, "value");
    Validate.notNull(data, "data bundle");
    final int m = _degree + 1;
    if (data.size() < m) {
        throw new IllegalArgumentException(
                "Need at least " + (_degree + 1) + " data points to perform this interpolation");
    }/*from  w ww.  ja  v  a 2s .  c  o  m*/
    final double[] xArray = data.getKeys();
    final double[] yArray = data.getValues();
    final int n = data.size() - 1;
    if (data.getLowerBoundIndex(value) == n) {
        return yArray[n];
    }
    double diff = Math.abs(value - xArray[0]);
    if (Math.abs(diff) < _eps) {
        return yArray[0];
    }
    double diff1;
    final double[] c = new double[m];
    final double[] d = new double[m];
    int ns = 0;
    for (int i = 0; i < m; i++) {
        diff1 = Math.abs(value - xArray[i]);
        if (diff < _eps) {
            return yArray[i];
        } else if (diff1 < diff) {
            ns = i;
            diff = diff1;
        }
        c[i] = yArray[i];
        d[i] = yArray[i] + _eps;
    }
    double y = yArray[ns--];
    double w, t, dd;
    for (int i = 1; i < m; i++) {
        for (int j = 0; j < m - i; j++) {
            w = c[j + 1] - d[j];
            diff = xArray[i + j] - value;
            t = (xArray[j] - value) * d[j] / diff;
            dd = t - c[j + 1];
            if (Math.abs(dd) < _eps) {
                throw new MathException("Interpolating function has a pole at x = " + value);
            }
            dd = w / dd;
            d[j] = c[j + 1] * dd;
            c[j] = t * dd;
        }
        y += 2 * (ns + 1) < m - i ? c[ns + 1] : d[ns--];
    }
    return y;
}

From source file:net.gtaun.shoebill.data.Velocity.java

public float angleZ() {
    return (float) Math.acos(getZ() / Math.abs(speed3d()));
}