List of usage examples for java.lang ArithmeticException ArithmeticException
public ArithmeticException(String s)
From source file:org.zaproxy.zap.extension.ascanrules.TestSQLInjection.java
/** * add two numbers with Arithmetic Overflow check * * @param firstNumber/*from w w w . j av a 2 s.com*/ * @param secondNumber * @return */ private static int addWithOverflowCheck(int firstNumber, int secondNumber) { long result = ((long) firstNumber) + ((long) secondNumber); if (result > Integer.MAX_VALUE) { throw new ArithmeticException("Overflow occurred"); } else if (result < Integer.MIN_VALUE) { throw new ArithmeticException("Underflow occurred"); } return (int) result; }
From source file:org.zaproxy.zap.extension.ascanrules.TestSQLInjection.java
/** * multiply two numbers with Arithmetic Overflow check * * @param firstNumber/*from ww w . j ava 2 s . c om*/ * @param secondNumber * @return */ private static int multiplyWithOverflowCheck(int firstNumber, int secondNumber) { long result = ((long) firstNumber) * ((long) secondNumber); if (result > Integer.MAX_VALUE) { throw new ArithmeticException("Overflow occurred"); } else if (result < Integer.MIN_VALUE) { throw new ArithmeticException("Underflow occurred"); } return (int) result; }
From source file:ffx.potential.nonbonded.ParticleMeshEwald.java
private int scfByPCG(boolean print, long startTime) { long directTime = System.nanoTime() - startTime; /**//from w ww . jav a 2s. co m * A request of 0 SCF cycles simplifies mutual polarization to direct * polarization. */ StringBuilder sb = null; if (print) { sb = new StringBuilder("\n Self-Consistent Field\n" + " Iter RMS Change (Debye) Time\n"); } /** * Find the induced dipole field due to direct dipoles (or predicted * induced dipoles from previous steps). */ computeInduceDipoleField(); try { /** * Set initial conjugate gradient residual (a field). * * Store the current induced dipoles and load the residual induced * dipole */ parallelTeam.execute(pcgInitRegion1); /** * Compute preconditioner. */ if (nSymm > 1) { parallelTeam.execute(expandInducedDipolesRegion); } parallelTeam.execute(inducedDipolePreconditionerRegion); /** * Revert to the stored induce dipoles. * * Set initial conjugate vector (induced dipoles). */ parallelTeam.execute(pcgInitRegion2); } catch (Exception e) { String message = "Exception initializing preconditioned CG."; logger.log(Level.SEVERE, message, e); } /** * Conjugate gradient iteration of the mutual induced dipoles. */ int completedSCFCycles = 0; int maxSCFCycles = 1000; double eps = 100.0; double previousEps; boolean done = false; while (!done) { long cycleTime = -System.nanoTime(); /** * Store a copy of the current induced dipoles, then set the induced * dipoles to the conjugate vector. */ for (int i = 0; i < nAtoms; i++) { vec[0][i] = inducedDipole[0][i][0]; vec[1][i] = inducedDipole[0][i][1]; vec[2][i] = inducedDipole[0][i][2]; inducedDipole[0][i][0] = conj[0][i]; inducedDipole[0][i][1] = conj[1][i]; inducedDipole[0][i][2] = conj[2][i]; vecCR[0][i] = inducedDipoleCR[0][i][0]; vecCR[1][i] = inducedDipoleCR[0][i][1]; vecCR[2][i] = inducedDipoleCR[0][i][2]; inducedDipoleCR[0][i][0] = conjCR[0][i]; inducedDipoleCR[0][i][1] = conjCR[1][i]; inducedDipoleCR[0][i][2] = conjCR[2][i]; } /** * Find the induced dipole field. */ computeInduceDipoleField(); try { /** * Revert the induced dipoles to the saved values, then save the * new residual field. * * Compute dot product of the conjugate vector and new residual. * * Reduce the residual field, add to the induced dipoles based * on the scaled conjugate vector and finally set the induced * dipoles to the polarizability times the residual field. */ parallelTeam.execute(pcgIterRegion1); /** * Compute preconditioner. */ if (nSymm > 1) { parallelTeam.execute(expandInducedDipolesRegion); } parallelTeam.execute(inducedDipolePreconditionerRegion); /** * Revert the induced dipoles to the saved values. * * Compute the dot product of the residual and preconditioner. * * Update the conjugate vector and sum the square of the * residual field. */ pcgIterRegion2.sum = pcgIterRegion1.sumShared.get(); pcgIterRegion2.sumCR = pcgIterRegion1.sumCRShared.get(); parallelTeam.execute(pcgIterRegion2); } catch (Exception e) { String message = "Exception in first CG iteration region."; logger.log(Level.SEVERE, message, e); } previousEps = eps; // eps = max(eps, epsCR); eps = max(pcgIterRegion2.epsShared.get(), pcgIterRegion2.epsCRShared.get()); completedSCFCycles++; eps = MultipoleType.DEBYE * sqrt(eps / (double) nAtoms); cycleTime += System.nanoTime(); if (print) { sb.append(format(" %4d %15.10f %7.4f\n", completedSCFCycles, eps, cycleTime * toSeconds)); } /** * If the RMS Debye change increases, fail the SCF process. */ if (eps > previousEps) { if (sb != null) { logger.warning(sb.toString()); } String message = format("Fatal SCF convergence failure: (%10.5f > %10.5f)\n", eps, previousEps); throw new ArithmeticException(message); } /** * The SCF should converge well before the max iteration check. * Otherwise, fail the SCF process. */ if (completedSCFCycles >= maxSCFCycles) { if (sb != null) { logger.warning(sb.toString()); } String message = format("Maximum SCF iterations reached: (%d)\n", completedSCFCycles); throw new ArithmeticException(message); } /** * Check if the convergence criteria has been achieved. */ if (eps < poleps) { done = true; } } if (print) { sb.append(format(" Direct: %7.4f\n", toSeconds * directTime)); startTime = System.nanoTime() - startTime; sb.append(format(" Total: %7.4f", startTime * toSeconds)); logger.info(sb.toString()); } /** * Find the final induced dipole field. */ computeInduceDipoleField(); return completedSCFCycles; }
From source file:ffx.algorithms.RotamerOptimization.java
/** * Prunes rotamer ri of residue i if all ri-j pair energies are worse than * the best i-j pair by some threshold value; additionally prunes ri-rj * pairs if they exceed the best i-j pair by a greater threshold value; * additionally performs this in reverse (searches over j-i). * * @param residues Residues whose rotamers are to be pruned. *///from w ww . j ava 2 s. co m public void prunePairClashes(Residue residues[]) { if (!prunePairClashes) { return; } int nResidues = residues.length; for (int i = 0; i < nResidues - 1; i++) { Residue resi = residues[i]; Rotamer[] roti = resi.getRotamers(library); int ni = roti.length; for (int j = i + 1; j < nResidues; j++) { Residue resj = residues[j]; Rotamer[] rotj = resj.getRotamers(library); int nj = rotj.length; double minPair = Double.MAX_VALUE; for (int ri = 0; ri < ni; ri++) { if (check(i, ri)) { continue; } for (int rj = 0; rj < nj; rj++) { if (check(j, rj)) { continue; } if (minPair > (pair(i, ri, j, rj) + self(i, ri) + self(j, rj))) { minPair = (pair(i, ri, j, rj) + self(i, ri) + self(j, rj)); } } } //double pruneThreshold = pairClashThreshold; double threshold = pairClashThreshold; if (resi instanceof MultiResidue) { threshold += multiResPairClashAddn; } if (resj instanceof MultiResidue) { threshold += multiResPairClashAddn; } int numNARes = (resi.getResidueType() == NA ? 1 : 0) + (resj.getResidueType() == NA ? 1 : 0); switch (numNARes) { case 0: break; case 1: threshold *= pairHalfPruningFactor; //pruneThreshold *= pairHalfPruningFactor; break; case 2: threshold *= pruningFactor; //pruneThreshold *= pruningFactor; break; default: throw new ArithmeticException(" RotamerOptimization.prunePairClashes() has somehow " + "found less than zero or more than two nucleic acid residues in a pair of" + " residues. This result should be impossible."); } threshold += minPair; // Check for elimination of any rotamer ri. for (int ri = 0; ri < ni; ri++) { if (check(i, ri)) { continue; } double eliminate = Double.MAX_VALUE; for (int rj = 0; rj < nj; rj++) { if (check(j, rj)) { continue; } if ((pair(i, ri, j, rj) + self(i, ri) + self(j, rj)) < eliminate) { eliminate = (pair(i, ri, j, rj) + self(i, ri) + self(j, rj)); } } // Prune based on clash threshold and the appropriate // pruning factor (1.0 for AA pairs, pF for NA pairs, // arithmetic mean for AA-NA pairs). if (eliminate > threshold) { // don't prune orig-coords rotamers if (ri != 0 || !library.getUsingOrigCoordsRotamer()) { eliminateRotamer(residues, i, ri, print); logIfMaster(String.format( " Pruning rotamer %s %d that clashes with all %s rotamers %16.8f >> %16.8f.", resi, ri, resj, eliminate, threshold)); } } /*if (resi.getResidueType() == NA && resj.getResidueType() == NA) { if (eliminate > (minPair + (pairClashThreshold * pruningFactor))) { if (ri != 0 || !RotamerLibrary.getUsingOrigCoordsRotamer()) { eliminateRotamer(residues, i, ri, print); logIfMaster(String.format( " Pruning rotamer %s %d that clashes with all %s rotamers %16.8f >> %16.8f.", resi, ri, resj, eliminate, minPair + (pairClashThreshold * pruningFactor))); } } } else if (resi.getResidueType() == NA || resj.getResidueType() == NA) { if (eliminate > (minPair + (pairClashThreshold * pairHalfPruningFactor))) { if (ri != 0 || !RotamerLibrary.getUsingOrigCoordsRotamer()) { eliminateRotamer(residues, i, ri, print); logIfMaster(String.format( " Pruning rotamer %s %d that clashes with all %s rotamers %16.8f >> %16.8f.", resi, ri, resj, eliminate, minPair + (pairClashThreshold * pairHalfPruningFactor))); } } } else { if (eliminate > (minPair + pairClashThreshold)) { // don't prune orig-coords rotamers if (ri != 0 || !RotamerLibrary.getUsingOrigCoordsRotamer()) { eliminateRotamer(residues, i, ri, print); logIfMaster(String.format( " Pruning rotamer %s %d that clashes with all %s rotamers %16.8f >> %16.8f.", resi, ri, resj, eliminate, minPair + pairClashThreshold)); } } }*/ } // Check for elimination of any rotamer rj. for (int rj = 0; rj < nj; rj++) { if (check(j, rj)) { continue; } double eliminate = Double.MAX_VALUE; for (int ri = 0; ri < ni; ri++) { if (check(i, ri)) { continue; } if ((pair(i, ri, j, rj) + self(i, ri) + self(j, rj)) < eliminate) { eliminate = (pair(i, ri, j, rj) + self(i, ri) + self(j, rj)); } } if (eliminate > threshold) { if (rj != 0 || !library.getUsingOrigCoordsRotamer()) { eliminateRotamer(residues, j, rj, print); logIfMaster(String.format( " Pruning rotamer %s %d that clashes with all %s rotamers %16.8f >> %16.8f.", resj, rj, resi, eliminate, threshold)); } } /*if (resi.getResidueType() == NA && resj.getResidueType() == NA) { if (eliminate > (minPair + (pairClashThreshold * pruningFactor))) { if (rj != 0 || !RotamerLibrary.getUsingOrigCoordsRotamer()) { eliminateRotamer(residues, j, rj, print); logIfMaster(String.format( " Pruning rotamer %s %d that clashes with all %s rotamers %16.8f >> %16.8f.", resj, rj, resi, eliminate, minPair + (pairClashThreshold * pruningFactor))); } } } else if (resi.getResidueType() == NA || resj.getResidueType() == NA) { if (eliminate > (minPair + (pairClashThreshold * pairHalfPruningFactor))) { if (rj != 0 || !RotamerLibrary.getUsingOrigCoordsRotamer()) { eliminateRotamer(residues, j, rj, print); logIfMaster(String.format( " Pruning rotamer %s %d that clashes with all %s rotamers %16.8f >> %16.8f.", resj, rj, resi, eliminate, minPair + (pairClashThreshold * pairHalfPruningFactor))); } } } else { if (eliminate > (minPair + pairClashThreshold)) { // don't prune orig-coords rotamers if (rj != 0 || !RotamerLibrary.getUsingOrigCoordsRotamer()) { eliminateRotamer(residues, j, rj, print); logIfMaster(String.format( " Pruning rotamer %s %d that clashes with all %s rotamers %16.8f >> %16.8f.", resj, rj, resi, eliminate, minPair + pairClashThreshold)); } } }*/ } /* Presently consitutively false if (pruneIndivPairs) { for (int ri = 0; ri < ni; ni++) { if (check(i, ri)) { continue; } for (int rj = 0; rj < nj; nj++) { if (check(j, rj) || check(i, ri, j, rj)) { continue; } double currentPairEnergy = pair(i, ri, j, rj); if (currentPairEnergy > indivPruneThreshold) { eliminateRotamerPair(residues, i, ri, j, ri, print); logIfMaster(String.format(" Pruning rotamer pair %s %d %s %d that clashes with best " + "%s %s rotamer pair %16.8f >> %16.8f.", resi, ri, resj, rj, resi, resj, currentPairEnergy, indivPruneThreshold)); } } } }*/ } } }
From source file:com.clark.func.Functions.java
/** * <p>//from www.j ava 2s . c om * 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 */ private static void modifyDate(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 == 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 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"); }