## 2010-02-04

### FastMath :: fast floor + ceil

Calling Math.floor() simply takes too long. The problem with optimizing is that you can't simply cast the floatingpoint number to an integer, because that will result in invalid results for negative numbers. If we know our input values are in a specific range, we can safely add a certain (big) constant to the input, cast the guaranteed positive value to an integer and subtract the constant again.

The code is ~9x faster than Math.floor(). Replacing the doubles with floats makes it faster, but the results are rather... random, so don't.

```public class FastMath
{
private static final int    BIG_ENOUGH_INT   = 16 * 1024;
private static final double BIG_ENOUGH_FLOOR = BIG_ENOUGH_INT + 0.0000;
private static final double BIG_ENOUGH_ROUND = BIG_ENOUGH_INT + 0.5000;
private static final double BIG_ENOUGH_CEIL  = BIG_ENOUGH_INT + 0.9999;

public static int fastFloor(float x) {
return (int) (x + BIG_ENOUGH_FLOOR) - BIG_ENOUGH_INT;
}

public static int fastRound(float x) {
return (int) (x + BIG_ENOUGH_ROUND) - BIG_ENOUGH_INT;
}

public static int fastCeil(float x) {
return (int) (x + BIG_ENOUGH_CEIL) - BIG_ENOUGH_INT;
}
}
```

1. Clever code, very useful. Thanks for posting.
Keith

2. I'll append to this as I found this to be the fastest version overall. I was using doubles so the speed improvements are different, but I tried 4 different approaches.

double val

Standard Floor: Math.floor(val)
Bound checking: val < 0 ? ((int)val) - 1 : (int)val
Adding/Subtracting (Riven method): ((int)(val + x)) - x
Raw casting (flawed): (int)val

The results, running many loops and first burning it in before timing so it's not a hotspot artifact.

Standard Floor: 30.41 seconds
Bound checking: 14.26 seconds