Example usage for java.lang ArithmeticException ArithmeticException

List of usage examples for java.lang ArithmeticException ArithmeticException

Introduction

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

Prototype

public ArithmeticException(String s) 

Source Link

Document

Constructs an ArithmeticException with the specified detail message.

Usage

From source file:org.nd4j.linalg.util.BigDecimalMath.java

/**
 * The natural logarithm./* w ww.  j a  v a2 s  . c o  m*/
 *
 * @param r  The main argument, a strictly positive value.
 * @param mc The requirements on the precision.
 * @return ln(r).
 */
static public BigDecimal log(final Rational r, final MathContext mc) {
    /* the value is undefined if x is negative.
     */
    if (r.compareTo(Rational.ZERO) <= 0) {
        throw new ArithmeticException("Cannot take log of negative " + r.toString());
    } else if (r.compareTo(Rational.ONE) == 0) {
        return BigDecimal.ZERO;
    } else {
        /* log(r+epsr) = log(r)+epsr/r. Convert the precision to an absolute error in the result.
         * eps contains the required absolute error of the result, epsr/r.
         */
        double eps = prec2err(Math.log(r.doubleValue()), mc.getPrecision());
        /* Convert this further into a requirement of the relative precision in r, given that
         * epsr/r is also the relative precision of r. Add one safety digit.
         */
        MathContext mcloc = new MathContext(1 + err2prec(eps));
        final BigDecimal resul = log(r.BigDecimalValue(mcloc));
        return resul.round(mc);
    }
}

From source file:org.nd4j.linalg.util.BigDecimalMath.java

/**
 * Power function./*  w w  w  .  j  a v  a  2s .  c om*/
 *
 * @param x Base of the power.
 * @param y Exponent of the power.
 * @return x^y.
 * The estimation of the relative error in the result is |log(x)*err(y)|+|y*err(x)/x|
 */
static public BigDecimal pow(final BigDecimal x, final BigDecimal y) {
    if (x.compareTo(BigDecimal.ZERO) < 0) {
        throw new ArithmeticException("Cannot power negative " + x.toString());
    } else if (x.compareTo(BigDecimal.ZERO) == 0) {
        return BigDecimal.ZERO;
    } else {
        /* return x^y = exp(y*log(x)) ;
         */
        BigDecimal logx = log(x);
        BigDecimal ylogx = y.multiply(logx);
        BigDecimal resul = exp(ylogx);
        /* The estimation of the relative error in the result is |log(x)*err(y)|+|y*err(x)/x|
         */
        double errR = Math.abs(logx.doubleValue() * y.ulp().doubleValue() / 2.)
                + Math.abs(y.doubleValue() * x.ulp().doubleValue() / 2. / x.doubleValue());
        MathContext mcR = new MathContext(err2prec(1.0, errR));
        return resul.round(mcR);
    }
}

From source file:edu.cmu.tetrad.util.StatUtils.java

/**
 * @param array a long array.//ww  w .ja  va2  s. c o m
 * @param N     the number of values of array which should be considered.
 * @return the skew of the first N values in array.
 */
public static double skewness(long array[], int N) {
    double mean = StatUtils.mean(array, N);
    double secondMoment = 0.0; // StatUtils.variance(array, N);
    double thirdMoment = 0.0;

    for (int j = 0; j < N; j++) {
        double s = array[j] - mean;
        secondMoment += s * s;
        thirdMoment += s * s * s;
    }

    if (secondMoment == 0) {
        throw new ArithmeticException("StatUtils.skew:  There is no skew " + "when the variance is zero.");
    }

    //        thirdMoment /= (N * Math.pow(secondMoment, 1.5));
    return thirdMoment / Math.pow(secondMoment, 1.5);
}

From source file:com.s3d.webapps.util.time.DateUtils.java

/**
 * <p>Internal calculation method.</p>
 * /*from   w w w .ja  v a 2s  . c om*/
 * @param val  the calendar
 * @param field  the field constant
 * @param modType  type to truncate, round or ceiling
 * @throws ArithmeticException if the year is over 280 million
 */
private static void modify(Calendar val, int field, int modType) {
    if (val.get(Calendar.YEAR) > 280000000) {
        throw new ArithmeticException("Calendar value too large for accurate calculations");
    }

    if (field == Calendar.MILLISECOND) {
        return;
    }

    // ----------------- Fix for LANG-59 ---------------------- START ---------------
    // see http://issues.apache.org/jira/browse/LANG-59
    //
    // Manually truncate milliseconds, seconds and minutes, rather than using
    // Calendar methods.

    Date date = val.getTime();
    long time = date.getTime();
    boolean done = false;

    // truncate milliseconds
    int millisecs = val.get(Calendar.MILLISECOND);
    if (MODIFY_TRUNCATE == modType || millisecs < 500) {
        time = time - millisecs;
    }
    if (field == Calendar.SECOND) {
        done = true;
    }

    // truncate seconds
    int seconds = val.get(Calendar.SECOND);
    if (!done && (MODIFY_TRUNCATE == modType || seconds < 30)) {
        time = time - (seconds * 1000L);
    }
    if (field == Calendar.MINUTE) {
        done = true;
    }

    // truncate minutes
    int minutes = val.get(Calendar.MINUTE);
    if (!done && (MODIFY_TRUNCATE == modType || minutes < 30)) {
        time = time - (minutes * 60000L);
    }

    // reset time
    if (date.getTime() != time) {
        date.setTime(time);
        val.setTime(date);
    }
    // ----------------- Fix for LANG-59 ----------------------- END ----------------

    boolean roundUp = false;
    for (int i = 0; i < fields.length; i++) {
        for (int j = 0; j < fields[i].length; j++) {
            if (fields[i][j] == field) {
                //This is our field... we stop looping
                if (modType == MODIFY_CEILING || (modType == MODIFY_ROUND && roundUp)) {
                    if (field == DateUtils.SEMI_MONTH) {
                        //This is a special case that's hard to generalize
                        //If the date is 1, we round up to 16, otherwise
                        //  we subtract 15 days and add 1 month
                        if (val.get(Calendar.DATE) == 1) {
                            val.add(Calendar.DATE, 15);
                        } else {
                            val.add(Calendar.DATE, -15);
                            val.add(Calendar.MONTH, 1);
                        }
                        // ----------------- Fix for LANG-440 ---------------------- START ---------------
                    } else if (field == Calendar.AM_PM) {
                        // This is a special case
                        // If the time is 0, we round up to 12, otherwise
                        //  we subtract 12 hours and add 1 day
                        if (val.get(Calendar.HOUR_OF_DAY) == 0) {
                            val.add(Calendar.HOUR_OF_DAY, 12);
                        } else {
                            val.add(Calendar.HOUR_OF_DAY, -12);
                            val.add(Calendar.DATE, 1);
                        }
                        // ----------------- Fix for LANG-440 ---------------------- END ---------------
                    } else {
                        //We need at add one to this field since the
                        //  last number causes us to round up
                        val.add(fields[i][0], 1);
                    }
                }
                return;
            }
        }
        //We have various fields that are not easy roundings
        int offset = 0;
        boolean offsetSet = false;
        //These are special types of fields that require different rounding rules
        switch (field) {
        case DateUtils.SEMI_MONTH:
            if (fields[i][0] == Calendar.DATE) {
                //If we're going to drop the DATE field's value,
                //  we want to do this our own way.
                //We need to subtrace 1 since the date has a minimum of 1
                offset = val.get(Calendar.DATE) - 1;
                //If we're above 15 days adjustment, that means we're in the
                //  bottom half of the month and should stay accordingly.
                if (offset >= 15) {
                    offset -= 15;
                }
                //Record whether we're in the top or bottom half of that range
                roundUp = offset > 7;
                offsetSet = true;
            }
            break;
        case Calendar.AM_PM:
            if (fields[i][0] == Calendar.HOUR_OF_DAY) {
                //If we're going to drop the HOUR field's value,
                //  we want to do this our own way.
                offset = val.get(Calendar.HOUR_OF_DAY);
                if (offset >= 12) {
                    offset -= 12;
                }
                roundUp = offset >= 6;
                offsetSet = true;
            }
            break;
        }
        if (!offsetSet) {
            int min = val.getActualMinimum(fields[i][0]);
            int max = val.getActualMaximum(fields[i][0]);
            //Calculate the offset from the minimum allowed value
            offset = val.get(fields[i][0]) - min;
            //Set roundUp if this is more than half way between the minimum and maximum
            roundUp = offset > ((max - min) / 2);
        }
        //We need to remove this field
        if (offset != 0) {
            val.set(fields[i][0], val.get(fields[i][0]) - offset);
        }
    }
    throw new IllegalArgumentException("The field " + field + " is not supported");

}

From source file:edu.cmu.tetrad.util.StatUtils.java

/**
 * @param array a long array./*from   w  w w.jav  a 2 s  .co  m*/
 * @param N     the number of values of array which should be considered.
 * @return the curtosis of the first N values in array.
 */
public static double kurtosis(long array[], int N) {
    double mean = StatUtils.mean(array, N);
    double variance = StatUtils.variance(array, N);
    double kurt = 0.0;

    for (int j = 0; j < N; j++) {
        double s = array[j] - mean;
        kurt += s * s * s * s;
    }

    if (variance == 0) {
        throw new ArithmeticException("Kurtosis is undefined when variance is zero.");
    }

    kurt = kurt / N;

    kurt = kurt / (variance * variance) - 3.0;

    return kurt;
}

From source file:edu.cmu.tetrad.util.StatUtils.java

public static double standardizedFifthMoment(double array[], int N) {
    double mean = StatUtils.mean(array, N);
    double variance = StatUtils.variance(array, N);
    double kurt = 0.0;

    for (int j = 0; j < N; j++) {
        double s = array[j] - mean;
        kurt += s * s * s * s * s;/* w  w w.j  av a 2s . co m*/
    }

    if (variance == 0) {
        throw new ArithmeticException("Kurtosis is undefined when variance is zero.");
    }

    kurt = (kurt / (N * Math.pow(variance, 5 / 2.)));

    return kurt;
}

From source file:edu.cmu.tetrad.util.StatUtils.java

public static double standardizedSixthMoment(double array[], int N) {
    double mean = StatUtils.mean(array, N);
    double variance = StatUtils.variance(array, N);
    double kurt = 0.0;

    for (int j = 0; j < N; j++) {
        double s = array[j] - mean;
        kurt += s * s * s * s * s * s;//from w  ww  . j  av  a 2  s.c o  m
    }

    if (variance == 0) {
        throw new ArithmeticException("Kurtosis is undefined when variance is zero.");
    }

    kurt = (kurt / (N * Math.pow(variance, 6 / 2.)));

    return kurt;
}

From source file:net.pms.util.Rational.java

/**
 * @return The numerator.//from  w  w w.  j a  v  a 2 s  .com
 * @throws ArithmeticException If this is {@code NaN} or infinite.
 */
public BigInteger getNumerator() {
    if (isNaN() || isInfinite()) {
        throw new ArithmeticException("Numerator is undefined");
    }
    return numerator;
}

From source file:net.pms.util.Rational.java

/**
 * @return The denominator.//from ww w  . j a  v  a2 s.co m
 * @throws ArithmeticException If this is {@code NaN} or infinite.
 */
public BigInteger getDenominator() {
    if (isNaN() || isInfinite()) {
        throw new ArithmeticException("Denominator is undefined");
    }
    return denominator;
}

From source file:net.pms.util.Rational.java

/**
 * The reduced numerator is the numerator divided by
 * {@link #getGreatestCommonDivisor}.//from   w  w  w.ja v  a  2s. com
 *
 * @return The reduced numerator.
 * @throws ArithmeticException If this is {@code NaN} or infinite.
 */
public BigInteger getReducedNumerator() {
    if (isNaN() || isInfinite()) {
        throw new ArithmeticException("Numerator is undefined");
    }
    return reducedNumerator;
}