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:projects.tgas.exec.HrDiagram.java

/**
 * Main constructor./* ww w.j av a 2  s. co m*/
 */
public HrDiagram() {

    tgasStars = TgasUtils.loadTgasCatalogue();

    method = METHOD.NAIVE;
    fMax = 1.0;

    final JComboBox<METHOD> methodComboBox = new JComboBox<METHOD>(METHOD.values());
    methodComboBox.setSelectedItem(method);
    methodComboBox.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            method = (METHOD) methodComboBox.getSelectedItem();
            updateChart();
        }
    });

    final JSlider fSlider = GuiUtil.buildSlider(0.0, 2.0, 5, "%3.3f");
    fSlider.setValue((int) Math.rint(100.0 * fMax / 2.0));
    final JLabel fLabel = new JLabel(getFLabel());
    // Create a change listener fot these
    ChangeListener cl = new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            JSlider source = (JSlider) e.getSource();

            if (source == fSlider) {
                // Compute fractional parallax error from slider position
                double newF = (2.0 * source.getValue() / 100.0);
                fMax = newF;
                fLabel.setText(getFLabel());
            }
            updateChart();
        }
    };
    fSlider.addChangeListener(cl);
    // Add a bit of padding to space things out
    fSlider.setBorder(new EmptyBorder(5, 5, 5, 5));

    // Present controls below the HR diagram
    JPanel controls = new JPanel(new GridLayout(2, 2));
    controls.add(new JLabel("Distance estimation method:"));
    controls.add(methodComboBox);
    controls.add(fLabel);
    controls.add(fSlider);

    // Initialise the ChartPanel
    updateChart();

    // Build the panel contents
    setLayout(new BorderLayout());
    add(hrDiagPanel, BorderLayout.CENTER);
    add(controls, BorderLayout.SOUTH);
}

From source file:org.renjin.primitives.MathExt.java

@Builtin
@Deferrable
@DataParallel
public static double round(double x) {
    return Math.rint(x);
}

From source file:sadl.run.datagenerators.AlgoWeaknessesDataGenerator.java

@SuppressWarnings("null")
private void run() {

    MasterSeed.setSeed(seed);/*from ww  w  .j av  a 2s .co  m*/
    rndm = MasterSeed.nextRandom();

    PDRTA nPDRTA = null;
    List<PDRTA> aPDRTAs = null;

    try {
        nPDRTA = PDRTA.parse(normalPDRTA.toFile());
        aPDRTAs = parse(anomalyPDRTAs);
    } catch (final IOException e) {
        e.printStackTrace();
    }

    final int numTrain = (int) Math.rint(setSize * (1.0 - testAmount));
    final int numTestNeg = (int) Math.rint((setSize - numTrain) * (1.0 - anomalyAmount));
    final int numTestPos = setSize - (numTrain + numTestNeg);

    for (int i = 0; i < aPDRTAs.size(); i++) {
        for (int j = 0; j < numSets; j++) {
            final TimedInput inpTrain = sample(nPDRTA, numTrain, ClassLabel.NORMAL);
            final TimedInput inpTestNeg = sample(nPDRTA, numTestNeg, ClassLabel.NORMAL);
            final TimedInput inpTestPos = sample(aPDRTAs.get(i), numTestPos, ClassLabel.ANOMALY);
            final Path outFile = outDir
                    .resolve(anomalyPDRTAs.get(i).getFileName().toString().replaceAll("\\.[^\\.]+$", "") + "_"
                            + j + ".txt");
            write(inpTrain, inpTestNeg, inpTestPos, outFile);
        }
    }

}

From source file:projects.hip.exec.HrDiagram.java

/**
 * Main constructor./*  w  ww  .  j av  a  2  s .  c o m*/
 */
public HrDiagram() {

    hipStars = HipUtils.loadHipCatalogue();

    method = METHOD.NAIVE;
    fMax = 1.0;

    final JComboBox<METHOD> methodComboBox = new JComboBox<METHOD>(METHOD.values());
    methodComboBox.setSelectedItem(method);
    methodComboBox.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            method = (METHOD) methodComboBox.getSelectedItem();
            updateChart();
        }
    });

    final JSlider fSlider = GuiUtil.buildSlider(0.0, 2.0, 5, "%3.3f");
    fSlider.setValue((int) Math.rint(100.0 * fMax / 2.0));
    final JLabel fLabel = new JLabel(getFLabel());
    // Create a change listener fot these
    ChangeListener cl = new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            JSlider source = (JSlider) e.getSource();

            if (source == fSlider) {
                // Compute fractional parallax error from slider position
                double newF = (2.0 * source.getValue() / 100.0);
                fMax = newF;
                fLabel.setText(getFLabel());
            }
            updateChart();
        }
    };
    fSlider.addChangeListener(cl);
    // Add a bit of padding to space things out
    fSlider.setBorder(new EmptyBorder(5, 5, 5, 5));

    // Present controls below the HR diagram
    JPanel controls = new JPanel(new GridLayout(2, 2));
    controls.add(new JLabel("Distance estimation method:"));
    controls.add(methodComboBox);
    controls.add(fLabel);
    controls.add(fSlider);

    // Initialise the ChartPanel
    updateChart();

    // Build the panel contents
    setLayout(new BorderLayout());
    add(hrDiagPanel, BorderLayout.WEST);
    add(dDistPanel, BorderLayout.EAST);
    add(controls, BorderLayout.SOUTH);
}

From source file:org.renjin.MathExt.java

public static double round(double x, int digits) {
    // adapted from the nmath library, fround.c
    /* = 308 (IEEE); was till R 0.99: (DBL_DIG - 1) */
    /* Note that large digits make sense for very small numbers */
    double sgn;//w  w  w.  java2  s .  c o  m
    int dig;

    if (Double.isNaN(x) || Double.isNaN(digits)) {
        return x + digits;
    }
    if (Double.isInfinite(x)) {
        return x;
    }

    if (digits == Double.POSITIVE_INFINITY) {
        return x;
    } else if (digits == Double.NEGATIVE_INFINITY) {
        return 0.0;
    }

    if (digits > MAX_DIGITS) {
        digits = MAX_DIGITS;
    }
    dig = (int) Math.floor(digits + 0.5);

    if (x < 0.) {
        sgn = -1.;
        x = -x;
    } else {
        sgn = 1.;
    }
    if (dig == 0) {
        return sgn * Math.rint(x);
    } else if (dig > 0) {
        double pow10 = Math.pow(10., dig);
        double intx = Math.floor(x);
        return sgn * (intx + Math.rint((x - intx) * pow10) / pow10);
    } else {
        double pow10 = Math.pow(10., -dig);
        return sgn * Math.rint(x / pow10) * pow10;
    }
}

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

@Test
public void testVector() throws IOException {
    SchemaSampler s = new SchemaSampler(
            Resources.asCharSource(Resources.getResource("schema029.json"), Charsets.UTF_8).read());
    for (int i = 0; i < 10; i++) {
        JsonNode data = s.sample();//from w w w.ja  v a2 s  .c o m
        /*
        {
        "class": "vector",
        "name": "prices",
        "mean": 4.65,
        "sd": 0.01,
        "length": 10000,
        "transform": "exp",
        "seed": 1,
        },
        */
        JsonNode v = data.get("prices");
        assertTrue(v.isArray());
        assertEquals(10000, v.size());
        double[] v1 = new double[10000];
        double[] v2 = new double[10000];
        for (int j = 0; j < 10000; j++) {
            v1[j] = v.get(j).asDouble();
            v2[j] = Math.log(v1[j]);
        }
        assertEquals(100, median(v1), 0.03);
        assertEquals(100, mean(v1), 0.05);
        assertEquals(Math.log(100), mean(v2), 0.001);
        assertEquals(0.01, sd(v2), 0.0003);
        assertTrue(isNormal(v2, Math.log(100), 0.01));

        /*
        {
        "class": "vector",
        "name": "zero",
        "mean": 0,
        "sd": 10,
        "length": 10000,
        "seed": 2
        },
        */
        v = data.get("zero");
        assertTrue(v.isArray());
        for (int j = 0; j < 10000; j++) {
            v1[j] = v.get(j).asDouble();
        }
        assertEquals(0, mean(v1), 0.3);
        assertEquals(10, sd(v1), 0.2);
        assertTrue(isNormal(v1, 0, 10));
        /*
        {
        "class": "vector",
        "name": "clipped",
        "mean": 0,
        "sd": 10,
        "length": 10000,
        "max": 0,
        "seed": 3
        },
        */
        v = data.get("clipped");
        assertTrue(v.isArray());
        Random rand = new Random();
        for (int j = 0; j < 10000; j++) {
            v1[j] = v.get(j).asDouble();
            assertTrue(v1[j] <= 0);
            v1[j] = v1[j] * (rand.nextBoolean() ? 1 : -1);
        }
        assertEquals(0, mean(v1), 0.3);
        assertEquals(10, sd(v1), 0.3);
        assertTrue(isNormal(v1, 0, 10));

        /*
        {
        "class": "vector",
        "name": "ten",
        "min": 1,
        "max": 10,
        "length": 20000,
        "transform": "log",
        "seed": 4
        }
        ]
                */
        v = data.get("ten");
        assertTrue(v.isArray());
        for (int j = 0; j < 10000; j++) {
            v1[j] = v.get(j).asDouble();
            v2[j] = Math.exp(v1[j]);
            assertTrue(v1[j] >= 1);
            assertTrue(v1[j] <= 10);
        }
        assertTrue(isUniform(v2, Math.exp(1), Math.exp(10)));

        v = data.get("coarse");
        assertTrue(v.isArray());
        for (int j = 0; j < 10000; j++) {
            double x = v.get(j).asDouble();
            assertTrue(x >= 1);
            assertTrue(x <= 10);
            assertEquals(Math.rint(x / 0.1) * 0.1, x, 1e-10);
        }
    }
}

From source file:edu.kit.dama.transfer.client.impl.CLDownloadClient.java

@Override
public final void transferFinished(TransferTask pTask) {
    int finished = (int) Math.rint((double) getTransferClient().getTransferInfo().getFinishedTaskCount()
            / (double) getTransferClient().getTransferInfo().getTaskCount() * 100d);
    System.out.printf("Transfer %1$s -> %2$s finished. [%3$s%%]%n",
            new Object[] { formatString(pTask.getSourceFile().getUrl().toString()),
                    formatString(pTask.getTargetFile().getUrl().toString()), Integer.toString(finished) });
}

From source file:org.renjin.primitives.MathExt.java

@Builtin
@Deferrable//from   ww  w. j a v  a2s.  co m
@DataParallel
public static double round(double x, int digits) {
    // adapted from the nmath library, fround.c

    double sign;
    int dig;

    if (Double.isNaN(x) || Double.isNaN(digits)) {
        return x + digits;
    }
    if (!DoubleVector.isFinite(x)) {
        return x;
    }

    if (digits == Double.POSITIVE_INFINITY) {
        return x;
    } else if (digits == Double.NEGATIVE_INFINITY) {
        return 0.0;
    }

    if (digits > MAX_DIGITS) {
        digits = MAX_DIGITS;
    }
    dig = (int) Math.floor(digits + 0.5);

    if (x < 0.) {
        sign = -1.;
        x = -x;
    } else {
        sign = 1.;
    }
    if (dig == 0) {
        return sign * Math.rint(x);
    } else if (dig > 0) {
        // round to a specific number of decimal places
        return sign * new BigDecimal(x).setScale(digits, RoundingMode.HALF_EVEN).doubleValue();
    } else {
        // round to the nearest power of 10
        double pow10 = Math.pow(10., -dig);
        return sign * Math.rint(x / pow10) * pow10;
    }
}

From source file:de.tor.tribes.ui.windows.ClockFrame.java

protected void updateTime(String time, int millis) {
    if (isVisible()) {
        jLabel1.setText(time);/*from  w  w  w  . j  a v a  2s.co  m*/
        double markerMin = 0;
        double markerMax = 1000;
        double diff = markerMax - markerMin;
        float ratio = 0;
        if (diff > 0) {
            ratio = (float) ((millis - markerMin) / (markerMax - markerMin));
        }

        Color c1 = Color.GREEN;
        if (millis >= 500) {
            c1 = Color.YELLOW;
        }
        Color c2 = Color.RED;
        if (millis < 500) {
            c2 = Color.YELLOW;
            ratio += .5f;
        } else {
            ratio -= .5f;
        }
        int red = (int) Math.rint(c2.getRed() * ratio + c1.getRed() * (1f - ratio));
        int green = (int) Math.rint(c2.getGreen() * ratio + c1.getGreen() * (1f - ratio));
        int blue = (int) Math.rint(c2.getBlue() * ratio + c1.getBlue() * (1f - ratio));

        red = (red < 0) ? 0 : red;
        green = (green < 0) ? 0 : green;
        blue = (blue < 0) ? 0 : blue;
        red = (red > 255) ? 255 : red;
        green = (green > 255) ? 255 : green;
        blue = (blue > 255) ? 255 : blue;
        cp.setForeground(new Color(red, green, blue));
        cp.setValue(millis);
    }

    for (final TimerPanel p : timers.toArray(new TimerPanel[timers.size()])) {
        if (p.isExpired()) {
            SystrayHelper.showInfoMessage("Timer  '" + p.getName() + "' ist abgelaufen");
            //moved playing the sound to a new Thread because of graphic problems
            new Thread(new Runnable() {
                @Override
                public void run() {
                    playSound(p.getSound());
                }
            }).start();
            removeTimer(p);
        } else {
            p.update();
        }
    }
}

From source file:us.mn.state.health.lims.common.provider.query.TestReflexCD4Provider.java

private String createTestReflexXML(String resultIds, String values, String childRow, StringBuilder xml) {
    String[] resultIdSeries = resultIds.split(VALUE_SEPERATOR);

    String CD4Result = "";
    String GBResult = "";
    String LymphResult = "";

    if (resultIdSeries.length > 0) {
        String[] valueSeries = values.split(VALUE_SEPERATOR);

        for (int i = 0; i < resultIdSeries.length; i++) {
            Result result = resultDAO.getResultById(resultIdSeries[i]);

            if (result != null) {
                String testId = result.getAnalysis().getTest().getId();

                if (CD4_TEST_ID.equals(testId)) {
                    CD4Result = valueSeries[i];
                } else if (LYMPH_TEST_ID.equals(testId)) {
                    LymphResult = valueSeries[i];
                } else if (GB_TEST_ID.equals(testId)) {
                    GBResult = valueSeries[i];
                }/*from w  ww  .jav a  2s .c  o  m*/
            }
        }

        CD4Result = fetchIfNeeded(CD4_TEST_ID, CD4Result, resultIdSeries[0]);
        LymphResult = fetchIfNeeded(LYMPH_TEST_ID, LymphResult, resultIdSeries[0]);
        GBResult = fetchIfNeeded(GB_TEST_ID, GBResult, resultIdSeries[0]);

        double result = 0;
        try {
            result = Double.parseDouble(CD4Result) * Double.parseDouble(GBResult)
                    * Double.parseDouble(LymphResult) * 0.1;
            result = Math.rint(result);
        } catch (NumberFormatException e) {
            return INVALID;
        }

        XMLUtil.appendKeyValue("childRow", childRow, xml);
        XMLUtil.appendKeyValue("value", String.valueOf(result), xml);
        return VALID;
    }

    return INVALID;
}