Java Integer Divide divideWithoutOverflow(int x, int y)

Here you can find the source of divideWithoutOverflow(int x, int y)

Description

Integer divide without overflow

License

Apache License

Exception

Parameter Description
ArithmeticException on overflow or divide-by-zero

Declaration

public static int divideWithoutOverflow(int x, int y) 

Method Source Code

//package com.java2s;
/*//from w w  w.  j a  va2 s  . co  m
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

public class Main {
    /**
     * Integer divide without overflow
     * @throws ArithmeticException on overflow or divide-by-zero
     */
    public static int divideWithoutOverflow(int x, int y) {
        if (x == Integer.MIN_VALUE && y == -1) {
            throw new ArithmeticException("integer overflow");
        }
        return x / y;
    }

    /**
     * Long divide without overflow
     * @throws ArithmeticException on overflow or divide-by-zero
     */
    public static long divideWithoutOverflow(long x, long y) {
        if (x == Long.MIN_VALUE && y == -1L) {
            throw new ArithmeticException("long overflow");
        }
        return x / y;
    }

    /**
     * Divides two floats but throws {@code ArithmeticException}
     * if the result overflows, or would create NaN from finite
     * inputs ({@code x == 0, y == 0})
     */
    public static float divideWithoutOverflow(float x, float y) {
        return checkNaNFloat(x, y, checkInfFloat(x, y, x / y));
    }

    /**
     * Divides two doubles but throws {@code ArithmeticException}
     * if the result overflows, or would create NaN from finite
     * inputs ({@code x == 0, y == 0})
     */
    public static double divideWithoutOverflow(double x, double y) {
        return checkNaNDouble(x, y, checkInfDouble(x, y, x / y));
    }

    /**
     * Checks for NaN, result is NaN but operands are finite
     * @throws ArithmeticException if overflow occurred
     */
    private static float checkNaNFloat(float x, float y, float z) {
        if (Float.isNaN(z)) {
            if (Float.isFinite(x) && Float.isFinite(y)) {
                throw new ArithmeticException("NaN");
            }
        }
        return z;
    }

    /**
     * Checks for overflow, result is infinite but operands are finite
     * @throws ArithmeticException if overflow occurred
     */
    private static float checkInfFloat(float x, float y, float z) {
        if (Float.isInfinite(z)) {
            if (Float.isFinite(x) && Float.isFinite(y)) {
                throw new ArithmeticException("float overflow");
            }
        }
        return z;
    }

    /**
     * Checks for NaN, result is NaN but operands are finite
     * @throws ArithmeticException if overflow occurred
     */
    private static double checkNaNDouble(double x, double y, double z) {
        if (Double.isNaN(z)) {
            if (Double.isFinite(x) && Double.isFinite(y)) {
                throw new ArithmeticException("NaN");
            }
        }
        return z;
    }

    /**
     * Checks for NaN, result is infinite but operands are finite
     * @throws ArithmeticException if overflow occurred
     */
    private static double checkInfDouble(double x, double y, double z) {
        if (Double.isInfinite(z)) {
            if (Double.isFinite(x) && Double.isFinite(y)) {
                throw new ArithmeticException("double overflow");
            }
        }
        return z;
    }
}

Related

  1. divideStringWithInt(String strDividend, int divisor)
  2. divideToCeil(int numerator, int denominator)
  3. divideToInt(String address)
  4. divideUnsigned(int dividend, int divisor)
  5. divideWithCeilingRoundingMode(int p, int q)
  6. division(int i_Divisor, int i_Dividend)
  7. divisor(int a, int b)
  8. divisor(int m, int n)
  9. divisorMod(int a, int n)