Java examples for java.lang:Math Calculation
Computes the floored division dividend/divisor which is useful when dividing potentially negative numbers into bins.
// Copyright (C) 2002-2012 Three Rings Design, Inc., All Rights Reserved //package com.java2s; public class Main { /**//from w w w . j av a2 s . c o m * Computes the floored division <code>dividend/divisor</code> which * is useful when dividing potentially negative numbers into bins. * * <p> For example, the following numbers floorDiv 10 are: * <pre> * -15 -10 -8 -2 0 2 8 10 15 * -2 -1 -1 -1 0 0 0 1 1 * </pre> */ public static int floorDiv(int dividend, int divisor) { return ((dividend >= 0) == (divisor >= 0)) ? dividend / divisor : (divisor >= 0 ? (dividend - divisor + 1) / divisor : (dividend - divisor - 1) / divisor); } }