aboutsummaryrefslogtreecommitdiff
path: root/src/Java/gtPlusPlus/core/util/math
diff options
context:
space:
mode:
Diffstat (limited to 'src/Java/gtPlusPlus/core/util/math')
-rw-r--r--src/Java/gtPlusPlus/core/util/math/MathUtils.java70
1 files changed, 67 insertions, 3 deletions
diff --git a/src/Java/gtPlusPlus/core/util/math/MathUtils.java b/src/Java/gtPlusPlus/core/util/math/MathUtils.java
index bda722b47e..2f4db2efcc 100644
--- a/src/Java/gtPlusPlus/core/util/math/MathUtils.java
+++ b/src/Java/gtPlusPlus/core/util/math/MathUtils.java
@@ -13,6 +13,8 @@ import gtPlusPlus.core.util.Utils;
public class MathUtils {
+ final static Random rand = CORE.RANDOM;
+
/**
* Returns a psuedo-random number between min and max, inclusive.
* The difference between min and max can be at most
@@ -23,9 +25,6 @@ public class MathUtils {
* @return Integer between min and max, inclusive.
* @see java.util.Random#nextInt(int)
*/
-
- final static Random rand = CORE.RANDOM;
-
public static int randInt(final int min, final int max) {
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
@@ -692,6 +691,39 @@ public class MathUtils {
public static int balance(int aInput, int aMin, int aMax) {
return Math.max(Math.min(aInput, aMax), aMin);
}
+
+ /**
+ * Balances a number within a range.
+ * @param aInput - The number to balance
+ * @param aMin - The minimum bounds
+ * @param aMax - The maximum bounds
+ * @return - A Number which will be between the bounds, or a boundary value.
+ */
+ public static Number balance(Number aInput, Number aMin, Number aMax) {
+ return max(min(aInput, aMax), aMin);
+ }
+
+ /**
+ * Balances a number within a range.
+ * @param aInput - The number to balance
+ * @param aMin - The minimum bounds
+ * @param aMax - The maximum bounds
+ * @return - An Integer which will be between the bounds, or a boundary value.
+ */
+ public static int balanceInt(Number aInput, Number aMin, Number aMax) {
+ return MathUtils.safeCast_LongToInt((long) balance(max(min(aInput, aMax), aMin), Integer.MIN_VALUE, Integer.MAX_VALUE));
+ }
+
+ /**
+ * Balances a number within a range.
+ * @param aInput - The number to balance
+ * @param aMin - The minimum bounds
+ * @param aMax - The maximum bounds
+ * @return - A Long which will be between the bounds, or a boundary value.
+ */
+ public static long balanceLong(Number aInput, Number aMin, Number aMax) {
+ return (long) balance(max(min(aInput, aMax), aMin), Long.MIN_VALUE, Long.MAX_VALUE);
+ }
public static int getValueWithinRange(int i, int aMin, int aMax) {
int aAmount = Math.max(Math.min(i, aMax), aMin);
@@ -703,5 +735,37 @@ public class MathUtils {
int aRemainder = (int) (aLong - (aIntMaxInLong * Integer.MAX_VALUE));
return new Pair<Integer, Integer>(aIntMaxInLong, aRemainder);
}
+
+
+
+
+ /**
+ * Returns the smaller of two {@code Number}s. That is,
+ * the result the argument closer to the value of
+ * {@link Long#MIN_VALUE}. If the arguments have the same
+ * value, the result is that same value.
+ *
+ * @param a an argument.
+ * @param b another argument.
+ * @return the smaller of {@code a} and {@code b}.
+ */
+ public static Number min(Number a, Number b) {
+ return (a.longValue() <= b.longValue()) ? a : b;
+ }
+
+ /**
+ * Returns the greater of two {@code Number}s. That is, the
+ * result is the argument closer to the value of
+ * {@link Long#MAX_VALUE}. If the arguments have the same value,
+ * the result is that same value.
+ *
+ * @param a an argument.
+ * @param b another argument.
+ * @return the larger of {@code a} and {@code b}.
+ */
+ public static Number max(Number a, Number b) {
+ return (a.longValue() >= b.longValue()) ? a : b;
+ }
+
}