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:ch.algotrader.entity.trade.OrderImpl.java

@Override
public void setQuantity(long quantityIn) {

    // always set a positive quantity
    super.setQuantity(Math.abs(quantityIn));
}

From source file:Main.java

public static float[] createSequence(float begin, float end) {
    int sign = end > begin ? 1 : -1;
    int len = (int) (Math.abs(end - begin) + 1);
    float[] seq = new float[len];
    for (int i = 0; i < len; i++) {
        seq[i] = begin + i * sign;/*from www . jav  a  2 s  .c o m*/
    }
    return seq;
}

From source file:cn.hxh.springside.utils.EncodeUtils.java

private static String alphabetEncode(long num, int base) {
    num = Math.abs(num);
    StringBuilder sb = new StringBuilder();
    for (; num > 0; num /= base) {
        sb.append(ALPHABET.charAt((int) (num % base)));
    }/*from   w  ww.ja  v a  2  s. co m*/

    return sb.toString();
}

From source file:com.qpark.eip.core.spring.statistics.dao.StatisticsEraser.java

/**
 * Each day at 0:00 remove old out dated entries.
 *//*from  w ww .j av a2  s . c  om*/
@Scheduled(cron = "0 0 0 * * *")
public void erase() {
    Calendar gc = new GregorianCalendar();
    gc.set(Calendar.HOUR_OF_DAY, 0);
    gc.set(Calendar.MINUTE, 0);
    gc.set(Calendar.SECOND, 0);
    gc.set(Calendar.MILLISECOND, 0);
    gc.add(Calendar.WEEK_OF_YEAR, -1 * Math.abs(this.numberOfWeeksToKeepLogs));
    this.dao.eraseSystemUserLog(gc.getTime());
    this.dao.eraseApplicationUserLog(gc.getTime());
    this.dao.eraseFlowLogMessage(gc.getTime());
}

From source file:net.sf.jasperreports.functions.standard.MathFunctions.java

/**
 * Returns the absolute value of a number.
 *//*ww  w.j a va  2s.c  o  m*/
@Function("ABS")
@FunctionParameters({ @FunctionParameter("number") })
public static Number ABS(Number number) {
    if (number == null) {
        logNullArgument();
        return null;
    } else {
        if (number instanceof Integer) {
            return Math.abs((Integer) number);
        } else if (number instanceof Double) {
            return Math.abs((Double) number);
        } else if (number instanceof Float) {
            return Math.abs((Float) number);
        } else if (number instanceof Long) {
            return Math.abs((Long) number);
        } else {
            // fall-back
            return Math.abs(number.doubleValue());
        }
    }
}

From source file:entity.Chart.java

private void init() {
    try {//  w w w .  j ava 2s  .  co  m
        if (this.reportID < 0) {
            this.reportName = "Maj_Runs";
            this.sql = "exec sp_getMaj_Runs @BotID = " + Math.abs(reportID);
            this.server = SQLHelper.LOCAL_IP;
            this.database = SQLHelper.DB_QA_DATA;
            this.reportType = "tablechart";
            this.comments = "";
            this.startRow = 1;
            this.referQueryID = -1;
            this.groupClause = null;
            return;
        }
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("node", reportID);
        SqlCommand command = new SqlCommand(SQLHelper.LOCAL_IP, SQLHelper.DB_QA, CommandType.StoredProcedure,
                "sp_getQuery", params);
        HashMap<String, Object> cells = SQLHelper.executeCommand(command, new MapRowHandler());

        this.reportName = (String) cells.get("name");
        this.sql = (String) cells.get("queryText");
        this.server = (String) cells.get("server");
        if (this.server.equals("127.0.0.1") || this.server.equals("localhost")) {
            this.server = SQLHelper.LOCAL_IP;
        }
        this.database = (String) cells.get("database");
        this.reportType = (String) cells.get("queryType");
        this.comments = (String) cells.get("comments");
        this.startRow = (Integer) cells.get("startRow");
        this.referQueryID = (Integer) cells.get("ReferQueryID");
        this.groupClause = (String) cells.get("GroupClause");
        this.totalColumnName = (String) cells.get("TotalItemColumn");
        //set report parameters
        command = new SqlCommand(SQLHelper.LOCAL_IP, SQLHelper.DB_QA, CommandType.Text,
                "SELECT * FROM Qa_Query_Params WHERE ReportID = " + reportID);
        CachedRowSet rowSet = SQLHelper.executeCommand(command, new CachedRowSetResultHandler());
        rParams = new QueryParameter[rowSet.size()];
        int i = 0;
        while (rowSet.next()) {
            rParams[i] = new QueryParameter();
            rParams[i].name = rowSet.getString("Name");
            rParams[i].value = rowSet.getString("DefaultValue");
            rParams[i].type = rowSet.getString("DataType");
            i++;
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:mlflex.helper.MathUtilities.java

/** This method finds the absolute value for each of a list of values.
 *
 * @param values Values for which the absolute value should be found
 * @return Absolute values//from  w ww  . jav a2 s  .c o m
 */
public static ArrayList<Double> GetAbsoluteValues(ArrayList<Double> values) {
    ArrayList<Double> absoluteValues = new ArrayList<Double>();

    for (double value : values)
        absoluteValues.add(Math.abs(value));

    return absoluteValues;
}

From source file:Randomizer.java

/**
 * This method computes a pseudo-random number between 0 and 1 using a very
 * simple algorithm. Math.random() and java.util.Random are actually a lot
 * better at computing randomness./*from ww  w .j a  va2 s.  c  om*/
 */
public float randomFloat() {
    seed = (seed * a + c) % m;
    return (float) Math.abs((float) seed / (float) m);
}

From source file:Main.java

/**
 * http://stackoverflow.com/questions/2067955/fast-bitmap-blur-for-android-sdk
 * http://incubator.quasimondo.com//*from   w ww.ja v a2  s .c o  m*/
 *
 * @param sentBitmap
 * @param scale
 * @param radius
 * @return
 */
public static Bitmap fastBlur(Bitmap sentBitmap, float scale, int radius) {
    int width = Math.round(sentBitmap.getWidth() * scale);
    int height = Math.round(sentBitmap.getHeight() * scale);
    sentBitmap = Bitmap.createScaledBitmap(sentBitmap, width, height, false);

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

    if (radius < 1) {
        return null;
    }
    int w = img.getWidth();
    int h = img.getHeight();

    int[] pix = new int[w * h];
    img.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++) {
            pix[yi] = 0xff000000 | (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;
        }
    }

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

    return img;
}

From source file:Main.java

/**
 * <p>/*from   www .  j  ava  2 s  .co  m*/
 * Gets the greatest common divisor of the absolute value of two numbers,
 * using the "binary gcd" method which avoids division and modulo
 * operations. See Knuth 4.5.2 algorithm B. This algorithm is due to Josef
 * Stein (1961).
 * </p>
 * 
 * @param u a non-zero number
 * @param v a non-zero number
 * @return the greatest common divisor, never zero
 * @since 1.1
 */
public static int gcd(int u, int v) {
    if (u * v == 0) {
        return (Math.abs(u) + Math.abs(v));
    }
    // keep u and v negative, as negative integers range down to
    // -2^31, while positive numbers can only be as large as 2^31-1
    // (i.e. we can't necessarily negate a negative number without
    // overflow)
    /* assert u!=0 && v!=0; */
    if (u > 0) {
        u = -u;
    } // make u negative
    if (v > 0) {
        v = -v;
    } // make v negative
      // B1. [Find power of 2]
    int k = 0;
    while ((u & 1) == 0 && (v & 1) == 0 && k < 31) { // while u and v are
                                                     // both even...
        u /= 2;
        v /= 2;
        k++; // cast out twos.
    }
    if (k == 31) {
        throw new ArithmeticException("overflow: gcd is 2^31");
    }
    // B2. Initialize: u and v have been divided by 2^k and at least
    // one is odd.
    int t = ((u & 1) == 1) ? v : -(u / 2)/* B3 */;
    // t negative: u was odd, v may be even (t replaces v)
    // t positive: u was even, v is odd (t replaces u)
    do {
        /* assert u<0 && v<0; */
        // B4/B3: cast out twos from t.
        while ((t & 1) == 0) { // while t is even..
            t /= 2; // cast out twos
        }
        // B5 [reset max(u,v)]
        if (t > 0) {
            u = -t;
        } else {
            v = t;
        }
        // B6/B3. at this point both u and v should be odd.
        t = (v - u) / 2;
        // |u| larger: t positive (replace u)
        // |v| larger: t negative (replace v)
    } while (t != 0);
    return -u * (1 << k); // gcd is u*2^k
}