/** Increment a number, detecting overflows. * @param n number to increment * @return n+1 if no overflows occur * @exception MathArithmeticException if an overflow occurs * @since 3.4 */ public static long incrementExact(final long n) throws MathArithmeticException { if (n == Long.MAX_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, n, 1); } return n + 1; }
/** Increment a number, detecting overflows. * @param n number to increment * @return n+1 if no overflows occur * @exception MathArithmeticException if an overflow occurs * @since 3.4 */ public static int incrementExact(final int n) throws MathArithmeticException { if (n == Integer.MAX_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, n, 1); } return n + 1; }
/** Decrement a number, detecting overflows. * @param n number to decrement * @return n-1 if no overflows occur * @exception MathArithmeticException if an overflow occurs * @since 3.4 */ public static long decrementExact(final long n) throws MathArithmeticException { if (n == Long.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, n, 1); } return n - 1; }
/** Decrement a number, detecting overflows. * @param n number to decrement * @return n-1 if no overflows occur * @exception MathArithmeticException if an overflow occurs * @since 3.4 */ public static int decrementExact(final int n) throws MathArithmeticException { if (n == Integer.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, n, 1); } return n - 1; }
/** Convert a long to interger, detecting overflows * @param n number to convert to int * @return integer with same valie as n if no overflows occur * @exception MathArithmeticException if n cannot fit into an int * @since 3.4 */ public static int toIntExact(final long n) throws MathArithmeticException { if (n < Integer.MIN_VALUE || n > Integer.MAX_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW); } return (int) n; }
/** Multiply two numbers, detecting overflows. * @param a first number to multiply * @param b second number to multiply * @return a*b if no overflows occur * @exception MathArithmeticException if an overflow occurs * @since 3.4 */ public static int multiplyExact(final int a, final int b) { if (((b > 0) && (a > Integer.MAX_VALUE / b || a < Integer.MIN_VALUE / b)) || ((b < -1) && (a > Integer.MIN_VALUE / b || a < Integer.MAX_VALUE / b)) || ((b == -1) && (a == Integer.MIN_VALUE))) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_MULTIPLICATION, a, b); } return a * b; }
/** Multiply two numbers, detecting overflows. * @param a first number to multiply * @param b second number to multiply * @return a*b if no overflows occur * @exception MathArithmeticException if an overflow occurs * @since 3.4 */ public static long multiplyExact(final long a, final long b) { if (((b > 0l) && (a > Long.MAX_VALUE / b || a < Long.MIN_VALUE / b)) || ((b < -1l) && (a > Long.MIN_VALUE / b || a < Long.MAX_VALUE / b)) || ((b == -1l) && (a == Long.MIN_VALUE))) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_MULTIPLICATION, a, b); } return a * b; }
/** * Return the additive inverse of this fraction. * @return the negation of this fraction. */ public Fraction negate() { if (numerator==Integer.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION, numerator, denominator); } return new Fraction(-numerator, denominator); }
/** * {@inheritDoc} * * @throws MathArithmeticException if {@code a} is zero */ public BigReal divide(BigReal a) throws MathArithmeticException { try { return new BigReal(d.divide(a.d, scale, roundingMode)); } catch (ArithmeticException e) { // Division by zero has occurred throw new MathArithmeticException(LocalizedFormats.ZERO_NOT_ALLOWED); } }
/** * {@inheritDoc} * * @throws MathArithmeticException if {@code this} is zero */ public BigReal reciprocal() throws MathArithmeticException { try { return new BigReal(BigDecimal.ONE.divide(d, scale, roundingMode)); } catch (ArithmeticException e) { // Division by zero has occurred throw new MathArithmeticException(LocalizedFormats.ZERO_NOT_ALLOWED); } }
/** {@inheritDoc} */ public Vector3D normalize() throws MathArithmeticException { double s = getNorm(); if (s == 0) { throw new MathArithmeticException(LocalizedFormats.CANNOT_NORMALIZE_A_ZERO_NORM_VECTOR); } return scalarMultiply(1 / s); }
/** {@inheritDoc} */ public Vector1D normalize() throws MathArithmeticException { double s = getNorm(); if (s == 0) { throw new MathArithmeticException(LocalizedFormats.CANNOT_NORMALIZE_A_ZERO_NORM_VECTOR); } return scalarMultiply(1 / s); } /** {@inheritDoc} */
/** {@inheritDoc} */ public Vector2D normalize() throws MathArithmeticException { double s = getNorm(); if (s == 0) { throw new MathArithmeticException(LocalizedFormats.CANNOT_NORMALIZE_A_ZERO_NORM_VECTOR); } return scalarMultiply(1 / s); }
/** {@inheritDoc} */ public FieldVector<T> mapInvToSelf() throws MathArithmeticException { final T one = field.getOne(); for (int i = 0; i < data.length; i++) { try { data[i] = one.divide(data[i]); } catch (final MathArithmeticException e) { throw new MathArithmeticException(LocalizedFormats.INDEX, i); } } return this; }
/** Set the normal vactor. * @param normal normal direction to the plane (will be copied) * @exception MathArithmeticException if the normal norm is too small */ private void setNormal(final Vector3D normal) throws MathArithmeticException { final double norm = normal.getNorm(); if (norm < 1.0e-10) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } w = new Vector3D(1.0 / norm, normal); }
/** * Creates a unit vector pointing in the direction of this vector. * The instance is not changed by this method. * * @return a unit vector pointing in direction of this vector. * @throws MathArithmeticException if the norm is zero. */ public RealVector unitVector() throws MathArithmeticException { final double norm = getNorm(); if (norm == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } return mapDivide(norm); }
/** * Converts this vector into a unit vector. * The instance itself is changed by this method. * * @throws MathArithmeticException if the norm is zero. */ public void unitize() throws MathArithmeticException { final double norm = getNorm(); if (norm == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } mapDivideToSelf(getNorm()); }
/** Get a normalized vector aligned with the instance. * @return a new normalized vector * @exception MathArithmeticException if the norm is zero */ public FieldVector3D<T> normalize() throws MathArithmeticException { final T s = getNorm(); if (s.getReal() == 0) { throw new MathArithmeticException(LocalizedFormats.CANNOT_NORMALIZE_A_ZERO_NORM_VECTOR); } return scalarMultiply(s.reciprocal()); }
/** {@inheritDoc} */ public FieldVector<T> mapInv() throws MathArithmeticException { T[] out = MathArrays.buildArray(field, data.length); final T one = field.getOne(); for (int i = 0; i < data.length; i++) { try { out[i] = one.divide(data[i]); } catch (final MathArithmeticException e) { throw new MathArithmeticException(LocalizedFormats.INDEX, i); } } return new ArrayFieldVector<T>(field, out, false); }
/** {@inheritDoc} */ @Override public void unitize() throws MathArithmeticException { double norm = getNorm(); if (isDefaultValue(norm)) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); entries.put(iter.key(), iter.value() / norm); } }