Example usage for java.lang Number doubleValue

List of usage examples for java.lang Number doubleValue

Introduction

In this page you can find the example usage for java.lang Number doubleValue.

Prototype

public abstract double doubleValue();

Source Link

Document

Returns the value of the specified number as a double .

Usage

From source file:NumberInRange.java

public static boolean isInRange(Number number, BigDecimal min, BigDecimal max) {
    try {//from   ww w .j  av a2 s  .  c om
        BigDecimal bigDecimal = null;
        if (number instanceof Byte || number instanceof Short || number instanceof Integer
                || number instanceof Long) {
            bigDecimal = new BigDecimal(number.longValue());
        } else if (number instanceof Float || number instanceof Double) {
            bigDecimal = new BigDecimal(number.doubleValue());
        } else if (number instanceof BigInteger) {
            bigDecimal = new BigDecimal((BigInteger) number);
        } else if (number instanceof BigDecimal) {
            bigDecimal = (BigDecimal) number;
        } else {
            bigDecimal = new BigDecimal(number.doubleValue());
        }
        return max.compareTo(bigDecimal) >= 0 && min.compareTo(bigDecimal) <= 0;
    } catch (NumberFormatException e) {
        return false;
    }
}

From source file:org.jfree.data.statistics.Statistics.java

/**
 * Calculates the median for a list of values ({@code Number} objects).
 * If {@code copyAndSort} is {@code false}, the list is assumed
 * to be presorted in ascending order by value.
 *
 * @param values  the values ({@code null} permitted).
 * @param copyAndSort  a flag that controls whether the list of values is
 *                     copied and sorted.
 *
 * @return The median.// ww w. j a  v  a 2 s .  co m
 */
public static double calculateMedian(List values, boolean copyAndSort) {

    double result = Double.NaN;
    if (values != null) {
        if (copyAndSort) {
            int itemCount = values.size();
            List copy = new ArrayList(itemCount);
            for (int i = 0; i < itemCount; i++) {
                copy.add(i, values.get(i));
            }
            Collections.sort(copy);
            values = copy;
        }
        int count = values.size();
        if (count > 0) {
            if (count % 2 == 1) {
                if (count > 1) {
                    Number value = (Number) values.get((count - 1) / 2);
                    result = value.doubleValue();
                } else {
                    Number value = (Number) values.get(0);
                    result = value.doubleValue();
                }
            } else {
                Number value1 = (Number) values.get(count / 2 - 1);
                Number value2 = (Number) values.get(count / 2);
                result = (value1.doubleValue() + value2.doubleValue()) / 2.0;
            }
        }
    }
    return result;
}

From source file:NumberInRange.java

public static boolean isInRange(Number number, BigInteger min, BigInteger max) {
    try {//from  ww  w .  j a  va 2s .c  om
        BigInteger bigInteger = null;
        if (number instanceof Byte || number instanceof Short || number instanceof Integer
                || number instanceof Long) {
            bigInteger = BigInteger.valueOf(number.longValue());
        } else if (number instanceof Float || number instanceof Double) {
            bigInteger = new BigDecimal(number.doubleValue()).toBigInteger();
        } else if (number instanceof BigInteger) {
            bigInteger = (BigInteger) number;
        } else if (number instanceof BigDecimal) {
            bigInteger = ((BigDecimal) number).toBigInteger();
        } else {
            // not a standard number
            bigInteger = new BigDecimal(number.doubleValue()).toBigInteger();
        }
        return max.compareTo(bigInteger) >= 0 && min.compareTo(bigInteger) <= 0;
    } catch (NumberFormatException e) {
        return false;
    }
}

From source file:org.jfree.data.statistics.Statistics.java

/**
 * Calculates the median for a sublist within a list of values
 * ({@code Number} objects).  The entire list will be sorted if the
 * {@code ascending} argument is {@code false}.
 *
 * @param values  the values ({@code null} not permitted).
 * @param start  the start index./*w  w  w .  j a v  a  2  s . com*/
 * @param end  the end index.
 * @param copyAndSort  a flag that that controls whether the list of values
 *                     is copied and sorted.
 *
 * @return The median.
 */
public static double calculateMedian(List values, int start, int end, boolean copyAndSort) {

    double result = Double.NaN;
    if (copyAndSort) {
        List working = new ArrayList(end - start + 1);
        for (int i = start; i <= end; i++) {
            working.add(values.get(i));
        }
        Collections.sort(working);
        result = calculateMedian(working, false);
    } else {
        int count = end - start + 1;
        if (count > 0) {
            if (count % 2 == 1) {
                if (count > 1) {
                    Number value = (Number) values.get(start + (count - 1) / 2);
                    result = value.doubleValue();
                } else {
                    Number value = (Number) values.get(start);
                    result = value.doubleValue();
                }
            } else {
                Number value1 = (Number) values.get(start + count / 2 - 1);
                Number value2 = (Number) values.get(start + count / 2);
                result = (value1.doubleValue() + value2.doubleValue()) / 2.0;
            }
        }
    }
    return result;

}

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

/**
 * Creates a new {@link XYSeries} containing the moving averages of one
 * series in the <code>source</code> dataset.
 *
 * @param source  the source dataset./*  w  w w  . ja va  2 s.  c  o  m*/
 * @param series  the series index (zero based).
 * @param name  the name for the new series.
 * @param period  the averaging period.
 * @param skip  the length of the initial skip period.
 *
 * @return The dataset.
 */
public static XYSeries createMovingAverage(XYDataset source, int series, String name, double period,
        double skip) {

    ParamChecks.nullNotPermitted(source, "source");
    if (period < Double.MIN_VALUE) {
        throw new IllegalArgumentException("period must be positive.");
    }
    if (skip < 0.0) {
        throw new IllegalArgumentException("skip must be >= 0.0.");
    }

    XYSeries result = new XYSeries(name);

    if (source.getItemCount(series) > 0) {

        // if the initial averaging period is to be excluded, then
        // calculate the lowest x-value to have an average calculated...
        double first = source.getXValue(series, 0) + skip;

        for (int i = source.getItemCount(series) - 1; i >= 0; i--) {

            // get the current data item...
            double x = source.getXValue(series, i);

            if (x >= first) {
                // work out the average for the earlier values...
                int n = 0;
                double sum = 0.0;
                double limit = x - period;
                int offset = 0;
                boolean finished = false;

                while (!finished) {
                    if ((i - offset) >= 0) {
                        double xx = source.getXValue(series, i - offset);
                        Number yy = source.getY(series, i - offset);
                        if (xx > limit) {
                            if (yy != null) {
                                sum = sum + yy.doubleValue();
                                n = n + 1;
                            }
                        } else {
                            finished = true;
                        }
                    } else {
                        finished = true;
                    }
                    offset = offset + 1;
                }
                if (n > 0) {
                    result.add(x, sum / n);
                } else {
                    result.add(x, null);
                }
            }

        }
    }

    return result;

}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java

/**
 * Returns the square root of the number.  If the number is less than zero
 * and function protection is enabled, this functions the square root of
 * the absolute value of the number.//  ww w .  jav  a2  s . c o  m
 * 
 * @param a the number
 * @return the square root of the number
 * @see Math#sqrt(double)
 */
public static Number sqrt(Number a) {
    if ((a.doubleValue() < 0.0) && Settings.isProtectedFunctions()) {
        return Math.sqrt(Math.abs(a.doubleValue()));
    } else {
        return Math.sqrt(a.doubleValue());
    }
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java

/**
 * Returns the natural logarithm of the number.  If the numbers is
 * negative and function protection is enabled, then this function returns
 * the natural logarithm of the absolute value of the number.  If the
 * number is near zero and function protection is enabled, this function
 * returns {@code 0.0}.//from  w  ww .  j a v a  2 s  .com
 * 
 * @param a the number
 * @return the natural logarithm of the number
 * @see Math#log(double)
 */
public static Number log(Number a) {
    if ((a.doubleValue() < Settings.EPS) && Settings.isProtectedFunctions()) {
        double value = Math.abs(a.doubleValue());

        if (value < Settings.EPS) {
            return 0.0;
        } else {
            return Math.log(value);
        }
    } else {
        return Math.log(a.doubleValue());
    }
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java

/**
 * Returns the base-10 logarithm of the number.  If the numbers is
 * negative and function protection is enabled, then this function returns
 * the base-10 logarithm of the absolute value of the number.  If the
 * number is near zero and function protection is enabled, this function
 * returns {@code 0.0}./*from  w w  w  .  ja va 2  s.co  m*/
 * 
 * @param a the number
 * @return the base-10 logarithm of the number
 * @see Math#log10(double)
 */
public static Number log10(Number a) {
    if ((a.doubleValue() < Settings.EPS) && Settings.isProtectedFunctions()) {
        double value = Math.abs(a.doubleValue());

        if (value < Settings.EPS) {
            return 0.0;
        } else {
            return Math.log10(value);
        }
    } else {
        return Math.log10(a.doubleValue());
    }
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java

/**
 * Returns the value from taking Euler's number <i>e</i> to the power of
 * the given number.//from www  .  j  a  v  a  2s .c om
 * 
 * @param a the number
 * @return  the value from taking Euler's number <i>e</i> to the power of
 *          the given number
 * @see Math#exp(double)
 */
public static Number exp(Number a) {
    return Math.exp(a.doubleValue());
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java

/**
 * Returns the trigonometric sine of the number.
 * //from   ww  w .  ja  va2  s  . c o m
 * @param a the number
 * @return the trigonometric sine of the number
 * @see Math#sin(double)
 */
public static Number sin(Number a) {
    return Math.sin(a.doubleValue());
}