Example usage for java.lang Double isNaN

List of usage examples for java.lang Double isNaN

Introduction

In this page you can find the example usage for java.lang Double isNaN.

Prototype

public static boolean isNaN(double v) 

Source Link

Document

Returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.

Usage

From source file:bb.mcmc.analysis.ESSConvergeStat.java

@Override
protected boolean checkEachConverged(double stat, String key) {

    boolean isConverged = true;

    if (Double.isNaN(stat)) {
        System.err.println(STATISTIC_NAME + " could not be calculated for variable with id " + key + "("
                + Double.NaN + "). Check log file for details. ");
    } else if (stat < essThreshold && !Double.isInfinite(stat)) {// || Double.isNaN(stat)) {
        isConverged = false;/*from w  ww.j a  v a2  s. com*/
    }

    return isConverged;

}

From source file:edu.jhuapl.bsp.detector.OpenMath.java

public static double sum(double[] in) {
    if (in != null) {
        double sum = 0;
        for (int i = 0; i < in.length; i++) {
            sum += in[i];//  ww  w .  ja v a  2s.c o  m
        }
        if (Double.isNaN(sum) || Double.isInfinite(sum)) {
            return 0;
        } else {
            return sum;
        }
    }
    return 0;
}

From source file:com.opengamma.analytics.financial.model.volatility.BlackScholesFormulaRepository.java

/**
* The <b>spot</b> price/*from  w w w. ja va2s.c  om*/
* @param spot The spot value of the underlying
* @param strike The Strike
* @param timeToExpiry The time-to-expiry
* @param lognormalVol The log-normal volatility
* @param interestRate The interest rate 
* @param costOfCarry The cost-of-carry  rate
* @param isCall True for calls, false for puts
* @return The <b>spot</b> price
*/
@ExternalFunction
public static double price(final double spot, final double strike, final double timeToExpiry,
        final double lognormalVol, final double interestRate, final double costOfCarry, final boolean isCall) {
    ArgumentChecker.isTrue(spot >= 0.0, "negative/NaN spot; have {}", spot);
    ArgumentChecker.isTrue(strike >= 0.0, "negative/NaN strike; have {}", strike);
    ArgumentChecker.isTrue(timeToExpiry >= 0.0, "negative/NaN timeToExpiry; have {}", timeToExpiry);
    ArgumentChecker.isTrue(lognormalVol >= 0.0, "negative/NaN lognormalVol; have {}", lognormalVol);
    ArgumentChecker.isFalse(Double.isNaN(interestRate), "interestRate is NaN");
    ArgumentChecker.isFalse(Double.isNaN(costOfCarry), "costOfCarry is NaN");

    if (interestRate > LARGE) {
        return 0.;
    }
    if (-interestRate > LARGE) {
        return Double.POSITIVE_INFINITY;
    }
    double discount = Math.abs(interestRate) < SMALL ? 1. : Math.exp(-interestRate * timeToExpiry);

    if (costOfCarry > LARGE) {
        return isCall ? Double.POSITIVE_INFINITY : 0.;
    }
    if (-costOfCarry > LARGE) {
        final double res = isCall ? 0. : (discount > SMALL ? strike * discount : 0.);
        return Double.isNaN(res) ? discount : res;
    }
    double factor = Math.exp(costOfCarry * timeToExpiry);

    if (spot > LARGE * strike) {
        final double tmp = Math.exp((costOfCarry - interestRate) * timeToExpiry);
        return isCall ? (tmp > SMALL ? spot * tmp : 0.) : 0.;
    }
    if (LARGE * spot < strike) {
        return (isCall || discount < SMALL) ? 0. : strike * discount;
    }
    if (spot > LARGE && strike > LARGE) {
        final double tmp = Math.exp((costOfCarry - interestRate) * timeToExpiry);
        return isCall ? (tmp > SMALL ? spot * tmp : 0.) : (discount > SMALL ? strike * discount : 0.);
    }

    final double rootT = Math.sqrt(timeToExpiry);
    double sigmaRootT = lognormalVol * rootT;
    if (Double.isNaN(sigmaRootT)) {
        sigmaRootT = 1.; //ref value is returned
    }

    final int sign = isCall ? 1 : -1;
    final double rescaledSpot = factor * spot;
    if (sigmaRootT < SMALL) {
        final double res = isCall ? (rescaledSpot > strike ? discount * (rescaledSpot - strike) : 0.)
                : (rescaledSpot < strike ? discount * (strike - rescaledSpot) : 0.);
        return Double.isNaN(res) ? sign * (spot - discount * strike) : res;
    }

    double d1 = 0.;
    double d2 = 0.;
    if (Math.abs(spot - strike) < SMALL || sigmaRootT > LARGE) {
        final double coefD1 = (costOfCarry / lognormalVol + 0.5 * lognormalVol);
        final double coefD2 = (costOfCarry / lognormalVol - 0.5 * lognormalVol);
        final double tmpD1 = coefD1 * rootT;
        final double tmpD2 = coefD2 * rootT;
        d1 = Double.isNaN(tmpD1) ? 0. : tmpD1;
        d2 = Double.isNaN(tmpD2) ? 0. : tmpD2;
    } else {
        final double tmp = costOfCarry * rootT / lognormalVol;
        final double sig = (costOfCarry >= 0.) ? 1. : -1.;
        final double scnd = Double.isNaN(tmp)
                ? ((lognormalVol < LARGE && lognormalVol > SMALL) ? sig / lognormalVol : sig * rootT)
                : tmp;
        d1 = Math.log(spot / strike) / sigmaRootT + scnd + 0.5 * sigmaRootT;
        d2 = d1 - sigmaRootT;
    }
    //    if (Double.isNaN(d1) || Double.isNaN(d2)) {
    //      throw new IllegalArgumentException("NaN found");
    //    }
    final double res = sign * discount
            * (rescaledSpot * NORMAL.getCDF(sign * d1) - strike * NORMAL.getCDF(sign * d2));
    return Double.isNaN(res) ? 0. : Math.max(res, 0.);
}

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.)// w w w . j ava2 s  .  c o  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:com.bdaum.zoom.gps.internal.MapDisplay.java

public Location defineLocation(Location loc) {
    IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    if (activeWorkbenchWindow != null) {
        MapDialog dialog = new MapDialog(activeWorkbenchWindow.getShell(), loc);
        if (dialog.open() == Window.OK) {
            final Location result = dialog.getResult();
            if (result != null && result.getLatitude() != null && !Double.isNaN(result.getLatitude())
                    && result.getLongitude() != null && !Double.isNaN(result.getLongitude())) {
                BusyIndicator.showWhile(activeWorkbenchWindow.getShell().getDisplay(), () -> {
                    try {
                        Place place = GpsUtilities.fetchPlaceInfo(result.getLatitude(), result.getLongitude());
                        if (place != null) {
                            if (!Double.isNaN(place.getLat()) && !Double.isNaN(place.getLon())) {
                                double elevation = GpsUtilities.fetchElevation(place.getLat(), place.getLon());
                                if (!Double.isNaN(elevation))
                                    place.setElevation(elevation);
                            }/*from ww  w .  j av  a  2  s. co m*/
                            GpsUtilities.transferPlacedata(place, result);
                        }
                    } catch (SocketTimeoutException e1) {
                        GpsActivator.getDefault().logError(
                                Messages.getString("MapDisplay.Naming_service_connection_timed_out"), //$NON-NLS-1$
                                e1);
                    } catch (HttpException e2) {
                        GpsActivator.getDefault().logError(Messages.getString("MapDisplay.http_exception"), //$NON-NLS-1$
                                e2);
                    } catch (IOException e3) {
                        GpsActivator.getDefault().logError(Messages.getString("MapDisplay.Error_when_parsing"), //$NON-NLS-1$
                                e3);
                    } catch (SAXException e4) {
                        GpsActivator.getDefault().logError(
                                Messages.getString("MapDisplay.XML_problem_when_parsing"), //$NON-NLS-1$
                                e4);
                    } catch (WebServiceException e5) {
                        GpsActivator.getDefault().logError(
                                Messages.getString("MapDisplay.Webservice_problem_when_naming_places"), //$NON-NLS-1$
                                e5);
                    } catch (ParserConfigurationException e6) {
                        GpsActivator.getDefault().logError(
                                Messages.getString("MapDisplay.internal_error_configuring_sax"), //$NON-NLS-1$
                                e6);
                    }
                });
                return result;
            }
        }
    }
    return null;
}

From source file:bb.mcmc.analysis.GewekeConvergeStat.java

@Override
protected boolean checkEachConverged(double stat, String key) {

    boolean isConverged = true;

    if (Double.isNaN(stat)) {
        System.err.println(STATISTIC_NAME + " could not be calculated for variable with id " + key + "("
                + Double.NaN + "). Geweke algorithm might not converged. Check log file for details. ");
    } else if (Math.abs(stat) > gewekeThreshold && !Double.isInfinite(stat)) {
        isConverged = false;/*from   w w  w .  j  a va 2s.  co  m*/
    }
    return isConverged;

}

From source file:org.jfree.data.time.TimeTableXYDatasetTest.java

/**
 * Some checks for a simple dataset./*from w w w  . ja v a2s.c  o  m*/
 */
@Test
public void testStandard() {
    TimeTableXYDataset d = new TimeTableXYDataset();
    d.add(new Year(1999), 1.0, "Series 1");
    assertEquals(d.getItemCount(), 1);
    assertEquals(d.getSeriesCount(), 1);
    d.add(new Year(2000), 2.0, "Series 2");
    assertEquals(d.getItemCount(), 2);
    assertEquals(d.getSeriesCount(), 2);
    assertEquals(d.getYValue(0, 0), 1.0, DELTA);
    assertTrue(Double.isNaN(d.getYValue(0, 1)));
    assertTrue(Double.isNaN(d.getYValue(1, 0)));
    assertEquals(d.getYValue(1, 1), 2.0, DELTA);
}

From source file:com.cloudera.oryx.common.OryxTest.java

public static void assertNaN(double value) {
    Assert.assertTrue(value + " is not NaN", Double.isNaN(value));
}

From source file:IEEE754rUtils.java

/**
 * <p>Gets the minimum of two <code>double</code> values.</p>
 * /* www  . j  a  v a  2s. c  o m*/
 * <p>NaN is only returned if all numbers are NaN as per IEEE-754r. </p>
 * 
 * @param a  value 1
 * @param b  value 2
 * @return  the smallest of the values
 */
public static double min(double a, double b) {
    if (Double.isNaN(a)) {
        return b;
    } else if (Double.isNaN(b)) {
        return a;
    } else {
        return Math.min(a, b);
    }
}

From source file:com.davidsoergel.stats.SimpleXYZSeries.java

public void incrementPoint(double x, double y, double zIncrement) throws StatsException {
    if (Double.isNaN(x) || Double.isInfinite(x)) {
        throw new StatsException("Invalid x value in SimpleXYZSeries: " + x);
    }/*from ww  w.j  a va2 s  .  c om*/
    if (Double.isNaN(y) || Double.isInfinite(y)) {
        throw new StatsException("Invalid y value in SimpleXYZSeries: " + y);
    }
    if (Double.isNaN(zIncrement) || Double.isInfinite(zIncrement)) {
        throw new StatsException("Invalid zIncrement value in SimpleXYZSeries: " + zIncrement);
    }
    XYZPoint currentPoint = points.get(x, y);
    if (currentPoint != null) {
        currentPoint.z += zIncrement;
        updateBounds(x, y, currentPoint.z);
    } else {
        points.put(x, y, new XYZPoint(x, y, zIncrement));

        updateBounds(x, y, zIncrement);
    }
}