Java floor floorDiv(long a, long b)

Here you can find the source of floorDiv(long a, long b)

Description

Returns the floor division.

License

Open Source License

Parameter

Parameter Description
a the dividend
b the divisor

Return

the floor division

Declaration

public static long floorDiv(long a, long b) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  ww w. j  av a2  s. c  o  m
     * Returns the floor division.
     * <p>
     * This returns {@code 0} for {@code floorDiv(0, 4)}.<br />
     * This returns {@code -1} for {@code floorDiv(-1, 4)}.<br />
     * This returns {@code -1} for {@code floorDiv(-2, 4)}.<br />
     * This returns {@code -1} for {@code floorDiv(-3, 4)}.<br />
     * This returns {@code -1} for {@code floorDiv(-4, 4)}.<br />
     * This returns {@code -2} for {@code floorDiv(-5, 4)}.<br />
     *
     * @param a  the dividend
     * @param b  the divisor
     * @return the floor division
     */
    public static long floorDiv(long a, long b) {
        return (a >= 0 ? a / b : ((a + 1) / b) - 1);
    }

    /**
     * Returns the floor division.
     * <p>
     * This returns {@code 1} for {@code floorDiv(3, 3)}.<br />
     * This returns {@code 0} for {@code floorDiv(2, 3)}.<br />
     * This returns {@code 0} for {@code floorDiv(1, 3)}.<br />
     * This returns {@code 0} for {@code floorDiv(0, 3)}.<br />
     * This returns {@code -1} for {@code floorDiv(-1, 3)}.<br />
     * This returns {@code -1} for {@code floorDiv(-2, 3)}.<br />
     * This returns {@code -1} for {@code floorDiv(-3, 3)}.<br />
     * This returns {@code -2} for {@code floorDiv(-4, 3)}.<br />
     *
     * @param a  the dividend
     * @param b  the divisor
     * @return the floor division
     */
    public static int floorDiv(int a, int b) {
        return (a >= 0 ? a / b : ((a + 1) / b) - 1);
    }
}

Related

  1. floorDiv(final int x, final int y)
  2. floorDiv(int dividend, int divisor)
  3. floorDiv(int dividend, int divisor)
  4. floorDiv(int dividend, int divisor)
  5. floorDiv(int v1, int v2)
  6. floorDiv(long x, long y)
  7. floorDivide(int value, int divisor)
  8. floorDivide(long n, long d)
  9. floorDivide(long numerator, long denominator)