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:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java

/**
 * Returns the hyperbolic arc tangent of the number.
 * /*from   w  w w .j  av  a2 s.  c om*/
 * @param a the number
 * @return the hyperbolic arc tangent of the number
 * @see FastMath#atanh(double)
 */
public static Number atanh(Number a) {
    return FastMath.atanh(a.doubleValue());
}

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

/**
 * Returns the value of the first number to the power of the second.
 * //from  w w w .  ja  v a 2  s.co m
 * @param a the first number
 * @param b the second number
 * @return the value of the first number to the power of the second
 * @see Math#pow(double, double)
 */
public static Number pow(Number a, Number b) {
    return Math.pow(a.doubleValue(), b.doubleValue());
}

From source file:Main.java

/**
 * Casts an object to the specified type
 *
 * @param var//ww w .j  a  v  a2  s. com
 * @param type
 *
 */
static Object convert(Object var, Class type) {
    if (var instanceof Number) { //use number conversion

        Number newNum = (Number) var;

        if (type == Integer.class) {
            return new Integer(newNum.intValue());
        } else if (type == Long.class) {
            return new Long(newNum.longValue());
        } else if (type == Float.class) {
            return new Float(newNum.floatValue());
        } else if (type == Double.class) {
            return new Double(newNum.doubleValue());
        } else if (type == String.class) {
            return new String(newNum.toString());
        }
    } else { //direct cast

        if (type == Integer.class) {
            return new Integer(((Integer) var).intValue());
        } else if (type == Long.class) {
            return new Long(((Long) var).longValue());
        } else if (type == Float.class) {
            return new Float(((Float) var).floatValue());
        } else if (type == Double.class) {
            return new Double(((Double) var).doubleValue());
        } else if (type == String.class) {
            return new String(var.toString());
        }
    }

    return null;
}

From source file:Main.java

static Object convert(Object[] objects, Object var) {
    Object newVar = getObject(objects);

    if (newVar instanceof Number) {
        Number newNum = (Number) var;

        if (newVar instanceof Integer) {
            return new Integer(newNum.intValue());
        } else if (newVar instanceof Long) {
            return new Long(newNum.longValue());
        } else if (newVar instanceof Float) {
            return new Float(newNum.floatValue());
        } else if (newVar instanceof Double) {
            return new Double(newNum.doubleValue());
        } else {//  w w  w . j a va 2 s  .  c  om
            return null;
        }
    } else if (newVar instanceof String) {
        return new String((String) newVar);
    } else {
        //TODO: add other classes
        return null;
    }
}

From source file:Main.java

/**
 * Divides num1 by num2, and return the result in the correct number class.
 * //from  w w  w.  ja v a  2s . c om
 * @param num1 numerator
 * @param num2 denominator
 * @return num1/num2 in the most appropriate class
 */
static Number divide(Number num1, Number num2) {
    Number[] both = new Number[2];
    both[0] = num1;
    both[1] = num2;

    Number division = (Number) getObject(both);

    if (division == null) {
        return null;
    }

    //Integer, Long, Float, Double
    if (division instanceof Integer) {
        //we've got 2 integers, but we're going to use double anyways
        return new Double(num1.doubleValue() / num2.doubleValue());
    } else if (division instanceof Long) {
        return new Long(num1.longValue() / num2.longValue());
    } else if (division instanceof Float) {
        return new Float(num1.floatValue() / num2.floatValue());
    } else if (division instanceof Double) {
        return new Double(num1.doubleValue() / num2.doubleValue());
    } else {
        return null;
    }
}

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

/**
 * Creates a new {@link TimeSeries} containing moving average values for
 * the given series.  If the series is empty (contains zero items), the
 * result is an empty series./*from  w ww  .j  a  va2s  .  co  m*/
 *
 * @param source  the source series.
 * @param name  the name of the new series.
 * @param periodCount  the number of periods used in the average
 *                     calculation.
 * @param skip  the number of initial periods to skip.
 *
 * @return The moving average series.
 */
public static TimeSeries createMovingAverage(TimeSeries source, String name, int periodCount, int skip) {

    ParamChecks.nullNotPermitted(source, "source");
    if (periodCount < 1) {
        throw new IllegalArgumentException("periodCount must be greater " + "than or equal to 1.");
    }

    TimeSeries result = new TimeSeries(name);

    if (source.getItemCount() > 0) {

        // if the initial averaging period is to be excluded, then
        // calculate the index of the
        // first data item to have an average calculated...
        long firstSerial = source.getTimePeriod(0).getSerialIndex() + skip;

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

            // get the current data item...
            RegularTimePeriod period = source.getTimePeriod(i);
            long serial = period.getSerialIndex();

            if (serial >= firstSerial) {
                // work out the average for the earlier values...
                int n = 0;
                double sum = 0.0;
                long serialLimit = period.getSerialIndex() - periodCount;
                int offset = 0;
                boolean finished = false;

                while ((offset < periodCount) && (!finished)) {
                    if ((i - offset) >= 0) {
                        TimeSeriesDataItem item = source.getRawDataItem(i - offset);
                        RegularTimePeriod p = item.getPeriod();
                        Number v = item.getValue();
                        long currentIndex = p.getSerialIndex();
                        if (currentIndex > serialLimit) {
                            if (v != null) {
                                sum = sum + v.doubleValue();
                                n = n + 1;
                            }
                        } else {
                            finished = true;
                        }
                    }
                    offset = offset + 1;
                }
                if (n > 0) {
                    result.add(period, sum / n);
                } else {
                    result.add(period, null);
                }
            }

        }
    }

    return result;

}

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

/**
 * Returns the absolute value of the number.
 * /*from   www.j a  va  2s .c o m*/
 * @param a the number
 * @return the absolute value of the number
 * @see Math#abs(long)
 * @see Math#abs(double)
 */
public static Number abs(Number a) {
    if (isFloatingPoint(a)) {
        return Math.abs(a.doubleValue());
    } else {
        return Math.abs(a.longValue());
    }
}

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

/**
 * Returns the smallest integer value greater than or equal to the given
 * number./*  www.  j a v  a 2s .c  o m*/
 * 
 * @param a the number
 * @return the smallest integer value greater than or equal to the given
 *         number
 * @see Math#ceil(double)
 */
public static Number ceil(Number a) {
    if (isFloatingPoint(a)) {
        return Math.ceil(a.doubleValue());
    } else {
        return a.longValue();
    }
}

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

/**
 * Returns the largest integer value less than or equal to the given
 * number.//from   w w  w.  j av a2s  .co  m
 * 
 * @param a the number
 * @return the largest integer value less than or equal to the given
 *         number
 * @see Math#floor(double)
 */
public static Number floor(Number a) {
    if (isFloatingPoint(a)) {
        return Math.floor(a.doubleValue());
    } else {
        return a.longValue();
    }
}

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

/**
 * Returns the value of the number rounded to the nearest integer.
 * /*from  ww  w  .  java2  s  .  c  om*/
 * @param a the number
 * @return the value of the number rounded to the nearest integer
 * @see Math#round(double)
 */
public static Number round(Number a) {
    if (isFloatingPoint(a)) {
        return Math.round(a.doubleValue());
    } else {
        return a.longValue();
    }
}