Package ch.njol.util
Class Math2
java.lang.Object
ch.njol.util.Math2
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic longceil(double d)Ceils the given double and returns the result as a long.static longceil(float f)static intceilI(double d)static intceilI(float f)static doublefit(double min, double x, double max)Fits a number into the given interval.static floatfit(float min, float x, float max)Fits a number into the given interval.static intfit(int min, int x, int max)Fits a number into the given interval.static longfit(long min, long x, long max)Fits a number into the given interval.static shortfit(short min, short x, short max)Fits a number into the given interval.static longfloor(double d)Floors the given double and returns the result as a long.static longfloor(float f)static intfloorI(double d)static intfloorI(float f)static doublefrac(double d)static floatfrac(float f)static doublemax(double... nums)static doublemax(double a, double b, double c)static intmax(int... nums)static intmax(int a, int b, int c)static doublemin(double... nums)static doublemin(double a, double b, double c)static intmin(int... nums)static intmin(int a, int b, int c)static intminPositive(int... nums)finds the smallest positive number (≥0) in the sequencestatic doublemod(double d, double m)Modulo that returns positive values even for negative arguments.static floatmod(float d, float m)Modulo that returns positive values even for negative arguments.static intmod(int d, int m)Modulo that returns positive values even for negative arguments.static longmod(long d, long m)Modulo that returns positive values even for negative arguments.static intnextPowerOfTwo(int n)Gets the smallest power of two ≥n.static longnextPowerOfTwo(long n)Gets the smallest power of two ≥n.static longround(double d)Rounds the given double (where .5 is rounded up) and returns the result as a long.static longround(float f)Rounds the given float (where .5 is rounded up) and returns the result as a long.static introundI(double d)static introundI(float f)Rounds the given float (where .5 is rounded up) and returns the result as an int.static doublesafe(double d)Guarantees a double is neither NaN nor INF.static floatsafe(float f)Guarantees a float is neither NaN nor INF.static intsign(byte i)static intsign(double d)static intsign(float f)static intsign(int i)static intsign(long i)static intsign(short i)static doublesmoothStep(double x, double x1, double x2)Performs a hermite interpolation between the given values, or returns 0 or 1 respectively if the value is out of range.
-
Constructor Details
-
Math2
public Math2()
-
-
Method Details
-
min
public static int min(int a, int b, int c) -
min
public static int min(int... nums) -
max
public static int max(int a, int b, int c) -
max
public static int max(int... nums) -
min
public static double min(double a, double b, double c) -
min
public static double min(double... nums) -
max
public static double max(double a, double b, double c) -
max
public static double max(double... nums) -
minPositive
public static int minPositive(int... nums)finds the smallest positive number (≥0) in the sequence- Parameters:
nums-- Returns:
- smallest positive number in the sequence or -1 if no number is positive
-
fit
public static int fit(int min, int x, int max)Fits a number into the given interval. The method's behaviour when min > max is unspecified.- Returns:
- x <= min ? min : x >= max ? max : x
-
fit
public static short fit(short min, short x, short max)Fits a number into the given interval. The method's behaviour when min > max is unspecified.- Returns:
- x <= min ? min : x >= max ? max : x
-
fit
public static long fit(long min, long x, long max)Fits a number into the given interval. The method's behaviour when min > max is unspecified.- Returns:
- x <= min ? min : x >= max ? max : x
-
fit
public static float fit(float min, float x, float max)Fits a number into the given interval. The method's behaviour when min > max is unspecified.- Returns:
- x <= min ? min : x >= max ? max : x
-
fit
public static double fit(double min, double x, double max)Fits a number into the given interval. The method's behaviour when min > max is unspecified.- Returns:
- x <= min ? min : x >= max ? max : x
-
mod
public static double mod(double d, double m)Modulo that returns positive values even for negative arguments.- Parameters:
d-m-- Returns:
- d%m < 0 ? d%m + m : d%m
-
mod
public static float mod(float d, float m)Modulo that returns positive values even for negative arguments.- Parameters:
d-m-- Returns:
- d%m < 0 ? d%m + m : d%m
-
mod
public static int mod(int d, int m)Modulo that returns positive values even for negative arguments.- Parameters:
d-m-- Returns:
- d%m < 0 ? d%m + m : d%m
-
mod
public static long mod(long d, long m)Modulo that returns positive values even for negative arguments.- Parameters:
d-m-- Returns:
- d%m < 0 ? d%m + m : d%m
-
floor
public static long floor(double d)Floors the given double and returns the result as a long.This method can be up to 20 times faster than the default
Math.floor(double)(both with and without casting to long). -
ceil
public static long ceil(double d)Ceils the given double and returns the result as a long.This method can be up to 20 times faster than the default
Math.ceil(double)(both with and without casting to long). -
round
public static long round(double d)Rounds the given double (where .5 is rounded up) and returns the result as a long.This method is more exact and faster than
Math.round(double)of Java 7 and older. -
floorI
public static int floorI(double d) -
ceilI
public static int ceilI(double d) -
roundI
public static int roundI(double d) -
floor
public static long floor(float f) -
ceil
public static long ceil(float f) -
round
public static long round(float f)Rounds the given float (where .5 is rounded up) and returns the result as a long.This method is more exact and faster than
Math.round(float)of Java 7 and older. -
floorI
public static int floorI(float f) -
ceilI
public static int ceilI(float f) -
roundI
public static int roundI(float f)Rounds the given float (where .5 is rounded up) and returns the result as an int.This method is more exact and faster than
Math.round(float)of Java 7 and older. -
nextPowerOfTwo
public static int nextPowerOfTwo(int n)Gets the smallest power of two ≥n. ReturnsInteger.MIN_VALUEif n > 230. -
nextPowerOfTwo
public static long nextPowerOfTwo(long n)Gets the smallest power of two ≥n. ReturnsLong.MIN_VALUEif n > 262. -
frac
public static double frac(double d)- Returns:
- The floating point part of d in the range [0, 1)
-
frac
public static float frac(float f)- Returns:
- The floating point part of f in the range [0, 1)
-
sign
public static int sign(byte i)- Returns:
- -1 if i is negative, 0 if i is 0, or 1 if i is positive
-
sign
public static int sign(short i)- Returns:
- -1 if i is negative, 0 if i is 0, or 1 if i is positive
-
sign
public static int sign(int i)- Returns:
- -1 if i is negative, 0 if i is 0, or 1 if i is positive
-
sign
public static int sign(long i)- Returns:
- -1 if i is negative, 0 if i is 0, or 1 if i is positive
-
sign
public static int sign(float f)- Returns:
- -1 if f is negative, 0 if f is +0, -0 or NaN, or 1 if f is positive
-
sign
public static int sign(double d)- Returns:
- -1 if d is negative, 0 if d is +0, -0 or NaN, or 1 if d is positive
-
smoothStep
public static double smoothStep(double x, double x1, double x2)Performs a hermite interpolation between the given values, or returns 0 or 1 respectively if the value is out of range.Specifically this method returns d * d * (3 - 2 * d), where d =
fit(0, (x - x1) / (x2 - x1), 1). This is very similar to 0.5 - 0.5 * cos(PI * d).This function is essentially equal to GLSL's smoothstep, but with a different argument order.
- Parameters:
x- The value to get the step atx1- The lower end of the stepx2- The upper end of the step- Returns:
- The step's value at x
-
safe
public static float safe(float f)Guarantees a float is neither NaN nor INF. Useful for situations when safe floats are required.- Parameters:
f-- Returns:
- 0 if f is NaN or INF, otherwise f
-
safe
public static double safe(double d)Guarantees a double is neither NaN nor INF. Useful for situations when safe doubles are required.- Parameters:
d-- Returns:
- 0 if d is NaN or INF, otherwise d
-