Java Long Number Divide divide(long dividend, long divisor)

Here you can find the source of divide(long dividend, long divisor)

Description

Returns dividend / divisor, where the dividend and divisor are treated as unsigned 64-bit quantities.

License

Apache License

Parameter

Parameter Description
dividend the dividend (numerator)
divisor the divisor (denominator)

Exception

Parameter Description
ArithmeticException if divisor is 0

Declaration

private static long divide(long dividend, long divisor) 

Method Source Code

//package com.java2s;
//Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    /**//w  ww  .j  a v a 2  s .  c o  m
     * Returns dividend / divisor, where the dividend and divisor are treated as unsigned 64-bit quantities.
     *
     * @param dividend
     *            the dividend (numerator)
     * @param divisor
     *            the divisor (denominator)
     * @throws ArithmeticException
     *             if divisor is 0
     */
    private static long divide(long dividend, long divisor) {
        if (divisor < 0) { // i.e., divisor >= 2^63:
            if (compareUnsigned(dividend, divisor) < 0) {
                return 0; // dividend < divisor
            } else {
                return 1; // dividend >= divisor
            }
        }

        // Optimization - use signed division if dividend < 2^63
        if (dividend >= 0) {
            return dividend / divisor;
        }

        /*
         * Otherwise, approximate the quotient, check, and correct if necessary. Our approximation is guaranteed to be
         * either exact or one less than the correct value. This follows from fact that floor(floor(x)/i) == floor(x/i)
         * for any real x and integer i != 0. The proof is not quite trivial.
         */
        long quotient = ((dividend >>> 1) / divisor) << 1;
        long rem = dividend - quotient * divisor;
        return quotient + (compareUnsigned(rem, divisor) >= 0 ? 1 : 0);
    }

    /**
     * Compares the two specified {@code long} values, treating them as unsigned values between {@code 0} and
     * {@code 2^64 - 1} inclusive.
     *
     * @param a
     *            the first unsigned {@code long} to compare
     * @param b
     *            the second unsigned {@code long} to compare
     * @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is greater than
     *         {@code b}; or zero if they are equal
     */
    private static int compareUnsigned(long a, long b) {
        return compareSigned(flip(a), flip(b));
    }

    /**
     * Compares the two specified {@code long} values. The sign of the value returned is the same as that of
     * {@code ((Long) a).compareTo(b)}.
     *
     * <p>
     * <b>Note for Java 7 and later:</b> this method should be treated as deprecated; use the equivalent
     * {@link Long#compare} method instead.
     *
     * @param a
     *            the first {@code long} to compare
     * @param b
     *            the second {@code long} to compare
     * @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is greater than
     *         {@code b}; or zero if they are equal
     */
    private static int compareSigned(long a, long b) {
        return (a < b) ? -1 : ((a > b) ? 1 : 0);
    }

    private static int flip(int value) {
        return value ^ Integer.MIN_VALUE;
    }

    /**
     * A (self-inverse) bijection which converts the ordering on unsigned longs to the ordering on longs, that is,
     * {@code a <= b} as unsigned longs if and only if {@code flip(a) <= flip(b)} as signed longs.
     */
    private static long flip(long a) {
        return a ^ Long.MIN_VALUE;
    }
}

Related

  1. div100(Long input)
  2. div_ceil(long n, long d)
  3. divCeil(long a, long b)
  4. divide(final long dividend, final long divisor)
  5. divide(Long d1, Long d2)
  6. divide(long l1, long l2)
  7. divideAndRoundUp(long x, long y)
  8. divideForMean(long sum, long count)
  9. divideLongBy1000(long l)