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.knime.al.util.MathUtils.java

/**
 * Calculates the shanon entropy of the distribution of values in an double
 * array./* ww w  .  ja v a2  s.co  m*/
 *
 * @param distribution
 *            a double array
 * @return the entropy of the array
 * @throws ArithmeticException
 *             in case the sum of the elements of the distribution array is
 *             not 1.
 */
public static final double entropy(final double[] distribution) {

    if (!checkDistribution(distribution)) {
        throw new ArithmeticException("The sum of the distribution elements is not 1");
    }

    // calculate the entropy
    double entropy = 0;
    for (final double d : distribution) {
        entropy += d * log2(d);
    }

    entropy /= log2(distribution.length);
    if (Double.isNaN(entropy)) {
        entropy = 0;
    }
    return -entropy;
}

From source file:org.kalypso.model.wspm.tuhh.core.profile.TUHHInterpolationHandler.java

@Override
public boolean doInterpolation(final TupleResult result, final IRecord record, final int index,
        final double distance) {
    if (result == null || record == null || index < 0 || index > result.size() - 2)
        return false;

    try {//  w  ww . ja  v  a 2s.  c o m
        final IRecord previous = result.get(index);
        final IRecord next = result.get(index + 1);
        for (final String id : INTERPOLATION_IDS) {
            final int i = result.indexOfComponent(id);
            if (i < 0) {
                continue;
            }

            final Object v1 = previous.getValue(i);
            final Object v2 = next.getValue(i);
            final Object interp = interpolateValues(v1, v2, distance);
            record.setValue(i, interp);
        }

        for (final String id : CONSTANT_IDS) {
            final int i = result.indexOfComponent(id);
            if (i < 0) {
                continue;
            }
            record.setValue(i, previous.getValue(i));
        }
        return true;
    } catch (final Exception e) {
        e.printStackTrace();
        throw new ArithmeticException(e.getLocalizedMessage());
    }
}

From source file:us.ihmc.idl.CDR.java

public void write_type_4(long val) {
    if (val < 0)
        throw new ArithmeticException(
                "(CDR.java:134): long " + val + " cannot be cast to unsigned int. cannot be negative");
    else if (val > UNSIGNED_INT_MAX)
        throw new ArithmeticException("(CDR.java:134): long " + val
                + " cannot be cast to unsigned int. UNSIGNED_INT_MAX = " + UNSIGNED_INT_MAX);

    write_type_2((int) val);
}

From source file:pt.webdetails.cda.CdaQueryComponent.java

public boolean execute() throws Exception {

    final QueryOptions queryOptions = new QueryOptions();

    final CdaSettings cdaSettings = CdaEngine.getInstance().getSettingsManager().parseSettingsFile(file);

    final String CDA_PARAMS = "cdaParameterString";
    final String CDA_PARAM_SEPARATOR = ";";

    // page info/*  w ww.  j  a va 2  s .  c  o m*/

    final long pageSize = inputsGetLong("pageSize", 0);
    final long pageStart = inputsGetLong("pageStart", 0);
    final boolean paginate = "true".equals(inputsGetString("paginateQuery", "false"));
    if (pageSize > 0 || pageStart > 0 || paginate) {
        if (pageSize > Integer.MAX_VALUE || pageStart > Integer.MAX_VALUE) {
            throw new ArithmeticException("Paging values too large");
        }
        queryOptions.setPaginate(true);
        queryOptions.setPageSize(pageSize > 0 ? (int) pageSize : paginate ? DEFAULT_PAGE_SIZE : 0);
        queryOptions.setPageStart(pageStart > 0 ? (int) pageStart : paginate ? DEFAULT_START_PAGE : 0);
    }

    // query info 

    queryOptions.setOutputType(inputsGetString("outputType", "resultset"));
    queryOptions.setDataAccessId(inputsGetString("dataAccessId", "<blank>"));
    queryOptions.setOutputIndexId(inputsGetInteger("outputIndexId", 1));

    // params and settings

    //process parameter string "name1=value1;name2=value2"
    String cdaParamString = inputsGetString(CDA_PARAMS, null);
    if (cdaParamString != null && cdaParamString.trim().length() > 0) {

        List<String> cdaParams = new ArrayList<String>();
        //split to 'name=val' tokens
        CSVTokenizer tokenizer = new CSVTokenizer(cdaParamString, CDA_PARAM_SEPARATOR);
        while (tokenizer.hasMoreTokens()) {
            cdaParams.add(tokenizer.nextToken());
        }

        //split '='
        for (String nameValue : cdaParams) {
            int i = 0;
            CSVTokenizer nameValSeparator = new CSVTokenizer(nameValue, "=");
            String name = null, value = null;
            while (nameValSeparator.hasMoreTokens()) {
                if (i++ == 0) {
                    name = nameValSeparator.nextToken();
                } else {
                    value = nameValSeparator.nextToken();
                    break;
                }
            }
            if (name != null)
                queryOptions.addParameter(name, value);
        }
    }

    for (String param : inputs.keySet()) {
        if (param.startsWith("param")) {
            queryOptions.addParameter(param.substring(5), inputsGetString(param, ""));
        } else if (param.startsWith("setting")) {
            queryOptions.addSetting(param.substring(7), inputsGetString(param, ""));
        }
    }

    // TODO: Support binary outputs if outputtype not equals resultset
    if (queryOptions.getOutputType().equals("resultset")) {
        TableModel tableModel = cdaSettings.getDataAccess(queryOptions.getDataAccessId()).doQuery(queryOptions);
        resultSet = convertTableToResultSet(tableModel);
    }

    return true;
}

From source file:org.knime.al.util.MathUtils.java

/**
 * Calculates the entropy of the distribution of values in an double array.
 *
 * @param distribution/*  w ww  .ja v  a 2s . c om*/
 *            a double array containing an distribution
 *
 *
 * @return the variance of the array
 * @throws ArithmeticException
 *             if case the sum of the elements of the distribution array is
 *             not 1.
 */
public static double variance(final double[] distribution) {

    // check if input array was a proper distribution.
    if (!checkDistribution(distribution)) {
        throw new ArithmeticException("The sum of the distribution elements is not 1");
    }
    return VAR.evaluate(distribution);
}

From source file:com.github.jessemull.microflex.util.DoubleUtil.java

/**
 * Safely converts an object to a double. Loss of precision may occur. Throws
 * an arithmetic exception upon overflow.
 * @param    Object    object to parse//from w  ww .  j  a  v a  2 s .c o  m
 * @return             parsed object
 * @throws   ArithmeticException    on overflow
 */
public static double toDouble(Object obj) {

    /* Switch on class and convert to double */

    String type = obj.getClass().getSimpleName();
    double parsed;

    switch (type) {

    case "Byte":
        Byte by = (Byte) obj;
        parsed = by.doubleValue();
        break;

    case "Short":
        Short sh = (Short) obj;
        parsed = sh.doubleValue();
        break;

    case "Integer":
        Integer in = (Integer) obj;
        parsed = in.doubleValue();
        break;

    case "Long":
        Long lo = (Long) obj;
        parsed = lo.doubleValue();
        break;

    case "Float":
        Float fl = (Float) obj;
        parsed = fl.doubleValue();
        break;

    case "BigInteger":
        BigInteger bi = (BigInteger) obj;
        if (!OverFlowUtil.doubleOverflow(bi)) {
            throw new ArithmeticException("Overflow casting " + obj + " to a double.");
        }
        parsed = bi.doubleValue();
        break;

    case "BigDecimal":
        BigDecimal bd = (BigDecimal) obj;
        if (!OverFlowUtil.doubleOverflow(bd)) {
            throw new ArithmeticException("Overflow casting " + obj + " to a double.");
        }
        parsed = bd.doubleValue();
        break;

    case "Double":
        Double db = (Double) obj;
        parsed = db.doubleValue();
        break;

    default:
        throw new IllegalArgumentException(
                "Invalid type: " + type + "\nData values " + "must extend the abstract Number class.");

    }

    return parsed;
}

From source file:com.github.jessemull.microflex.util.IntegerUtil.java

/**
 * Safely converts a number to an integer. Loss of precision may occur. Throws
 * an arithmetic exception upon overflow.
 * @param    Object    number to parse//from   w  ww .  j ava 2s  .c  om
 * @return             parsed number
 * @throws   ArithmeticException    on overflow
 */
public static int toInteger(Object obj) {

    /* Switch on class and convert to an int */

    String type = obj.getClass().getSimpleName();
    int parsed;

    switch (type) {

    case "Byte":
        Byte by = (Byte) obj;
        parsed = by.intValue();
        break;

    case "Short":
        Short sh = (Short) obj;
        parsed = sh.intValue();
        break;

    case "Integer":
        Integer in = (Integer) obj;
        parsed = in.intValue();
        break;

    case "Long":
        Long lo = (Long) obj;
        if (!OverFlowUtil.intOverflow(lo)) {
            throw new ArithmeticException("Overflow casting " + obj + " to an int.");
        }
        parsed = lo.intValue();
        break;

    case "Float":
        Float fl = (Float) obj;
        if (!OverFlowUtil.intOverflow(fl)) {
            throw new ArithmeticException("Overflow casting " + obj + " to an int.");
        }
        parsed = fl.intValue();
        break;

    case "BigInteger":
        BigInteger bi = (BigInteger) obj;
        if (!OverFlowUtil.intOverflow(bi)) {
            throw new ArithmeticException("Overflow casting " + obj + " to an int.");
        }
        parsed = bi.intValue();
        break;

    case "BigDecimal":
        BigDecimal bd = (BigDecimal) obj;
        if (!OverFlowUtil.intOverflow(bd)) {
            throw new ArithmeticException("Overflow casting " + obj + " to an int.");
        }
        parsed = bd.intValue();
        break;

    case "Double":
        Double db = (Double) obj;
        if (!OverFlowUtil.intOverflow(db)) {
            throw new ArithmeticException("Overflow casting " + obj + " to an int.");
        }
        parsed = db.intValue();
        break;

    default:
        throw new IllegalArgumentException(
                "Invalid type: " + type + "\nData values " + "must extend the abstract Number class.");

    }

    return parsed;
}

From source file:org.apache.hadoop.hive.ql.io.sarg.ConvertAstToSearchArg.java

private static Object boxLiteral(ExprNodeConstantDesc constantDesc, PredicateLeaf.Type type) {
    Object lit = constantDesc.getValue();
    if (lit == null) {
        return null;
    }//from w ww.  j  a  v a  2  s  . c o m
    switch (type) {
    case LONG:
        if (lit instanceof HiveDecimal) {
            HiveDecimal dec = (HiveDecimal) lit;
            if (!dec.isLong()) {
                throw new ArithmeticException("Overflow");
            }
            return dec.longValue();
        }
        return ((Number) lit).longValue();
    case STRING:
        if (lit instanceof HiveChar) {
            return ((HiveChar) lit).getPaddedValue();
        } else if (lit instanceof String) {
            return lit;
        } else {
            return lit.toString();
        }
    case FLOAT:
        if (lit instanceof HiveDecimal) {
            // HiveDecimal -> Float -> Number -> Double
            return ((Number) ((HiveDecimal) lit).floatValue()).doubleValue();
        } else {
            return ((Number) lit).doubleValue();
        }
    case TIMESTAMP:
        return Timestamp.valueOf(lit.toString());
    case DATE:
        return Date.valueOf(lit.toString());
    case DECIMAL:
        return new HiveDecimalWritable(lit.toString());
    case BOOLEAN:
        return lit;
    default:
        throw new IllegalArgumentException("Unknown literal " + type);
    }
}

From source file:org.neo4j.kernel.impl.util.CappedLoggerTest.java

@Test
public void mustLogExceptions() throws Exception {
    logMethod.log(logger, "MESSAGE", new ArithmeticException("EXCEPTION"));
    logProvider.assertContainsLogCallContaining("MESSAGE");
    logProvider.assertContainsLogCallContaining("ArithmeticException");
    logProvider.assertContainsLogCallContaining("EXCEPTION");
}

From source file:ubic.basecode.math.DescriptiveWithMissing.java

/**
 * Calculate the pearson correlation of two arrays. Missing values (NaNs) are ignored.
 * /*from   w  w  w . j a v  a  2s  . co m*/
 * @param x DoubleArrayList
 * @param y DoubleArrayList
 * @return double
 */
public static double correlation(DoubleArrayList x, DoubleArrayList y) {
    int j;
    double syy, sxy, sxx, sx, sy, xj, yj, ay, ax;
    int numused;
    syy = 0.0;
    sxy = 0.0;
    sxx = 0.0;
    sx = 0.0;
    sy = 0.0;
    numused = 0;
    if (x.size() != y.size()) {
        throw new ArithmeticException("Unequal vector sizes: " + x.size() + " != " + y.size());
    }

    double[] xel = x.elements();
    double[] yel = y.elements();

    int length = x.size();
    for (j = 0; j < length; j++) {
        xj = xel[j];
        yj = yel[j];

        if (!Double.isNaN(xj) && !Double.isNaN(yj)) {
            sx += xj;
            sy += yj;
            sxy += xj * yj;
            sxx += xj * xj;
            syy += yj * yj;
            numused++;
        }
    }

    if (numused > 0) {
        ay = sy / numused;
        ax = sx / numused;
        return (sxy - sx * ay) / Math.sqrt((sxx - sx * ax) * (syy - sy * ay));
    }
    return Double.NaN; // signifies that it could not be calculated.

}