Example usage for java.lang Double isFinite

List of usage examples for java.lang Double isFinite

Introduction

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

Prototype

public static boolean isFinite(double d) 

Source Link

Document

Returns true if the argument is a finite floating-point value; returns false otherwise (for NaN and infinity arguments).

Usage

From source file:com.simiacryptus.mindseye.lang.Tensor.java

/**
 * Set.// w w w . ja va  2s .  c o  m
 *
 * @param coords the coords
 * @param value  the value
 */
public void set(@Nonnull final int[] coords, final double value) {
    assert Double.isFinite(value);
    set(index(coords), value);
}

From source file:com.cohort.util.String2.java

/**
 * This makes a JSON version of a number. 
 *
 * @param d/*from   w ww.j av a2s  .  com*/
 * @return "null" if not finite. Return an integer if it ends with ".0".
 *    Else returns the number as a string.
 */
public static String toJson(double d) {
    if (!Double.isFinite(d))
        return "null";
    String s = "" + d;
    return s.endsWith(".0") ? s.substring(0, s.length() - 2) : s;
}

From source file:com.cohort.util.String2.java

/**
 * This returns a double[] with just the finite values from the original 
 * array.// ww w. j  a va 2 s  .com
 *
 * @param dar is a double[]
 * @return a new double[] with just finite values.
 *   dar=null returns null.
 */
public static double[] justFiniteValues(double dar[]) {
    if (dar == null)
        return null;
    int n = dar.length;
    int nFinite = 0;
    double da[] = new double[n];
    for (int i = 0; i < n; i++)
        if (Double.isFinite(dar[i]))
            da[nFinite++] = dar[i];

    //copy to a new array
    double daf[] = new double[nFinite];
    System.arraycopy(da, 0, daf, 0, nFinite);
    return daf;
}

From source file:com.cohort.util.String2.java

/**
 * This returns the number formatted with up to 6 digits to the left and right of
 * the decimal and trailing decimal 0's removed.  
 * If abs(d) &lt; 0.0999995 or abs(d) &gt;= 999999.9999995, the number is displayed
 * in scientific notation (e.g., 8.954321E-5).
 * Thus the maximum length should be 14 characters (-123456.123456).
 * 0 returns "0"// ww  w  . ja  v  a2 s  .co m
 * NaN returns "NaN".
 * Double.POSITIVE_INFINITY returns "Infinity".
 * Double.NEGATIVE_INFINITY returns "-Infinity".
 *
 * @param d a number
 * @return the number converted to a string
 */
public static String genEFormat6(double d) {

    //!finite
    if (!Double.isFinite(d))
        return "" + d;

    //almost 0
    if (Math2.almost0(d))
        return "0";

    //close to 0 
    //String2.log("genEFormat test " + (d*1000) + " " + Math.rint(d*1000));
    if (Math.abs(d) < 0.0999995 && !Math2.almostEqual(6, d * 10000, Math.rint(d * 10000))) { //leave .0021 as .0021, but display .00023 as 2.3e-4
        synchronized (genExpFormat6) {
            return genExpFormat6.format(d);
        }
    }

    //large int
    if (Math.abs(d) < 1e13 && d == Math.rint(d))
        return "" + Math2.roundToLong(d);

    //>10e6
    if (Math.abs(d) >= 999999.9999995) {
        synchronized (genExpFormat6) {
            return genExpFormat6.format(d);
        }
    }

    synchronized (genStdFormat6) {
        return genStdFormat6.format(d);
    }
}

From source file:com.cohort.util.String2.java

/**
 * This returns the number formatted with up to 10 digits to the left and right of
 * the decimal and trailing decimal 0's removed.  
 * If abs(d) &lt; 0.09999999995 or abs(d) &gt;= 999999.99999999995, the number is displayed
 * in scientific notation (e.g., 8.9544680321E-5).
 * Thus the maximum length should be 18 characters (-123456.1234567898).
 * 0 returns "0"/*from   w w  w .  j a va  2 s  . c  om*/
 * NaN returns "NaN".
 * Double.POSITIVE_INFINITY returns "Infinity".
 * Double.NEGATIVE_INFINITY returns "-Infinity".
 *
 * @param d a number
 * @return the number converted to a string
 */
public static String genEFormat10(double d) {

    //!finite
    if (!Double.isFinite(d))
        return "" + d;

    //almost 0
    if (Math2.almost0(d))
        return "0";

    //close to 0 and many sig digits
    //String2.log("genEFormat test " + (d*1000) + " " + Math.rint(d*1000));
    if (Math.abs(d) < 0.09999999995 && !Math2.almostEqual(9, d * 1000000, Math.rint(d * 1000000))) { //leave .0021 as .0021, but display .00023 as 2.3e-4
        synchronized (genExpFormat10) {
            return genExpFormat10.format(d);
        }
    }

    //large int
    if (Math.abs(d) < 1e13 && d == Math.rint(d)) //rint only catches 9 digits(?)
        return "" + Math2.roundToLong(d);

    //>10e6
    if (Math.abs(d) >= 999999.99999999995) {
        synchronized (genExpFormat10) {
            return genExpFormat10.format(d);
        }
    }

    synchronized (genStdFormat10) {
        return genStdFormat10.format(d);
    }
}

From source file:com.cohort.util.String2.java

/** 
 * This converts a double to a rational number (m * 10^t).
 * This is similar to Math2.mantissa and Math2.intExponent, but works via string manipulation
 * to avoid roundoff problems (e.g., with 6.6260755e-24).
 * /*from   ww w.  j av  a  2s. co  m*/
 * @param d
 * @return int[2]: [0]=m, [1]=t.
 *  (or {0, 0} if d=0, or {1, Integer.MAX_VALUE} if !finite(d))
 */
public static int[] toRational(double d) {
    if (d == 0)
        return new int[] { 0, 0 };

    if (!Double.isFinite(d))
        return new int[] { 1, Integer.MAX_VALUE };

    String s = "" + d; //-12.0 or 6.6260755E-24
    //String2.log("\nd=" + d + "\ns=" + s);
    int ten = 0;

    //remove the e
    int epo = s.indexOf('E');
    if (epo > 0) {
        ten = parseInt(s.substring(epo + 1));
        s = s.substring(0, epo);
        //String2.log("remove E s=" + s + " ten=" + ten);
    }

    //remove .0; remove decimal point
    if (s.endsWith(".0"))
        s = s.substring(0, s.length() - 2);
    int dpo = s.indexOf('.');
    if (dpo > 0) {
        ten -= s.length() - dpo - 1;
        s = s.substring(0, dpo) + s.substring(dpo + 1);
        //String2.log("remove . s=" + s + " ten=" + ten);
    }

    //convert s to long
    //need to lose some precision?
    long tl = parseLong(s);
    //String2.log("tl=" + tl + " s=" + s);
    while (Math.abs(tl) > 1000000000) {
        tl = Math.round(tl / 10.0);
        ten++;
        //String2.log("tl=" + tl + " ten=" + ten);
    }
    //remove trailing 0's
    while (tl != 0 && tl / 10 == tl / 10.0) {
        tl /= 10;
        ten++;
        //String2.log("remove 0 tl=" + tl + " ten=" + ten);
    }
    //add up to 3 0's?
    if (tl < 100000 && ten >= 1 && ten <= 3) {
        while (ten > 0) {
            tl *= 10;
            ten--;
        }
    }

    return new int[] { (int) tl, ten }; //safe since large values handled above
}