Java Number Minus minusExact(final int a, final int b)

Here you can find the source of minusExact(final int a, final int b)

Description

minus Exact

License

Open Source License

Parameter

Parameter Description
a An int value.
b An int value.

Exception

Parameter Description
ArithmeticException if the mathematical result of a-b is not in [Integer.MIN_VALUE,Integer.MAX_VALUE] range.

Return

The mathematical result of a-b.

Declaration

public static int minusExact(final int a, final int b) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/* www .  java2  s.  c o  m*/
     * @param a
     *            An int value.
     * @param b
     *            An int value.
     * @return The mathematical result of a-b.
     * @throws ArithmeticException
     *             if the mathematical result of a-b is not in [Integer.MIN_VALUE,Integer.MAX_VALUE] range.
     */
    public static int minusExact(final int a, final int b) {
        final int diff = a - b;
        // HD 2-12 Overflow iff the arguments have different signs and
        // the sign of the result is different than the sign of "a".
        if (((a ^ b) & (a ^ diff)) < 0) {
            throw new ArithmeticException("integer overflow");
        }
        return diff;
    }

    /**
     * @param a
     *            A long value.
     * @param b
     *            A long value.
     * @return The mathematical result of a-b.
     * @throws ArithmeticException
     *             if the mathematical result of a-b is not in [Long.MIN_VALUE,Long.MAX_VALUE] range.
     */
    public static long minusExact(final long a, final long b) {
        final long diff = a - b;
        // HD 2-12 Overflow iff the arguments have different signs and
        // the sign of the result is different than the sign of "a".
        if (((a ^ b) & (a ^ diff)) < 0) {
            throw new ArithmeticException("integer overflow");
        }
        return diff;
    }
}

Related

  1. minusculas(char c)
  2. minusDayOfWeek(int dow, long days)
  3. minusEquals(double[] w, double v)
  4. minusEquals(int[] vals, int min)
  5. minusEqualsInverse(double[] w, double[] v, double a)
  6. minusExact(long a, long b)
  7. minusPercent(float maxValue, float minusValue)
  8. minusPIO2(final double angRad)