Example usage for java.lang Math rint

List of usage examples for java.lang Math rint

Introduction

In this page you can find the example usage for java.lang Math rint.

Prototype

public static double rint(double a) 

Source Link

Document

Returns the double value that is closest in value to the argument and is equal to a mathematical integer.

Usage

From source file:net.sf.zekr.common.util.VelocityUtils.java

public Integer round(Object num) {
    Number n = toDouble(num);
    return new Integer((int) Math.rint(n.doubleValue()));
}

From source file:org.pentaho.plugin.jfreereport.reportcharts.ScalingLogarithmicAxis.java

protected List refreshTicksVertical(final Graphics2D g2, final Rectangle2D dataArea, final RectangleEdge edge) {

    final ArrayList ticks = new ArrayList();

    // get lower bound value:
    double lowerBoundVal = getRange().getLowerBound();
    // if small log values and lower bound value too small
    // then set to a small value (don't allow <= 0):
    if (this.smallLogFlag && lowerBoundVal < SMALL_LOG_VALUE) {
        lowerBoundVal = SMALL_LOG_VALUE;
    }//from   w  w w. j a v  a 2  s.co m
    // get upper bound value
    final double upperBoundVal = getRange().getUpperBound();

    // get log10 version of lower bound and round to integer:
    int iBegCount = (int) Math.rint(switchedLog10(lowerBoundVal));
    // get log10 version of upper bound and round to integer:
    final int iEndCount = (int) Math.rint(switchedLog10(upperBoundVal));

    if (iBegCount == iEndCount && iBegCount > 0 && Math.pow(10, iBegCount) > lowerBoundVal) {
        // only 1 power of 10 value, it's > 0 and its resulting
        // tick value will be larger than lower bound of data
        --iBegCount; // decrement to generate more ticks
    }

    double tickVal;
    String tickLabel;
    boolean zeroTickFlag = false;
    final int[] intervals = { 10, 10, 10, 10, 10, 5, 5, 5, 3, 3 };

    for (int i = iBegCount; i <= iEndCount; i++) {
        // for each tick with a label to be displayed
        int jEndCount = getTicksVertical();
        if (jEndCount == -1) {
            jEndCount = Math.abs(iEndCount - iBegCount) < 10 ? intervals[iEndCount - iBegCount] : 1;
        }

        if (i == iEndCount) {
            jEndCount = 1;
        }

        for (int j = 0; j < jEndCount; j++) {
            // for each tick to be displayed
            if (this.smallLogFlag) {
                // small log values in use
                tickVal = Math.pow(10, i) + (Math.pow(10, i) * j);
                if (j == 0) {
                    // first tick of group; create label text
                    if (this.log10TickLabelsFlag) {
                        // if flag then
                        tickLabel = "10^" + i; // create "log10"-type label
                    } else { // not "log10"-type label
                        if (this.expTickLabelsFlag) {
                            // if flag then
                            tickLabel = "1e" + i; // create "1e#"-type label
                        } else { // not "1e#"-type label
                            if (i >= 0) { // if positive exponent then
                                // make integer
                                final NumberFormat format = getNumberFormatOverride();
                                if (format != null) {
                                    tickLabel = format.format(tickVal);
                                } else {
                                    tickLabel = LogCategoryItemLabelGenerator.formatValue(new Double(tickVal));
                                    /*tickLabel = Long.toString((long) Math.rint(tickVal));*/
                                }
                            } else {
                                // negative exponent; create fractional value
                                // set exact number of fractional digits to
                                // be shown:
                                this.numberFormatterObj.setMaximumFractionDigits(-i);
                                // create tick label:
                                tickLabel = this.numberFormatterObj.format(tickVal);
                            }
                        }
                    }
                } else { // not first tick to be displayed
                    tickLabel = ""; // no tick label
                }
            } else { // not small log values in use; allow for values <= 0
                if (zeroTickFlag) { // if did zero tick last iter then
                    --j;
                } // decrement to do 1.0 tick now
                tickVal = (i >= 0) ? Math.pow(10, i) + (Math.pow(10, i) * j)
                        : j == 0 ? -Math.pow(10, -i) : -(Math.pow(10, -i) - (Math.pow(10, -i - 1) * (9 - j)));
                if (j == 0) { // first tick of group
                    if (!zeroTickFlag) { // did not do zero tick last
                        // iteration
                        if (i > iBegCount && i < iEndCount && Math.abs(tickVal - 1.0) < 0.0001) {
                            // not first or last tick on graph and value
                            // is 1.0
                            tickVal = 0.0; // change value to 0.0
                            zeroTickFlag = true; // indicate zero tick
                            tickLabel = "0"; // create label for tick
                        } else {
                            // first or last tick on graph or value is 1.0
                            // create label for tick:
                            if (this.log10TickLabelsFlag) {
                                // create "log10"-type label
                                tickLabel = (((i < 0) ? "-" : "") + "10^" + Math.abs(i));
                            } else {
                                if (this.expTickLabelsFlag) {
                                    // create "1e#"-type label
                                    tickLabel = (((i < 0) ? "-" : "") + "1e" + Math.abs(i));
                                } else {
                                    final NumberFormat format = getNumberFormatOverride();
                                    if (format != null) {
                                        tickLabel = format.format(tickVal);
                                    } else {
                                        tickLabel = LogCategoryItemLabelGenerator.formatValue(new Double(
                                                tickVal));/*
                                                          tickLabel =  Long.toString((long) Math.rint(tickVal));*/
                                    }
                                }
                            }
                        }
                    } else { // did zero tick last iteration
                        tickLabel = ""; // no label
                        zeroTickFlag = false; // clear flag
                    }
                } else { // not first tick of group
                    tickLabel = ""; // no label
                    zeroTickFlag = false; // make sure flag cleared
                }
            }

            if (tickVal > upperBoundVal) {
                return ticks; // if past highest data value then exit method
            }

            if (tickVal >= lowerBoundVal - SMALL_LOG_VALUE) {
                // tick value not below lowest data value
                final TextAnchor anchor;
                final TextAnchor rotationAnchor;
                double angle = 0.0;
                if (isVerticalTickLabels()) {
                    if (edge == RectangleEdge.LEFT) {
                        anchor = TextAnchor.BOTTOM_CENTER;
                        rotationAnchor = TextAnchor.BOTTOM_CENTER;
                        angle = -Math.PI / 2.0;
                    } else {
                        anchor = TextAnchor.BOTTOM_CENTER;
                        rotationAnchor = TextAnchor.BOTTOM_CENTER;
                        angle = Math.PI / 2.0;
                    }
                } else {
                    if (edge == RectangleEdge.LEFT) {
                        anchor = TextAnchor.CENTER_RIGHT;
                        rotationAnchor = TextAnchor.CENTER_RIGHT;
                    } else {
                        anchor = TextAnchor.CENTER_LEFT;
                        rotationAnchor = TextAnchor.CENTER_LEFT;
                    }
                }

                ticks.add(new NumberTick(new Double(tickVal), tickLabel, anchor, rotationAnchor, angle));
            }
        }
    }
    return ticks;
}

From source file:Main.java

/**
 * Goal is to return a reasonable string representation
 * of x, using at most width spaces.  (If the parameter width is
 * unreasonably big or small, its value is adjusted to
 * lie in the range 6 to 25.)/*from  w w w. j a v a 2s . co m*/
 *
 * @param x value to create string representation of.
 * @param width maximum number of spaces used in string representation, if possible.
 * @return a string representation for x.  If x is Double.NaN, "undefined" is returned.
 *         If x is infinite, "INF" or "-INF" is returned.
 */
public static String realToString(double x, int width) {
    width = Math.min(25, Math.max(6, width));
    if (Double.isNaN(x))
        return "undefined";
    if (Double.isInfinite(x))
        if (x < 0)
            return "-INF";
        else
            return "INF";
    String s = String.valueOf(x);
    if (Math.rint(x) == x && Math.abs(x) < 5e15 && s.length() <= (width + 2))
        return String.valueOf((long) x); // return string without trailing ".0"
    if (s.length() <= width)
        return s;
    boolean neg = false;
    if (x < 0) {
        neg = true;
        x = -x;
        width--;
        s = String.valueOf(x);
    }
    long maxForNonExp = 5 * (long) Math.pow(10, width - 2);
    if (x >= 0.0005 && x <= maxForNonExp && (s.indexOf('E') == -1 && s.indexOf('e') == -1)) {
        s = round(s, width);
        s = trimZeros(s);
    } else if (x > 1) { // construct exponential form with positive exponent
        long power = (long) Math.floor(Math.log(x) / Math.log(10));
        String exp = "E" + power;
        int numlength = width - exp.length();
        x = x / Math.pow(10, power);
        s = String.valueOf(x);
        s = round(s, numlength);
        s = trimZeros(s);
        s += exp;
    } else { // constuct exponential form with negative argument
        long power = (long) Math.ceil(-Math.log(x) / Math.log(10));
        String exp = "E-" + power;
        int numlength = width - exp.length();
        x = x * Math.pow(10, power);
        s = String.valueOf(x);
        s = round(s, numlength);
        s = trimZeros(s);
        s += exp;
    }
    if (neg)
        return "-" + s;
    else
        return s;
}

From source file:org.gumtree.vis.plot1d.KLogarithmicAxis.java

@Override
protected List refreshTicksHorizontal(Graphics2D g2, Rectangle2D dataArea, RectangleEdge edge) {
    // TODO Auto-generated method stub
    List ticks = new java.util.ArrayList();
    Range range = getRange();/*  w  w  w .  ja va2 s.c om*/

    //get lower bound value:
    double lowerBoundVal = range.getLowerBound();
    //if small log values and lower bound value too small
    // then set to a small value (don't allow <= 0):
    if (this.smallLogFlag && lowerBoundVal < SMALL_LOG_VALUE) {
        lowerBoundVal = SMALL_LOG_VALUE;
    }

    //get upper bound value
    double upperBoundVal = range.getUpperBound();

    //get log10 version of lower bound and round to integer:
    int iBegCount = (int) Math.rint(switchedLog10(lowerBoundVal));
    //get log10 version of upper bound and round to integer:
    int iEndCount = (int) Math.rint(switchedLog10(upperBoundVal));

    //        if (iBegCount == iEndCount && iBegCount >= 0
    if (iBegCount == iEndCount && Math.pow(10, iBegCount) > lowerBoundVal) {
        //only 1 power of 10 value, it's > 0 and its resulting
        // tick value will be larger than lower bound of data
        --iBegCount; //decrement to generate more ticks
    }

    int numberOfGrids = 0;
    int numberOfTicks = 0;
    NumberTick lastTick = null;

    double currentTickValue;
    String tickLabel;
    boolean zeroTickFlag = false;
    for (int i = iBegCount; i <= iEndCount; i++) {
        //for each power of 10 value; create ten ticks
        for (int j = 0; j < 10; ++j) {
            //for each tick to be displayed
            if (this.smallLogFlag) {
                //small log values in use; create numeric value for tick
                currentTickValue = Math.pow(10, i) + (Math.pow(10, i) * j);
                if (this.expTickLabelsFlag || (i < 0 && currentTickValue > 0.0 && currentTickValue < 1.0)) {
                    //showing "1e#"-style ticks or negative exponent
                    // generating tick value between 0 & 1; show fewer
                    if (j == 0 || (i > -4 && (j < 2 || j == 4)) || currentTickValue >= upperBoundVal) {
                        //first tick of series, or not too small a value and
                        // one of first 3 ticks, or last tick to be displayed
                        // set exact number of fractional digits to be shown
                        // (no effect if showing "1e#"-style ticks):
                        this.numberFormatterObj.setMaximumFractionDigits(-i);
                        //create tick label (force use of fmt obj):
                        tickLabel = makeTickLabel(currentTickValue, true);
                    } else { //no tick label to be shown
                        //                            tickLabel = "";
                        if (numberOfTicks == 0) {
                            tickLabel = makeTickLabel(currentTickValue, true);
                        } else
                            tickLabel = "";
                    }
                } else { //tick value not between 0 & 1
                         //show tick label if it's the first or last in
                         // the set, or if it's 1-5; beyond that show
                         // fewer as the values get larger:
                    tickLabel = (j < 1 || (i < 1 && j < 5) || (j < 4 - i) || currentTickValue >= upperBoundVal
                            || numberOfTicks == 0) ? makeTickLabel(currentTickValue) : "";
                }
            } else { //not small log values in use; allow for values <= 0
                if (zeroTickFlag) { //if did zero tick last iter then
                    --j; //decrement to do 1.0 tick now
                } //calculate power-of-ten value for tick:
                currentTickValue = (i >= 0) ? Math.pow(10, i) + (Math.pow(10, i) * j)
                        : -(Math.pow(10, -i) - (Math.pow(10, -i - 1) * j));
                if (!zeroTickFlag) { // did not do zero tick last iteration
                    if (Math.abs(currentTickValue - 1.0) < 0.0001 && lowerBoundVal <= 0.0
                            && upperBoundVal >= 0.0) {
                        //tick value is 1.0 and 0.0 is within data range
                        currentTickValue = 0.0; //set tick value to zero
                        zeroTickFlag = true; //indicate zero tick
                    }
                } else { //did zero tick last iteration
                    zeroTickFlag = false; //clear flag
                } //create tick label string:
                //show tick label if "1e#"-style and it's one
                // of the first two, if it's the first or last
                // in the set, or if it's 1-5; beyond that
                // show fewer as the values get larger:
                tickLabel = ((this.expTickLabelsFlag && j < 2) || j < 1 || (i < 1 && j < 5) || (j < 4 - i)
                        || currentTickValue >= upperBoundVal || numberOfTicks == 0)
                                ? makeTickLabel(currentTickValue)
                                : "";
            }

            if (currentTickValue > upperBoundVal) {
                if (lastTick != null) {
                    String lastTickText = lastTick.getText();
                    if (lastTickText == null || lastTickText.trim().length() == 0) {
                        ticks.remove(lastTick);
                        ticks.add(new NumberTick(lastTick.getValue(),
                                createTickLabel(lastTick.getValue(), i - 1), lastTick.getTextAnchor(),
                                lastTick.getRotationAnchor(), lastTick.getAngle()));
                    }
                }
                if (numberOfTicks < 4) {
                    return getAllTicksHorizontal(g2, dataArea, edge);
                }
                return ticks; // if past highest data value then exit
                              // method
            }

            if (currentTickValue >= lowerBoundVal - SMALL_LOG_VALUE) {
                //tick value not below lowest data value
                TextAnchor anchor = null;
                TextAnchor rotationAnchor = null;
                double angle = 0.0;
                if (isVerticalTickLabels()) {
                    anchor = TextAnchor.CENTER_RIGHT;
                    rotationAnchor = TextAnchor.CENTER_RIGHT;
                    if (edge == RectangleEdge.TOP) {
                        angle = Math.PI / 2.0;
                    } else {
                        angle = -Math.PI / 2.0;
                    }
                } else {
                    if (edge == RectangleEdge.TOP) {
                        anchor = TextAnchor.BOTTOM_CENTER;
                        rotationAnchor = TextAnchor.BOTTOM_CENTER;
                    } else {
                        anchor = TextAnchor.TOP_CENTER;
                        rotationAnchor = TextAnchor.TOP_CENTER;
                    }
                }

                lastTick = new NumberTick(new Double(currentTickValue), tickLabel, anchor, rotationAnchor,
                        angle);
                ticks.add(lastTick);
                if (tickLabel != null && tickLabel.trim().length() > 0)
                    numberOfTicks++;
                numberOfGrids++;
            }
        }
    }
    if (numberOfTicks < 4) {
        return getAllTicksHorizontal(g2, dataArea, edge);
    }
    return ticks;
}

From source file:com.mapr.synth.samplers.DateSampler.java

@Override
public JsonNode sample() {
    synchronized (this) {
        long t = (long) Math.rint(base.nextDouble());
        return new TextNode(df.format(new java.util.Date(end - t)));
    }//from  w  ww  . j a  v  a 2  s  . c o m
}

From source file:de.tor.tribes.ui.wiz.dep.DefensePlanerWizard.java

private static List<SOSRequest> createSampleRequests() {
    int wallLevel = 20;
    int supportCount = 100;
    int maxAttackCount = 50;
    int maxFakeCount = 0;

    List<SOSRequest> result = new LinkedList<>();
    Village[] villages = GlobalOptions.getSelectedProfile().getTribe().getVillageList();
    Village[] attackerVillages = DataHolder.getSingleton().getTribeByName("Alexander25").getVillageList();

    for (int i = 0; i < supportCount; i++) {
        int id = (int) Math.rint(Math.random() * (villages.length - 1));
        Village target = villages[id];/*from w  ww.j  a  v  a  2  s.  c  o m*/
        SOSRequest r = new SOSRequest(target.getTribe());
        r.addTarget(target);
        TargetInformation info = r.getTargetInformation(target);
        info.setWallLevel(wallLevel);

        TroopAmountFixed troops = new TroopAmountFixed();
        troops.setAmountForUnit("spear", (int) Math.rint(Math.random() * 14000));
        troops.setAmountForUnit("sword", (int) Math.rint(Math.random() * 14000));
        troops.setAmountForUnit("heavy", (int) Math.rint(Math.random() * 5000));
        info.setTroops(troops);

        int cnt = (int) Math.rint(maxAttackCount * Math.random());
        for (int j = 0; j < cnt; j++) {
            int idx = (int) Math.rint(Math.random() * (attackerVillages.length - 2));
            Village v = attackerVillages[idx];
            info.addAttack(v, new Date(
                    System.currentTimeMillis() + Math.round(DateUtils.MILLIS_PER_DAY * 7 * Math.random())));
            for (int k = 0; k < (int) Math.rint(maxFakeCount * Math.random()); k++) {
                idx = (int) Math.rint(Math.random() * (attackerVillages.length - 2));
                v = attackerVillages[idx];
                info.addAttack(v, new Date(System.currentTimeMillis() + Math.round(3600 * Math.random())));
            }
        }
        result.add(r);
    }

    return result;
}

From source file:dk.netarkivet.harvester.harvesting.frontier.FrontierReportCsvExport.java

private static String getDisplayValue(double val) {
    if (Double.MIN_VALUE == val) {
        return FrontierReportLine.EMPTY_VALUE_TOKEN;
    }/*from www . java2s .co m*/
    return (Math.rint(val) == val ? Integer.toString((int) val) : "" + Double.toString(val));
}

From source file:org.renjin.MathExt.java

public static double round(double x) {
    return Math.rint(x);
}

From source file:sadl.models.pdrta.PDRTAInput.java

private TIntList loadTimeDelays(boolean expand) {

    final TIntList timePoints = new TIntArrayList();
    maxTimeDelay = Integer.MIN_VALUE;
    minTimeDelay = Integer.MAX_VALUE;

    int timeDelay;
    for (final TimedWord w : inp) {
        for (int i = 0; i < w.length(); i++) {
            timeDelay = w.getTimeValue(i);
            if (timeDelay > maxTimeDelay) {
                maxTimeDelay = timeDelay;
            }// ww w.  j  av a  2s .  c  o m
            if (timeDelay < minTimeDelay) {
                minTimeDelay = timeDelay;
            }
            timePoints.add(timeDelay);
        }
    }

    if (expand) {
        final int val = (int) Math.rint((maxTimeDelay - minTimeDelay) * 0.05);
        minTimeDelay -= val;
        maxTimeDelay += val;
        if (minTimeDelay < 0) {
            minTimeDelay = 0;
        }
    }

    return timePoints;
}

From source file:org.structr.common.PagingHelper.java

private static int getPageCount(int resultCount, int pageSize) {

    return (int) Math.rint(Math.ceil((double) resultCount / (double) pageSize));

}