FastMath { public static int floorDiv(final int a, final int b) throws MathArithmeticException { if (b == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR); } final int m = a % b; if ((a ^ b) >= 0 || m == 0) { return a / b; } else { return (a / b) - 1; } } private  FastMath(); static double sqrt(final double a); static double cosh(double x); static double sinh(double x); static double tanh(double x); static double acosh(final double a); static double asinh(double a); static double atanh(double a); static double signum(final double a); static float signum(final float a); static double nextUp(final double a); static float nextUp(final float a); static double nextDown(final double a); static float nextDown(final float a); static double random(); static double exp(double x); static double expm1(double x); static double log(final double x); static double log1p(final double x); static double log10(final double x); static double log(double base, double x); static double pow(double x, double y); static double pow(double d, int e); static double sin(double x); static double cos(double x); static double tan(double x); static double atan(double x); static double atan2(double y, double x); static double asin(double x); static double acos(double x); static double cbrt(double x); static double toRadians(double x); static double toDegrees(double x); static int abs(final int x); static long abs(final long x); static float abs(final float x); static double abs(double x); static double ulp(double x); static float ulp(float x); static double scalb(final double d, final int n); static float scalb(final float f, final int n); static double nextAfter(double d, double direction); static float nextAfter(final float f, final double direction); static double floor(double x); static double ceil(double x); static double rint(double x); static long round(double x); static int round(final float x); static int min(final int a, final int b); static long min(final long a, final long b); static float min(final float a, final float b); static double min(final double a, final double b); static int max(final int a, final int b); static long max(final long a, final long b); static float max(final float a, final float b); static double max(final double a, final double b); static double hypot(final double x, final double y); static double IEEEremainder(double dividend, double divisor); static int toIntExact(final long n); static int incrementExact(final int n); static long incrementExact(final long n); static int decrementExact(final int n); static long decrementExact(final long n); static int addExact(final int a, final int b); static long addExact(final long a, final long b); static int subtractExact(final int a, final int b); static long subtractExact(final long a, final long b); static int multiplyExact(final int a, final int b); static long multiplyExact(final long a, final long b); static int floorDiv(final int a, final int b); static long floorDiv(final long a, final long b); static int floorMod(final int a, final int b); static long floorMod(final long a, final long b); static double copySign(double magnitude, double sign); static float copySign(float magnitude, float sign); static int getExponent(final double d); static int getExponent(final float f); static void main(String[] a); static final double PI; static final double E; }
@Test public void testFloorDivInt() { Assert.assertEquals(+1, FastMath.floorDiv(+4, +3)); Assert.assertEquals(-2, FastMath.floorDiv(-4, +3)); Assert.assertEquals(-2, FastMath.floorDiv(+4, -3)); Assert.assertEquals(+1, FastMath.floorDiv(-4, -3)); try { FastMath.floorDiv(1, 0); Assert.fail("an exception should have been thrown"); } catch (MathArithmeticException mae) { } for (int a = -100; a <= 100; ++a) { for (int b = -100; b <= 100; ++b) { if (b != 0) { Assert.assertEquals(poorManFloorDiv(a, b), FastMath.floorDiv(a, b)); } } } }
@Test public void testFloorDivLong() { Assert.assertEquals(+1l, FastMath.floorDiv(+4l, +3l)); Assert.assertEquals(-2l, FastMath.floorDiv(-4l, +3l)); Assert.assertEquals(-2l, FastMath.floorDiv(+4l, -3l)); Assert.assertEquals(+1l, FastMath.floorDiv(-4l, -3l)); try { FastMath.floorDiv(1l, 0l); Assert.fail("an exception should have been thrown"); } catch (MathArithmeticException mae) { } for (long a = -100l; a <= 100l; ++a) { for (long b = -100l; b <= 100l; ++b) { if (b != 0) { Assert.assertEquals(poorManFloorDiv(a, b), FastMath.floorDiv(a, b)); } } } }