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.java376
1 files changed, 190 insertions, 186 deletions
diff --git a/src/Java/gtPlusPlus/core/util/math/MathUtils.java b/src/Java/gtPlusPlus/core/util/math/MathUtils.java
index 69e4a1a446..75d8452975 100644
--- a/src/Java/gtPlusPlus/core/util/math/MathUtils.java
+++ b/src/Java/gtPlusPlus/core/util/math/MathUtils.java
@@ -1,259 +1,263 @@
package gtPlusPlus.core.util.math;
+import gtPlusPlus.core.util.Utils;
+
import java.util.Map;
import java.util.Random;
-import gtPlusPlus.core.util.Utils;
-
public class MathUtils {
/**
- * Returns an int. The returned number is the value on i + 273.15F. Supports
- * ints.
+ * Returns a psuedo-random number between min and max, inclusive.
+ * The difference between min and max can be at most
+ * Integer.MAX_VALUE - 1.
*
- * @param i
- * Temp in Celcius.
- * @return int The celcius temp returned as Kelvin, rounded to the readest
- * whole.
+ * @param min Minimim value
+ * @param max Maximim value. Must be greater than min.
+ * @return Integer between min and max, inclusive.
+ * @see java.util.Random#nextInt(int)
*/
- public static float celsiusToKelvin(final int i) {
- final double f = i + 273.15F;
- return (int) MathUtils.decimalRoundingToWholes(f);
- }
+ public static int randInt(int min, int max) {
- // Smooth Rounding Function
+ // Usually this can be a field rather than a method variable
+ Random rand = new Random();
+
+ // nextInt is normally exclusive of the top value,
+ // so add 1 to make it inclusive
+ int randomNum = rand.nextInt((max - min) + 1) + min;
+
+ return randomNum;
+ }
+
+ public static double getChanceOfXOverYRuns(double x, double y){
+ double z = (1-Math.pow((1-x), y));
+ return z;
+ }
+
+
/**
- * Returns a double. The returned number is d rounded to the nearest d.01.
- * Supports Doubles.
+ * Returns a psuedo-random number between min and max, inclusive.
+ * The difference between min and max can be at most
+ * Long.MAX_VALUE - 1.
*
- * @param current
- * Current value.
- * @return double Rounded value.
+ * @param min Minimim value
+ * @param max Maximim value. Must be greater than min.
+ * @return Long between min and max, inclusive.
+ * @see java.util.Random#nextLong(long)
*/
- public static double decimalRounding(final double d) {
- return Math.round(d * 2) / 2.0;
+ public static long randLong(long min, long max) {
+ // Usually this can be a field rather than a method variable
+ Random rand = new Random();
+
+ // nextInt is normally exclusive of the top value,
+ // so add 1 to make it inclusive
+ long randomNum = MathUtils.nextLong(rand,(max - min) + 1) + min;
+
+ return randomNum;
+ }
+ private static long nextLong(Random rng, long n) {
+ // error checking and 2^x checking removed for simplicity.
+ long bits, val;
+ do {
+ bits = (rng.nextLong() << 1) >>> 1;
+ val = bits % n;
+ } while (bits-val+(n-1) < 0L);
+ return val;
}
+
- // Smooth Rounding Function (Nearest 5)
/**
- * Returns a double. The returned number is d rounded to the nearest d.5.
+ * Returns a percentage.
+ * The returned number is the % of X in Y.
* Supports Doubles.
*
- * @param current
- * Current value.
- * @return double Rounded value.
+ * @param current Current value.
+ * @param max Maximim value. Must be greater than min.
+ * @return double between min and max, inclusive.
*/
- public static double decimalRoundingToWholes(final double d) {
- return 5 * Math.round(d / 5);
+ public static double findPercentage(double current, double max){
+ double c = ((double) current / max) * 100;
+ double roundOff = Math.round(c * 100.00) / 100.00;
+ return roundOff;
}
+
+ //Smooth Rounding Function
/**
- * Returns a boolean. The returned boolean is wether or not X evenly fits in
- * to Y. Supports ints.
+ * Returns a double.
+ * The returned number is d rounded to the nearest d.01.
+ * Supports Doubles.
*
- * @param x
- * Value A.
- * @param y
- * Value B. Must be greater than min.
- * @return boolean Whether or not it divides evenly.
+ * @param current Current value.
+ * @return double Rounded value.
*/
- public static boolean divideXintoY(final int x, final int y) {
- if (x % y == 0) {
- return true;
- }
- return false;
+ public static double decimalRounding(double d) {
+ return Math.round(d * 2) / 2.0;
}
+
+ //Smooth Rounding Function (Nearest 5)
/**
- * Returns a percentage. The returned number is the % of X in Y. Supports
- * Doubles.
+ * Returns a double.
+ * The returned number is d rounded to the nearest d.5.
+ * Supports Doubles.
*
- * @param current
- * Current value.
- * @param max
- * Maximim value. Must be greater than min.
- * @return double between min and max, inclusive.
+ * @param current Current value.
+ * @return double Rounded value.
*/
- public static double findPercentage(final double current, final double max) {
- final double c = current / max * 100;
- final double roundOff = Math.round(c * 100.00) / 100.00;
- return roundOff;
- }
-
- private static long gcd(long a, long b) {
- while (b > 0) {
- final long temp = b;
- b = a % b; // % is remainder
- a = temp;
- }
- return a;
- }
-
- private static long gcd(final long[] input) {
- long result = input[0];
- for (int i = 1; i < input.length; i++) {
- result = MathUtils.gcd(result, input[i]);
- }
- return result;
+ public static double decimalRoundingToWholes(double d) {
+ return 5*(Math.round(d/5));
}
+
+ public static int roundToClosestMultiple(double number, int multiple) {
+ int result = multiple;
+ if (number % multiple == 0) {
+ return (int) number;
+ }
+ // If not already multiple of given number
+ if (number % multiple != 0) {
+ int division = (int) ((number / multiple) + 1);
+ result = division * multiple;
+ }
+ return result;
+ }
+
/**
- * Returns a hexInteger. The returned value is between min and max. Supports
- * ints.
+ * Returns a boolean.
+ * The returned boolean is wether or not X evenly fits in to Y.
+ * Supports ints.
*
- * @param min
- * Minimum value.
- * @param max
- * Maximium value. Must be greater than min.
- * @return hexInteger between min and max, inclusive.
+ * @param x Value A.
+ * @param y Value B. Must be greater than min.
+ * @return boolean Whether or not it divides evenly.
*/
- public static int generateRandomHexValue(final int min, final int max) {
- final int result = MathUtils.getHexNumberFromInt(MathUtils.randInt(min, max));
- return result;
+ public static boolean divideXintoY(int x, int y){
+ if ((x % y) == 0)
+ {
+ return true;
+ }
+ return false;
}
+
/**
- * Returns a random hex value. The returned value is between 000000-ffffff.
+ * Returns a boolean.
+ * The returned boolean is based on the odd/eveness of the input.
+ * Supports ints.
*
- * @return hexInteger between min and max, inclusive.
+ * @param x Value A.
+ * @return boolean Whether or not it divides evenly.
*/
- public static int generateSingularRandomHexValue() {
- String temp;
- final int randomInt = MathUtils.randInt(1, 5);
- final Map<Integer, String> colours = Utils.hexColourGeneratorRandom(5);
-
- if (colours.get(randomInt) != null && colours.size() > 0) {
- temp = colours.get(randomInt);
- }
- else {
- temp = "0F0F0F";
+ public static boolean isNumberEven(int x){
+ if ((x % 2) == 0)
+ {
+ return true;
}
-
- Utils.LOG_WARNING("Operating with " + temp);
- temp = Utils.appenedHexNotationToString(String.valueOf(temp));
- Utils.LOG_WARNING("Made " + temp + " - Hopefully it's not a mess.");
- Utils.LOG_WARNING("It will decode into " + Integer.decode(temp) + ".");
- return Integer.decode(temp);
+ return false;
}
- public static double getChanceOfXOverYRuns(final double x, final double y) {
- final double z = 1 - Math.pow(1 - x, y);
- return z;
- }
+
/**
- * Returns a hexInteger. The returned number is the hex value of the input.
+ * Returns an int.
+ * The returned number is the value on i + 273.15F.
* Supports ints.
*
- * @param input
- * Current value.
- * @return hexInteger.
+ * @param i Temp in Celcius.
+ * @return int The celcius temp returned as Kelvin, rounded to the readest whole.
*/
- public static int getHexNumberFromInt(final int input) {
- final String result = Integer.toHexString(input);
- final int resultINT = Integer.getInteger(result);
- return resultINT;
+ public static float celsiusToKelvin(int i){
+ double f = i + 273.15F;
+ return (int)decimalRoundingToWholes(f);
}
+
/**
- * Returns a boolean. The returned boolean is based on the odd/eveness of
- * the input. Supports ints.
+ * Returns a hexInteger.
+ * The returned number is the hex value of the input.
+ * Supports ints.
*
- * @param x
- * Value A.
- * @return boolean Whether or not it divides evenly.
+ * @param input Current value.
+ * @return hexInteger.
*/
- public static boolean isNumberEven(final int x) {
- if (x % 2 == 0) {
- return true;
- }
- return false;
- }
-
- private static long nextLong(final Random rng, final long n) {
- // error checking and 2^x checking removed for simplicity.
- long bits, val;
- do {
- bits = rng.nextLong() << 1 >>> 1;
- val = bits % n;
- }
- while (bits - val + n - 1 < 0L);
- return val;
+ public static int getHexNumberFromInt(int input){
+ String result = Integer.toHexString(input);
+ int resultINT = Integer.getInteger(result);
+ return resultINT;
}
+
/**
- * Returns a psuedo-random number between min and max, inclusive. The
- * difference between min and max can be at most Integer.MAX_VALUE - 1.
+ * Returns a hexInteger.
+ * The returned value is between min and max.
+ * Supports ints.
*
- * @param min
- * Minimim value
- * @param max
- * Maximim value. Must be greater than min.
- * @return Integer between min and max, inclusive.
- * @see java.util.Random#nextInt(int)
+ * @param min Minimum value.
+ * @param max Maximium value. Must be greater than min.
+ * @return hexInteger between min and max, inclusive.
*/
- public static int randInt(final int min, final int max) {
-
- // Usually this can be a field rather than a method variable
- final Random rand = new Random();
-
- // nextInt is normally exclusive of the top value,
- // so add 1 to make it inclusive
- final int randomNum = rand.nextInt(max - min + 1) + min;
-
- return randomNum;
+ public static int generateRandomHexValue(int min, int max){
+ int result = getHexNumberFromInt(randInt(min, max));
+ return result;
}
+
/**
- * Returns a psuedo-random number between min and max, inclusive. The
- * difference between min and max can be at most Long.MAX_VALUE - 1.
+ * Returns a random hex value.
+ * The returned value is between 000000-ffffff.
*
- * @param min
- * Minimim value
- * @param max
- * Maximim value. Must be greater than min.
- * @return Long between min and max, inclusive.
- * @see java.util.Random#nextLong(long)
+ * @return hexInteger between min and max, inclusive.
*/
- public static long randLong(final long min, final long max) {
- // Usually this can be a field rather than a method variable
- final Random rand = new Random();
-
- // nextInt is normally exclusive of the top value,
- // so add 1 to make it inclusive
- final long randomNum = MathUtils.nextLong(rand, max - min + 1) + min;
-
- return randomNum;
- }
+ public static int generateSingularRandomHexValue(){
+ String temp;
+ int randomInt = randInt(1, 5);
+ final Map<Integer, String> colours = Utils.hexColourGeneratorRandom(5);
- public static int roundToClosestMultiple(final double number, final int multiple) {
- int result = multiple;
- if (number % multiple == 0) {
- return (int) number;
+ if (colours.get(randomInt) != null && colours.size() > 0){
+ temp = colours.get(randomInt);
}
- // If not already multiple of given number
- if (number % multiple != 0) {
- final int division = (int) (number / multiple + 1);
- result = division * multiple;
+ else {
+ temp = "0F0F0F";
}
- return result;
- }
- public static long[] simplifyNumbersToSmallestForm(final long[] inputArray) {
- final long GCD = MathUtils.gcd(inputArray);
- final long[] outputArray = new long[inputArray.length];
- for (int i = 0; i < inputArray.length; i++) {
- if (GCD != 0) {
- outputArray[i] = inputArray[i] / GCD;
- }
- else {
+ Utils.LOG_WARNING("Operating with "+temp);
+ temp = Utils.appenedHexNotationToString(String.valueOf(temp));
+ Utils.LOG_WARNING("Made "+temp+" - Hopefully it's not a mess.");
+ Utils.LOG_WARNING("It will decode into "+Integer.decode(temp)+".");
+ return Integer.decode(temp);
+ }
+
+ public static long[] simplifyNumbersToSmallestForm(long[] inputArray){
+ long GCD = gcd(inputArray);
+ long[] outputArray = new long[inputArray.length];
+ for (int i=0;i<inputArray.length;i++){
+ if (GCD != 0)
+ outputArray[i] = (inputArray[i]/GCD);
+ else
outputArray[i] = inputArray[i];
- }
- }
- if (outputArray.length > 0) {
- return outputArray;
}
+ if (outputArray.length > 0)
+ return outputArray;
return null;
}
+
+ private static long gcd(long a, long b){
+ while (b > 0)
+ {
+ long temp = b;
+ b = a % b; // % is remainder
+ a = temp;
+ }
+ return a;
+ }
+
+ private static long gcd(long[] input){
+ long result = input[0];
+ for(int i = 1; i < input.length; i++) result = gcd(result, input[i]);
+ return result;
+ }
+
+
}