From 1cb3d1edb5dd12403223841727f9178f93f1a8b5 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Sat, 19 Apr 2003 22:28:28 +0000 Subject: [PATCH] Added floorDiv(). git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2432 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/media/util/MathUtil.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/java/com/threerings/media/util/MathUtil.java b/src/java/com/threerings/media/util/MathUtil.java index da5dd62c3..46da6e072 100644 --- a/src/java/com/threerings/media/util/MathUtil.java +++ b/src/java/com/threerings/media/util/MathUtil.java @@ -1,5 +1,5 @@ // -// $Id: MathUtil.java,v 1.8 2003/04/12 02:06:35 mdb Exp $ +// $Id: MathUtil.java,v 1.9 2003/04/19 22:28:28 mdb Exp $ package com.threerings.media.util; @@ -76,4 +76,21 @@ public class MathUtil { return (value < 0) ? -1 : 1; } + + /** + * Computes the floored division dividend/divisor which + * is useful when dividing potentially negative numbers into bins. For + * positive numbers, it is the same as normal division, for negative + * numbers it returns (dividend - divisor + 1) / divisor. + * + *

For example, the following numbers floorDiv 10 are: + *

+     * -15 -10 -8 -2 0 2 8 10 15
+     *  -2  -1 -1 -1 0 0 0  1  1
+     * 
+ */ + public static int floorDiv (int dividend, int divisor) + { + return ((dividend >= 0) ? dividend : (dividend - divisor + 1))/divisor; + } }