List of usage examples for java.lang ArithmeticException ArithmeticException
public ArithmeticException(String s)
From source file:Ternary.java
/** * Divide this divided by the specified divisor * * @param divisor to divide by//from w w w. ja v a2 s . com * @return ternary[] containing {quotient,remainder} */ public Ternary[] divide(Ternary divisor) { /** * 6/3=2r0 * 6=dividend * 3=divisor * 2=quotient * 0=remainder */ Ternary dividend = new Ternary(this); Ternary quotient = new Ternary(0); Ternary remainder = new Ternary(0); int dividendSign = dividend.signum(); if (dividendSign == 0) return new Ternary[] { quotient, remainder }; int divisorSign = divisor.signum(); if (divisorSign == 0) throw new ArithmeticException("Divide by Zero not currently supported."); if (dividendSign != divisorSign) { // if one or the other (not both) are negative, then the result will have a negative quotient Ternary tmp = null; Ternary[] results = null; if (dividendSign < 0) { tmp = new Ternary(dividend); tmp = invert(tmp); //results = tmp.divide(divisor); results = tmp.divide(divisor); } else { tmp = new Ternary(divisor); tmp = invert(tmp); //results = dividend.divide(tmp); results = dividend.divide(tmp); } quotient = invert(results[0]); remainder = dividend.subtract(quotient.multiply(divisor)); return new Ternary[] { quotient, remainder }; } // two positives or two negatives will be positive results if (dividendSign < 0) { dividend = invert(dividend); divisor = invert(divisor); } int position = dividend.tritLength() - 1; while (position >= 0) { remainder = (new Ternary(dividend)).subtract(quotient.multiply(divisor)); remainder.shiftRight(position); int compare = remainder.compareTo(divisor); if (compare > 0) { quotient.increment(); } else if (compare < 0) { if (position > 0) quotient.shiftLeft(1); position--; } else { quotient.increment(); position--; } } remainder = (new Ternary(dividend)).subtract(quotient.multiply(divisor)); return new Ternary[] { quotient, remainder }; }
From source file:org.nd4j.linalg.util.BigDecimalMath.java
/** * The square root./*ww w . ja v a 2s . co m*/ * * @param x the non-negative argument. * @return the square root of the BigDecimal rounded to the precision implied by x. */ static public BigDecimal sqrt(final BigDecimal x) { if (x.compareTo(BigDecimal.ZERO) < 0) { throw new ArithmeticException("negative argument " + x.toString() + " of square root"); } return root(2, x); }
From source file:org.nd4j.linalg.util.BigDecimalMath.java
/** * The integer root./*from ww w . jav a2s. c o m*/ * * @param n the positive argument. * @param x the non-negative argument. * @return The n-th root of the BigDecimal rounded to the precision implied by x, x^(1/n). */ static public BigDecimal root(final int n, final BigDecimal x) { if (x.compareTo(BigDecimal.ZERO) < 0) { throw new ArithmeticException("negative argument " + x.toString() + " of root"); } if (n <= 0) { throw new ArithmeticException("negative power " + n + " of root"); } if (n == 1) { return x; } /* start the computation from a double precision estimate */ BigDecimal s = new BigDecimal(Math.pow(x.doubleValue(), 1.0 / n)); /* this creates nth with nominal precision of 1 digit */ final BigDecimal nth = new BigDecimal(n); /* Specify an internal accuracy within the loop which is * slightly larger than what is demanded by eps below. */ final BigDecimal xhighpr = scalePrec(x, 2); MathContext mc = new MathContext(2 + x.precision()); /* Relative accuracy of the result is eps. */ final double eps = x.ulp().doubleValue() / (2 * n * x.doubleValue()); for (;;) { /* s = s -(s/n-x/n/s^(n-1)) = s-(s-x/s^(n-1))/n; test correction s/n-x/s for being * smaller than the precision requested. The relative correction is (1-x/s^n)/n, */ BigDecimal c = xhighpr.divide(s.pow(n - 1), mc); c = s.subtract(c); MathContext locmc = new MathContext(c.precision()); c = c.divide(nth, locmc); s = s.subtract(c); if (Math.abs(c.doubleValue() / s.doubleValue()) < eps) { break; } } return s.round(new MathContext(err2prec(eps))); }
From source file:com.kyleszombathy.sms_scheduler.Home.java
/**Sets up recycler view and adapter*/ private void setUpRecyclerView() { // Empty state mRecyclerEmptyState = (RelativeLayout) findViewById(R.id.Home_recycler_empty_state); // Setting up RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.Home_recycler_view); mRecyclerView.setHasFixedSize(true); mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerView.setItemAnimator(new SlideInDownAnimator()); initializeRecyclerAdapter();// w ww . ja v a 2s.c om // Item touch listener for editing message itemTouchHelper.attachToRecyclerView(mRecyclerView); mRecyclerView.addOnItemTouchListener( new RecyclerItemClickListener(Home.this, new RecyclerItemClickListener.OnItemClickListener() { @Override public void onItemClick(View view, int clickedPosition) { // Tie to global values for use after AddMessage return int oldAlarmNumber = messages.get(clickedPosition).getAlarmNumber(); if (oldAlarmNumber == -1) throw new ArithmeticException("oldAlarmNumber cannot be -1 if passing to AddMessage"); int newAlarmNumber = getRandomInt(MIN_INT, MAX_INT); // Bundle extras Bundle extras = new Bundle(); extras.putInt(ALARM_EXTRA, newAlarmNumber); extras.putInt("OLD_ALARM", oldAlarmNumber); extras.putBoolean(EDIT_MESSAGE_EXTRA, true); // Create new intent to AddMessage with data from item in position Intent intent = new Intent(new Intent(Home.this, AddMessage.class)); intent.putExtras(extras); // Go to AddMessage startActivityForResult(intent, EDIT_MESSAGE, ActivityOptions.makeSceneTransitionAnimation(Home.this).toBundle()); } })); Log.i(TAG, "setUpRecyclerView: Successfully set up recycler view"); }
From source file:ubic.basecode.math.DescriptiveWithMissing.java
/** * Calculate the mean of the values above to a particular quantile of an array. * //from w w w. ja va2s . c o m * @param quantile A value from 0 to 1 * @param array Array for which we want to get the quantile. * @return double */ public static double meanAboveQuantile(double quantile, DoubleArrayList array) { if (quantile < 0.0 || quantile > 1.0) { throw new IllegalArgumentException("Quantile must be between 0 and 1"); } double returnvalue = 0.0; int k = 0; double quantileValue = DescriptiveWithMissing.quantile(array, quantile); for (int i = 0; i < array.size(); i++) { if (array.get(i) > quantileValue) { returnvalue += array.get(i); k++; } } if (k == 0) { throw new ArithmeticException("No values found above quantile"); } return returnvalue / k; }
From source file:edu.umd.cfar.lamp.viper.geometry.ConvexPolygon.java
/** * This creates a new area that is the intersection of both. * //from w w w . j av a 2s . c o m * @param P a polygon to intersect * @param Q a polygon to intersect * @return the polygon representing the region in both polygons */ static public ConvexPolygon intersection(ConvexPolygon P, ConvexPolygon Q) { if (!P.getBoundingBox().intersects(Q.getBoundingBox())) { return new ConvexPolygon(); } ConvexPolygon solutionPoly = new ConvexPolygon(); int n = P.getNumberOfVerteces(); int m = Q.getNumberOfVerteces(); int a = 0; int b = 0; int a1, b1; Component A, B; Rational crossProduct; Rational bHa, aHb; Pnt Origin = new Pnt(0, 0); Pnt p = new Pnt(); Pnt q = new Pnt(); int inflag = UNKNOWN; // -1 = outside, 0 = UNKNOWN, +1 - inside int aa = 0; int ba = 0; boolean firstPoint = true; char code; // Advance around the edge of both polygons, so that the // they chase each other. Save the relevant verteces to the edge // list. do { /* Calculate key variables */ a1 = a - 1; b1 = b - 1; A = P.getVertex(a1).minus(P.getVertex(a)); B = Q.getVertex(b1).minus(Q.getVertex(b)); crossProduct = Util.areaSign(Origin, A, B); aHb = Util.areaSign(Q.getVertex(b1), Q.getVertex(b), P.getVertex(a)); bHa = Util.areaSign(P.getVertex(a1), P.getVertex(a), Q.getVertex(b)); /* If A & B intersect, update inflag. */ code = Util.lineIntersection(P.getVertex(a1), P.getVertex(a), Q.getVertex(b1), Q.getVertex(b), p); if (code == '1' || code == 'v') { if (inflag == UNKNOWN && firstPoint) { aa = ba = 0; firstPoint = false; } try { solutionPoly.addVertex(p); } catch (BadDataException bdx) { StringBuffer errMsg = new StringBuffer(); errMsg.append("While intersecting ").append(P.toString()).append(" with ").append(Q.toString()) .append(" adding ").append(P.toString()); errMsg.append("\n\t").append(bdx.getMessage()); errMsg.append("\n\t").append(" to get " + solutionPoly); throw new ArithmeticException(errMsg.toString()); } inflag = inOut(inflag, aHb, bHa); } /* Advance! */ /* A & B colinear in opposite directions */ if ((code == 'e') && (Component.dot(A, B).lessThan(0))) { try { solutionPoly.clearPolygon(); solutionPoly.addVertex(p); solutionPoly.addVertex(q); } catch (BadDataException bdx) { String s = "\nWhile intersecting " + P + " with " + Q + " to get " + solutionPoly; throw new ArithmeticException(bdx.getMessage() + s); } solutionPoly.initBbox(); return solutionPoly; } /* Special case: A & B parallel and separated. */ else if (crossProduct.equals(0) && aHb.lessThan(0) && bHa.lessThan(0)) { return solutionPoly; } /* Special case: A & B collinear. */ else if (crossProduct.equals(0) && aHb.equals(0) && bHa.equals(0)) { /* Advance but do not output point. */ if (inflag == P_IN) { ba++; b++; } else { aa++; a++; } } /* Generic cases. */ else if ((!crossProduct.lessThan(0) && bHa.greaterThan(0)) || (crossProduct.lessThan(0) && !aHb.greaterThan(0))) { if (inflag == P_IN) { try { solutionPoly.addVertex(P.getVertex(a)); } catch (BadDataException bdx) { String s = "\nWhile intersecting " + P + " with " + Q + " to get " + solutionPoly; throw new ArithmeticException(bdx.getMessage() + s); } } aa++; a++; } else if ((!crossProduct.lessThan(0) && !bHa.greaterThan(0)) || (crossProduct.lessThan(0) && aHb.greaterThan(0))) { if (inflag == Q_IN) { try { solutionPoly.addVertex(Q.getVertex(b)); } catch (BadDataException bdx) { String s = "\nWhile intersecting " + P + " with " + Q + " to get " + solutionPoly; throw new ArithmeticException(bdx.getMessage() + s); } } ba++; b++; } /* * Quit when both adv. indices have cycled, or one has cycled * twice. */ } while (((aa < n) || (ba < m)) && (aa < 2 * n) && (ba < 2 * m)); // If the boundaries don't cross, see if one is inside the other if (inflag == 0) { if (P.contains(Q.getVertex(0))) { return new ConvexPolygon(Q); } else if (Q.contains(P.getVertex(0))) { return new ConvexPolygon(P); } else { // Boundaries don't cross, one is not inside the other return new ConvexPolygon(); } } solutionPoly.initBbox(); return solutionPoly; }
From source file:marytts.unitselection.analysis.Phone.java
/** * Get the factors required to convert the F0 values recovered from the Datagrams in a SelectedUnit to the target F0 values. * // www . jav a 2 s . c o m * @param unit * from which to recover the realized F0 values * @param target * for which to get the target F0 values * @return each Datagram's F0 factor in an array, or null if realized and target F0 values differ in length * @throws ArithmeticException * if any of the F0 values recovered from the unit's Datagrams is zero */ private double[] getUnitF0Factors(SelectedUnit unit, HalfPhoneTarget target) throws ArithmeticException { double[] unitF0Values = getUnitF0Values(unit); if (ArrayUtils.contains(unitF0Values, 0)) { throw new ArithmeticException("Unit frames must not have F0 of 0!"); } double[] targetF0Values; if (target == null || target.isLeftHalf()) { targetF0Values = getLeftTargetF0Values(); } else { targetF0Values = getRightTargetF0Values(); } double[] f0Factors = null; try { f0Factors = MathUtils.divide(targetF0Values, unitF0Values); } catch (IllegalArgumentException e) { // leave at null } return f0Factors; }
From source file:edu.umd.cfar.lamp.viper.geometry.BoundingBox.java
/** * Unions this with the specified box(es) and sets this to that union. * /*w ww . ja v a2 s . c om*/ * @param other * The box(es) to add to this set */ public void extendToContain(BoundingBox other) { if (composed || other.composed) throw new ArithmeticException("Cannot extend composed bboxes"); rect.x = Math.min(rect.x, other.rect.x); rect.width = Math.max(rect.x + rect.width, other.rect.x + other.rect.width) - rect.x; rect.y = Math.min(rect.y, other.rect.y); rect.height = Math.max(rect.y + rect.height, other.rect.y + other.rect.height) - rect.y; bbox = null; }
From source file:edu.umd.cfar.lamp.viper.geometry.BoundingBox.java
/** * Gets the x-coordinate of the box's origin. * //from www .j a v a 2 s . c om * @return the x-coordinate of the box's origin. * @throws ArithmeticException * if the set of boxes is not a singleton */ public int getX() { if (composed) throw new ArithmeticException("Cannot get got composed bboxes"); return rect.x; }
From source file:edu.umd.cfar.lamp.viper.geometry.BoundingBox.java
/** * Gets the y-coordinate of the box's origin. * //from ww w .j a v a 2 s .c o m * @return the y-coordinate of the box's origin. * @throws ArithmeticException * if the set of boxes is not a singleton */ public int getY() { if (composed) throw new ArithmeticException("Cannot get got composed bboxes"); return rect.y; }