5

I was trying to use java's integer division, and it supposedly takes the floor. However, it rounds towards zero instead of the floor.

public class Main {
    public static void main(String[] args) {
        System.out.println(-1 / 100); // should be -1, but is 0
        System.out.println(Math.floor(-1d/100d)); // correct
    }
}

The problem is that I do not want to convert to a double/float because it needs to be efficient. I'm trying to solve this with a method, floorDivide(long a, long b). What I have is:

static long floorDivide(long a, long b) {
    if (a < 0) {
        // what do I put here?
    }
    return a / b;
}

How can I do this without a double/float?

1
  • 1
    There's nothing inefficient about casting one of your ints to double.
    – Zircon
    May 18, 2016 at 19:16

7 Answers 7

12

floorDiv() from Java.Math that does exactly what you want.

static long floorDiv(long x, long y)

Returns the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient.

1

Take the absolute value, divide it, multiply it by -1.

Weird bug.

2
  • 9
    That's not a bug, multiple way to compute a quotient/remainder exists. The one that rounds toward -1 is the euclidean division, while Java uses a truncated division.
    – Jack
    May 18, 2016 at 19:19
  • 2
    Doesn't work with something like -1 / 4. It gives 0 when I want -1
    – AMACB
    May 18, 2016 at 20:19
0

You can use

  int i = (int) Math.round(doubleValToRound);

It will return a double value that you can cast into an int without lost of precission and without performance problems (casts haven't a great computational cost)

Also it's equivalent to

 int a = (int) (doubleValToRound + 0.5);
 //in your case
 System.out.println((int) ((-1 / 100) + 0.5));

With this last one you won't have to enter into tedious and unnecesary "if" instructions. Like a good suit, its valid for every moment and has a higher portability for other languages.

0

This is ugly, but meets the requirement to not use a double/float. Really you should just cast it to a double.

The logic here is take the floor of a negative result from the integer division if it doesn't divide evenly.

static long floorDivide(long a, long b)
{
    if(a % b != 0 && ((a < 0 && b > 0) || (a > 0 && b < 0)))
    {
        return (a / b - 1);
    }
    else
    {
        return (a / b);
    }
}
0
0

Just divide the two integers. then add -1 to the result (in case the absolute value of both numerator and denominator are not same). For example -3/3 gives you -1, the right answer without adding -1 to the division.

0

Since a bit late, but you need to convert your parameters to long or double

int result = (int) Math.floor( (double) -1 / 5 );
// result == -1

This worked for me elegantly.

0

I would use floorDiv() for a general case, as Frank Harper suggested.

Note, however, that when the divisor is a power of 2, the division is often substituted by a right shift by an appropriate number of bits, i.e.

x / d

is the same as

x >> p

when p = 0,1,...,30 (or 0,1,...,62 for longs), d = 2p and x is non-negative. This is not only more effective than ordinary division but gives the right result (in mathematical sense) when x is negative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.