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:com.aqnote.shared.cryptology.util.lang.CommonUtil.java

public static String genRandom(int length) {
    final int maxNum = ASCII_CHAR.length;
    int i;// w w  w .  ja v a2s  .c  o m
    int count = 0;

    Random r = new Random();
    StringBuffer pwd = new StringBuffer("");
    while (count++ < length) {
        i = Math.abs(r.nextInt(maxNum));
        if (i >= 0 && i < ASCII_CHAR.length) {
            pwd.append(ASCII_CHAR[i]);
        }
    }
    return pwd.toString();
}

From source file:com.metawiring.load.generators.RandomStringGenerator.java

@Override
public String get() {
    return String.valueOf(Math.abs(theTwister.nextLong()));
}

From source file:eu.ggnet.dwoss.report.assist.gen.ReportLineGenerator.java

/** This Method return a Random Positiv int Value;
 * <p/>//from  w ww . j a va 2 s  . c  om
 * @return a positiv int Value.
 */
private static int getRandomInt() {
    return Math.abs(R.nextInt(1000000));
}

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

public static void inPlace(float[] v) {
    Validate.notNull(v);//from www .  j a  va2  s  .  com
    final int n = v.length;
    for (int i = 0; i < n; i++) {
        v[i] = Math.abs(v[i]);
    }
}

From source file:MathFunc.java

/**
 * Returns the arc cosine of an angle, in the range of 0.0 through <code>Math.PI</code>.
 * Special case:/*from ww w . jav  a 2 s .c om*/
 * <ul>
 *  <li>If the argument is <code>NaN</code> or its absolute value is greater than 1,
 *      then the result is <code>NaN</code>.
 * </ul>
 * 
 * @param a - the value whose arc cosine is to be returned.
 * @return the arc cosine of the argument.
 */
public static double acos(double a) {
    // Special case.
    if (Double.isNaN(a) || Math.abs(a) > 1.0) {
        return Double.NaN;
    }

    // Calculate the arc cosine.
    double aSquared = a * a;
    double arcCosine = atan2(Math.sqrt(1 - aSquared), a);
    return arcCosine;
}

From source file:com.fanniemae.ezpie.common.DateUtilities.java

public static String elapsedTime(long start, long end) {
    long elapsed = Math.abs(end - start);
    String elapsedPretty;//from  w  w  w .  j a v  a 2 s .  c  o m
    if (elapsed < 60000L) {
        elapsedPretty = DurationFormatUtils.formatDuration(elapsed, "s.S' seconds'");
    } else if (elapsed < 3600000L) {
        elapsedPretty = DurationFormatUtils.formatDuration(elapsed, "m' minutes' s.S' seconds'");
    } else if (elapsed < 86400000L) {
        elapsedPretty = DurationFormatUtils.formatDuration(elapsed, "H' hours' m' minutes' s.S' seconds'");
    } else {
        elapsedPretty = DurationFormatUtils.formatDuration(elapsed,
                "d' days' H' hours' m' minutes' s.S' seconds'");
    }

    // Make days/hours/minutes singular if just one unit.
    if (elapsedPretty.startsWith("1 days "))
        elapsedPretty = elapsedPretty.replace("1 days ", "1 day ");
    if (elapsedPretty.contains(" 1 hours "))
        elapsedPretty = elapsedPretty.replace(" 1 hours ", " 1 hour ");
    if (elapsedPretty.contains(" 1 minutes "))
        elapsedPretty = elapsedPretty.replace(" 1 minutes ", " 1 minute ");
    if (end - start < 0) {
        return "Negative " + elapsedPretty;
    }
    return elapsedPretty;
}

From source file:Methods.CalculusBisection.java

public static void bisection1(double xlower, double xupper, double decPoint) {//method used calculate root point acording the paramethers that enter the method and store the data in a global Stack

    S = new BidimensionalArrayStack();//Declaring a new Stack beeing sure is clear before using it
    Xlower = new BidimensionalArrayStack();
    Xupper = new BidimensionalArrayStack();
    double xnew, fxlower, fxupper, fxnew, diff;
    int iteration;

    fxlower = xlower - Math.pow(xlower, 2);
    fxupper = xupper - Math.pow(xupper, 2);
    iteration = 0;//from   ww  w . j  ava2s  .co m

    do {
        iteration += 1;
        // determine xnew and f (xnew)
        xnew = (xlower + xupper) / 2.0;
        fxnew = xnew - Math.pow(xnew, 2);
        System.out.println("Approx for iteration{}" + iteration + " is " + xnew);
        diff = Math.abs(xupper - xlower) / 2.0;
        Xlower.push(xlower, fxlower);//Inserting data in the Stack
        Xupper.push(xupper, fxupper);

        if (fxlower * fxnew > 0) {
            xlower = xnew;
            fxlower = fxnew;
        } else if (fxupper * fxnew > 0) {
            xupper = xnew;
            fxupper = fxnew;
        }
        S.push(xnew, fxnew);
    } while (diff > decPoint);
    System.out.println("root to six decimal places is " + xnew);
}

From source file:Util.java

/**
 * Find the index of the array nearest to the value. The values array can 
 * contain only unique values. If it doesn't the first occurence of a value
 * in the values array is the one used, subsequent duplicate are ignored. If
 * the value falls outside the bounds of the array, <b>null</b> is returned
 *
 * @param array Values to search through for the nearest point
 * @param value THe value to search for the nearest neighbor in the array
 * @return The index of the array value nearest the value. null if the value
 *      is larger or smaller than any values in the array.
 *///from  w  w w  . j  a  v  a  2s.c  o  m
public static Integer nearInclusive(final double[] array, final double value) {
    Integer i = null;
    int idx = binarySearch(array, value);
    if (idx < 0) {
        idx = -(idx) - 1;
        if (idx == 0 || idx >= array.length) {
            // Do nothing. This point is outside the array bounds return value will be null
        } else {
            // Find nearest point
            double d0 = Math.abs(array[idx - 1] - value);
            double d1 = Math.abs(array[idx] - value);
            i = (d0 <= d1) ? idx - 1 : idx;
        }
    } else {
        i = idx;
    }
    return i;
}

From source file:Main.java

public static Bitmap getBlur(Bitmap sentBitmap, int radius) {

    Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);

    if (radius < 1) {
        return (null);
    }/* ww  w.  ja  v a 2 s  . c  o m*/

    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    int[] pix = new int[w * h];
    bitmap.getPixels(pix, 0, w, 0, 0, w, h);

    int wm = w - 1;
    int hm = h - 1;
    int wh = w * h;
    int div = radius + radius + 1;

    int r[] = new int[wh];
    int g[] = new int[wh];
    int b[] = new int[wh];
    int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
    int vmin[] = new int[Math.max(w, h)];

    int divsum = (div + 1) >> 1;
    divsum *= divsum;
    int dv[] = new int[256 * divsum];
    for (i = 0; i < 256 * divsum; i++) {
        dv[i] = (i / divsum);
    }

    yw = yi = 0;

    int[][] stack = new int[div][3];
    int stackpointer;
    int stackstart;
    int[] sir;
    int rbs;
    int r1 = radius + 1;
    int routsum, goutsum, boutsum;
    int rinsum, ginsum, binsum;

    for (y = 0; y < h; y++) {
        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
        for (i = -radius; i <= radius; i++) {
            p = pix[yi + Math.min(wm, Math.max(i, 0))];
            sir = stack[i + radius];
            sir[0] = (p & 0xff0000) >> 16;
            sir[1] = (p & 0x00ff00) >> 8;
            sir[2] = (p & 0x0000ff);
            rbs = r1 - Math.abs(i);
            rsum += sir[0] * rbs;
            gsum += sir[1] * rbs;
            bsum += sir[2] * rbs;
            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
            } else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
            }
        }
        stackpointer = radius;

        for (x = 0; x < w; x++) {

            r[yi] = dv[rsum];
            g[yi] = dv[gsum];
            b[yi] = dv[bsum];

            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;

            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];

            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];

            if (y == 0) {
                vmin[x] = Math.min(x + radius + 1, wm);
            }
            p = pix[yw + vmin[x]];

            sir[0] = (p & 0xff0000) >> 16;
            sir[1] = (p & 0x00ff00) >> 8;
            sir[2] = (p & 0x0000ff);

            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];

            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;

            stackpointer = (stackpointer + 1) % div;
            sir = stack[(stackpointer) % div];

            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];

            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];

            yi++;
        }
        yw += w;
    }
    for (x = 0; x < w; x++) {
        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
        yp = -radius * w;
        for (i = -radius; i <= radius; i++) {
            yi = Math.max(0, yp) + x;

            sir = stack[i + radius];

            sir[0] = r[yi];
            sir[1] = g[yi];
            sir[2] = b[yi];

            rbs = r1 - Math.abs(i);

            rsum += r[yi] * rbs;
            gsum += g[yi] * rbs;
            bsum += b[yi] * rbs;

            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
            } else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
            }

            if (i < hm) {
                yp += w;
            }
        }
        yi = x;
        stackpointer = radius;
        for (y = 0; y < h; y++) {
            // Preserve alpha channel: ( 0xff000000 & pix[yi] )
            pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];

            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;

            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];

            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];

            if (x == 0) {
                vmin[y] = Math.min(y + r1, hm) * w;
            }
            p = x + vmin[y];

            sir[0] = r[p];
            sir[1] = g[p];
            sir[2] = b[p];

            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];

            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;

            stackpointer = (stackpointer + 1) % div;
            sir = stack[stackpointer];

            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];

            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];

            yi += w;
        }
    }

    bitmap.setPixels(pix, 0, w, 0, 0, w, h);

    return (bitmap);
}

From source file:org.jfree.chart.demo.DataParser.java

public int getFlippedX() {
    return Math.abs(coords[0] - effectiveX);
}