List of usage examples for java.lang ArithmeticException ArithmeticException
public ArithmeticException(String s)
From source file:edu.cmu.tetrad.util.StatUtils.java
/** * Calculates the Poisson Distribution for mean x and k events for doubles. * If third parameter is boolean true, the cumulative Poisson function is * returned./*from w w w . j a v a2 s .c o m*/ * * @param k # events * @param x mean * @param cum true if the cumulative Poisson is desired. * @return the value of the Poisson (or cumPoisson) at x. */ public static double poisson(double k, double x, boolean cum) { if ((x < 0) || (k < 1)) { throw new ArithmeticException("The Poisson Distribution Function requires x>=0 and k >= 1"); } k = k + 1; // algorithm uses k+1, not k if (cum) { return (1.0 - igamma(k, x)); } else { return ((Math.exp(-x) * Math.pow(x, k)) / gamma(k)); } }
From source file:net.pms.util.Rational.java
/** * The reduced denominator is the denominator divided by * {@link #getGreatestCommonDivisor}./*from www.j a va2 s . c om*/ * * @return The reduced denominator. * @throws ArithmeticException If this is {@code NaN} or infinite. */ public BigInteger getReducedDenominator() { if (isNaN() || isInfinite()) { throw new ArithmeticException("Denominator is undefined"); } return reducedDenominator; }
From source file:net.pms.util.Rational.java
/** * @return The greatest common divisor of the numerator and the denominator. * @throws ArithmeticException If this is {@code NaN} or infinite. *///from ww w . j ava2s . co m public BigInteger getGreatestCommonDivisor() { if (isNaN() || isInfinite()) { throw new ArithmeticException("Greatest common divisor is undefined"); } return greatestCommonDivisor; }
From source file:edu.cmu.tetrad.util.StatUtils.java
/** * Calculates the one-tail probability of the Chi-squared distribution for * doubles//w w w.ja v a 2s . c o m * * @return value of Chi at x with the stated degrees of freedom. */ public static double chidist(double x, int degreesOfFreedom) { if ((x < 0.0) || (degreesOfFreedom < 0)) { throw new ArithmeticException( "The Chi Distribution Function requires x > 0.0 and degrees of freedom > 0"); } return (1.0 - igamma((double) degreesOfFreedom / 2.0, x / 2.0)); }
From source file:com.xunlei.util.DateUtils.java
/** * <p>/*from ww w.jav a2 s . c o m*/ * Internal calculation method. * </p> * * @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 */ 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:org.nd4j.linalg.util.BigDecimalMath.java
/** * The trigonometric co-tangent.//from ww w. j a v a2 s . c o m * * @param x the argument in radians. * @return the cot(x) */ static public BigDecimal cot(final BigDecimal x) { if (x.compareTo(BigDecimal.ZERO) == 0) { throw new ArithmeticException("Cannot take cot of zero " + x.toString()); } else if (x.compareTo(BigDecimal.ZERO) < 0) { return cot(x.negate()).negate(); } else { /* reduce modulo pi */ BigDecimal res = modpi(x); /* absolute error in the result is err(x)/sin^2(x) to lowest order */ final double xDbl = res.doubleValue(); final double xUlpDbl = x.ulp().doubleValue() / 2.; final double eps = xUlpDbl / 2. / Math.pow(Math.sin(xDbl), 2.); final BigDecimal xhighpr = scalePrec(res, 2); final BigDecimal xhighprSq = multiplyRound(xhighpr, xhighpr); MathContext mc = new MathContext(err2prec(xhighpr.doubleValue(), eps)); BigDecimal resul = BigDecimal.ONE.divide(xhighpr, mc); /* x^(2i-1) */ BigDecimal xpowi = xhighpr; Bernoulli b = new Bernoulli(); /* 2^(2i) */ BigInteger fourn = new BigInteger("4"); /* (2i)! */ BigInteger fac = BigInteger.ONE; for (int i = 1;; i++) { Rational f = b.at(2 * i); fac = fac.multiply(new BigInteger("" + (2 * i))).multiply(new BigInteger("" + (2 * i - 1))); f = f.multiply(fourn).divide(fac); BigDecimal c = multiplyRound(xpowi, f); if (i % 2 == 0) { resul = resul.add(c); } else { resul = resul.subtract(c); } if (Math.abs(c.doubleValue()) < 0.1 * eps) { break; } fourn = fourn.shiftLeft(2); xpowi = multiplyRound(xpowi, xhighprSq); } mc = new MathContext(err2prec(resul.doubleValue(), eps)); return resul.round(mc); } }
From source file:org.nd4j.linalg.util.BigDecimalMath.java
/** * The inverse trigonometric sine.//from ww w . j av a 2 s. c o m * * @param x the argument. * @return the arcsin(x) in radians. */ static public BigDecimal asin(final BigDecimal x) { if (x.compareTo(BigDecimal.ONE) > 0 || x.compareTo(BigDecimal.ONE.negate()) < 0) { throw new ArithmeticException("Out of range argument " + x.toString() + " of asin"); } else if (x.compareTo(BigDecimal.ZERO) == 0) { return BigDecimal.ZERO; } else if (x.compareTo(BigDecimal.ONE) == 0) { /* arcsin(1) = pi/2 */ double errpi = Math.sqrt(x.ulp().doubleValue()); MathContext mc = new MathContext(err2prec(3.14159, errpi)); return pi(mc).divide(new BigDecimal(2)); } else if (x.compareTo(BigDecimal.ZERO) < 0) { return asin(x.negate()).negate(); } else if (x.doubleValue() > 0.7) { final BigDecimal xCompl = BigDecimal.ONE.subtract(x); final double xDbl = x.doubleValue(); final double xUlpDbl = x.ulp().doubleValue() / 2.; final double eps = xUlpDbl / 2. / Math.sqrt(1. - Math.pow(xDbl, 2.)); final BigDecimal xhighpr = scalePrec(xCompl, 3); final BigDecimal xhighprV = divideRound(xhighpr, 4); BigDecimal resul = BigDecimal.ONE; /* x^(2i+1) */ BigDecimal xpowi = BigDecimal.ONE; /* i factorial */ BigInteger ifacN = BigInteger.ONE; BigInteger ifacD = BigInteger.ONE; for (int i = 1;; i++) { ifacN = ifacN.multiply(new BigInteger("" + (2 * i - 1))); ifacD = ifacD.multiply(new BigInteger("" + i)); if (i == 1) { xpowi = xhighprV; } else { xpowi = multiplyRound(xpowi, xhighprV); } BigDecimal c = divideRound(multiplyRound(xpowi, ifacN), ifacD.multiply(new BigInteger("" + (2 * i + 1)))); resul = resul.add(c); /* series started 1+x/12+... which yields an estimate of the sums error */ if (Math.abs(c.doubleValue()) < xUlpDbl / 120.) { break; } } /* sqrt(2*z)*(1+...) */ xpowi = sqrt(xhighpr.multiply(new BigDecimal(2))); resul = multiplyRound(xpowi, resul); MathContext mc = new MathContext(resul.precision()); BigDecimal pihalf = pi(mc).divide(new BigDecimal(2)); mc = new MathContext(err2prec(resul.doubleValue(), eps)); return pihalf.subtract(resul, mc); } else { /* absolute error in the result is err(x)/sqrt(1-x^2) to lowest order */ final double xDbl = x.doubleValue(); final double xUlpDbl = x.ulp().doubleValue() / 2.; final double eps = xUlpDbl / 2. / Math.sqrt(1. - Math.pow(xDbl, 2.)); final BigDecimal xhighpr = scalePrec(x, 2); final BigDecimal xhighprSq = multiplyRound(xhighpr, xhighpr); BigDecimal resul = xhighpr.plus(); /* x^(2i+1) */ BigDecimal xpowi = xhighpr; /* i factorial */ BigInteger ifacN = BigInteger.ONE; BigInteger ifacD = BigInteger.ONE; for (int i = 1;; i++) { ifacN = ifacN.multiply(new BigInteger("" + (2 * i - 1))); ifacD = ifacD.multiply(new BigInteger("" + (2 * i))); xpowi = multiplyRound(xpowi, xhighprSq); BigDecimal c = divideRound(multiplyRound(xpowi, ifacN), ifacD.multiply(new BigInteger("" + (2 * i + 1)))); resul = resul.add(c); if (Math.abs(c.doubleValue()) < 0.1 * eps) { break; } } MathContext mc = new MathContext(err2prec(resul.doubleValue(), eps)); return resul.round(mc); } }
From source file:net.pms.util.Rational.java
/** * Converts this {@link Rational} to a {@link BigInteger}. This conversion * is analogous to the <i>narrowing primitive conversion</i> from * {@code double} to {@code long} as defined in section 5.1.3 of <cite>The * Java™ Language Specification</cite>: any fractional part of this * {@link Rational} will be discarded./*w ww.j ava2 s. co m*/ * * @return This {@link Rational} converted to a {@link BigInteger}. * @throws ArithmeticException If this is {@code NaN} or infinite. */ @Nonnull public BigInteger bigIntegerValue() { if (isNaN()) { throw new ArithmeticException("Impossible to express NaN as BigInteger"); } if (isInfinite()) { throw new ArithmeticException("Impossible to express infinity as BigInteger"); } return new BigDecimal(reducedNumerator).divide(new BigDecimal(reducedDenominator), RoundingMode.DOWN) .toBigInteger(); }
From source file:Rotation.java
/** Get a normalized vector aligned with the instance. * @return a new normalized vector/* www. j a v a2s . c om*/ * @exception ArithmeticException if the norm is zero */ public Vector3D normalize() { double s = getNorm(); if (s == 0) { throw new ArithmeticException("cannot normalize a zero norm vector"); } return scalarMultiply(1 / s); }
From source file:Rotation.java
/** Get a vector orthogonal to the instance. * <p>There are an infinite number of normalized vectors orthogonal * to the instance. This method picks up one of them almost * arbitrarily. It is useful when one needs to compute a reference * frame with one of the axes in a predefined direction. The * following example shows how to build a frame having the k axis * aligned with the known vector u ://from w w w . j a va 2 s .c o m * <pre><code> * Vector3D k = u.normalize(); * Vector3D i = k.orthogonal(); * Vector3D j = Vector3D.crossProduct(k, i); * </code></pre></p> * @return a new normalized vector orthogonal to the instance * @exception ArithmeticException if the norm of the instance is null */ public Vector3D orthogonal() { double threshold = 0.6 * getNorm(); if (threshold == 0) { throw new ArithmeticException("null norm"); } if ((x >= -threshold) && (x <= threshold)) { double inverse = 1 / Math.sqrt(y * y + z * z); return new Vector3D(0, inverse * z, -inverse * y); } else if ((y >= -threshold) && (y <= threshold)) { double inverse = 1 / Math.sqrt(x * x + z * z); return new Vector3D(-inverse * z, 0, inverse * x); } double inverse = 1 / Math.sqrt(x * x + y * y); return new Vector3D(inverse * y, -inverse * x, 0); }