0

Seems like Math.floor(x) is slower than (int) x as suggested in this test:

public final class FloorRunningTime {
    public static void main(final String[] args) {
        final double number = 4999.7;

        floorMethod(number);
        downcastMethod(number);

    }

    private static void floorMethod(final double number) {
        long start = System.nanoTime();
        final int floorValue = (int) Math.floor(number);
        long duration = System.nanoTime() - start;
        System.out.printf("Floor value is: %d. Running time (nanoseconds): %d.%n", floorValue,
            duration);
    }

    private static void downcastMethod(final double number) {
        long start = System.nanoTime();
        final int floorValue = (int) number;
        long duration = System.nanoTime() - start;
        System.out.printf("Floor value is: %d. Running time (nanoseconds): %d.%n", floorValue,
            duration);
    }
}

The output for this run is:

Floor value is: 4999. Running time (nanoseconds): 807820.
Floor value is: 4999. Running time (nanoseconds): 236.

This might make a difference on when running repetitive calls in order to floor different numbers.

Is any reason one should use Math.floor(x) instead of (int) x, when doing the operation for positive numbers?

Alex
  • 971
  • 1
  • 9
  • 23
  • I would not call that a relevant benchmark. You need at least to execute the same kind of operations a lot of time and run the two different methods in a different instance of JVM, otherwise the second that will run will benefit from the warm-up – Dici Sep 05 '15 at 20:40
  • Floor returns integer representation of double, and it returns double for scientific calculations. – lesnar Sep 05 '15 at 20:45
  • 1
    Not sure the duplicate is super relevant to the OP – Dici Sep 05 '15 at 20:50
  • There is clearly a difference between the running time for the two operations. If I run the two methods in different JVMs, the running time difference has the same order of magnitude as in my original post. – Alex Sep 05 '15 at 20:54
  • 1
    If I'm remembering this right, (and I'm not sure) it has something to do with the fact that any decimal number which isn't an exact inverse power of two can't be precisely represented in binary, so it's possibe that the loss of accuracy with a sufficiently small mantissa would result in an incorrect result when casting a floating point number to an int. i.e. casting 4999.000....1 to an int might yield 4998. https://en.wikipedia.org/wiki/Double-precision_floating-point_format – user5292387 Sep 05 '15 at 20:59
  • @user5292387 ints are also much smaller than the biggest `double`, same for longs, therefoe not every `double` can be caster down to an integer – Dici Sep 05 '15 at 21:07
  • If it's a duplicate question, then I'd be fascinated to know what the answer is, since the other post doesn't answer the question posed here. – user5292387 Sep 05 '15 at 21:17
  • @Dici I understand, but my use case is like this: I want to floor this number: (a + b)/2, where both a and b are integers (so the arithmetic mean should not overflow the integer scale). This is actually the divide step for a merge sort algorithm which divides an array of a given length into two sub-arrays. – Alex Sep 05 '15 at 21:18
  • 1
    Then why do you use doubles ? The division on integers naturally implements what you want, it returns the whole part of the fraction – Dici Sep 05 '15 at 21:20
  • @Dici Yes, right, that is how I do it like now and I am fine with it. Before I used that floor function in Math class, and I got a bigger running time, and I was curious why. But now I realize that Math#floor function is a much more general and you should use it in different cases... – Alex Sep 05 '15 at 21:30
  • Ok makes sense. I don't have a good answer for this, and the "duplicate" is really not helpful – Dici Sep 05 '15 at 21:33
  • Voting to reopen as not a duplicate, because I feel the supposed duplicate doesn't address the same question. The dupe is about why it's slower, this question is about what the difference is. – Maximillian Laumeister Sep 05 '15 at 22:01
  • @MaximillianLaumeister He's talking about **positive** numbers. There is no difference for positive numbers. I thought about this before closing as dupe. The only difference between casting and floor is for negative numbers... and the performance issue that is discussed at length in **both this post and the duplicate**. This is a duplicate. – durron597 Sep 06 '15 at 00:03

0 Answers0