Here you can find the source of floorDiv(long a, long b)
Parameter | Description |
---|---|
a | the dividend |
b | the divisor |
public static long floorDiv(long a, long b)
//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); } }