rebuild.util
Class MathUtilities

java.lang.Object
  extended by rebuild.util.MathUtilities

public final class MathUtilities
extends java.lang.Object

Provides some basic numeric operations.

Since:
BBX 1.0.0

Constructor Summary
MathUtilities()
           
 
Method Summary
static double acos(double x)
          Returns the arc cosine of the value x.
static double asin(double x)
          Returns the arc sine of the value x.
static double atan(double x)
          Returns the arc tangent of the value x.
static double atan2(double y, double x)
          Converts rectangular coordinates (x,y) to polar coordinates (r,theta).
static long bigMul(int a, int b)
          Produces the full product of two 32-bit numbers.
static double clamp(double low, double value, double high)
          Clamps provided double value between a lower and upper bound.
static float clamp(float low, float value, float high)
          Clamps provided float value between a lower and upper bound.
static int clamp(int low, int value, int high)
          Clamps provided integer value between a lower and upper bound.
static long clamp(long low, long value, long high)
          Clamps provided long integer value between a lower and upper bound.
static double cosh(double value)
          Returns the hyperbolic cosine of the specified angle.
static int divRem(int a, int b, RefLong result)
          Calculates the quotient of two 64-bit signed integers and also returns the remainder in an output parameter.
static int doubleToFP(double value)
          Convert a Double to a 16.16 fixed-point number.
static int doubleToFP(float value)
          Convert a Float to a 16.16 fixed-point number.
static double exp(double x)
          Return the exponential (base e) of x.
static double fpToDouble(int fp)
          Convert a 16.16 fixed-point number to a Double.
static float fpToFloat(int fp)
          Convert a 16.16 fixed-point number to a Float.
static boolean isNegativeInfinity(double d)
          Returns a value indicating whether the specified number evaluates to negative infinity.
static boolean isNegativeInfinity(float d)
          Returns a value indicating whether the specified number evaluates to negative infinity.
static boolean isPositiveInfinity(double d)
          Returns a value indicating whether the specified number evaluates to positive infinity.
static boolean isPositiveInfinity(float d)
          Returns a value indicating whether the specified number evaluates to positive infinity.
static double ldexp(double x, int exp)
          Returns the result of multiplying x (the significand) by 2 raised to the power of exp (the exponent).
static double log(double x)
          Return the natural logarithm (base e) of x.
static double log(double a, double newBase)
          Returns the logarithm of a specified number in a specified base.
static int log2(int value)
          Returns the log base 2 of the unsigned value rounded down.
static int log2(long value)
          Returns the log base 2 of the unsigned value rounded down.
static double nextDouble(double d)
          Finds the least double greater than d.
static double nextDouble(double d, boolean positive)
          Finds the least double greater than d (if positive == true), or the greatest double less than d (if positive == false).
static double pow(double x, double y)
          Return x raised to the power of y.
static double previousDouble(double d)
          Finds the greatest double less than d.
static long round(double a)
          Returns the closest long to the argument.
static int round(float a)
          Returns the closest int to the argument.
static int sign(byte value)
          Returns a value indicating the sign of a 8-bit signed integer.
static int sign(double value)
          Returns a value indicating the sign of a double-precision floating-point number.
static int sign(float value)
          Returns a value indicating the sign of a single-precision floating-point number.
static int sign(int value)
          Returns a value indicating the sign of a 32-bit signed integer.
static int sign(long value)
          Returns a value indicating the sign of a 64-bit signed integer.
static int sign(short value)
          Returns a value indicating the sign of a 16-bit signed integer.
static double sinh(double value)
          Returns the hyperbolic sine of the specified angle.
static double tanh(double value)
          Returns the hyperbolic tangent of the specified angle.
static double wrap(double low, double value, double high)
          Wraps provided double value around a lower and upper bound.
static float wrap(float low, float value, float high)
          Wraps provided float value around a lower and upper bound.
static int wrap(int low, int value, int high)
          Wraps provided integer value around a lower and upper bound.
static long wrap(long low, long value, long high)
          Wraps provided long integer value around a lower and upper bound.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MathUtilities

public MathUtilities()
Method Detail

acos

public static double acos(double x)
Returns the arc cosine of the value x.

Parameters:
x - the value.
Returns:
the arc cosine of the argument in radians.
Since:
BBX 1.0.0

asin

public static double asin(double x)
Returns the arc sine of the value x.

Parameters:
x - the value.
Returns:
the arc sine of the argument in radians.
Since:
BBX 1.0.0

atan

public static double atan(double x)
Returns the arc tangent of the value x.

Parameters:
x - the value.
Returns:
the arc tangent of the argument in radians.
Since:
BBX 1.0.0

atan2

public static double atan2(double y,
                           double x)
Converts rectangular coordinates (x,y) to polar coordinates (r,theta). This method computes the phase theta by computing the arc tangent of y/x in the range of -pi to pi.

Parameters:
y - the abscissa coordinate.
x - the ordinate coordinate.
Returns:
the theta component of the point (r,theta) in polar coordinates that corresponds to the point (x,y) in Cartesian coordinates.
Since:
BBX 1.0.0

clamp

public static int clamp(int low,
                        int value,
                        int high)
Clamps provided integer value between a lower and upper bound.

Parameters:
low - Lower bound value; must be less than your upper bound parameter.
value - Value to clamp.
high - Upper bound value.
Returns:
If the value is lower than the lower bound, this method returns the lower bound; if the value is higher than the higher bound, this method returns the higher bound; otherwise, this method returns the value itself.
Throws:
java.lang.IllegalArgumentException - If you provide a low parameter value that is greater than your high parameter value.
Since:
BBX 1.0.0

clamp

public static long clamp(long low,
                         long value,
                         long high)
Clamps provided long integer value between a lower and upper bound.

Parameters:
low - Lower bound value; must be less than your upper bound parameter.
value - Value to clamp.
high - Upper bound value.
Returns:
If the value is lower than the lower bound, this method returns the lower bound; if the value is higher than the higher bound, this method returns the higher bound; otherwise, this method returns the value itself.
Throws:
java.lang.IllegalArgumentException - If you provide a low parameter value that is greater than your high parameter value.
Since:
BBX 1.0.1

clamp

public static float clamp(float low,
                          float value,
                          float high)
Clamps provided float value between a lower and upper bound.

Parameters:
low - Lower bound value; must be less than your upper bound parameter.
value - Value to clamp.
high - Upper bound value.
Returns:
If the value is lower than the lower bound, this method returns the lower bound; if the value is higher than the higher bound, this method returns the higher bound; otherwise, this method returns the value itself.
Throws:
java.lang.IllegalArgumentException - If you provide a low parameter value that is greater than your high parameter value.
Since:
BBX 1.0.1

clamp

public static double clamp(double low,
                           double value,
                           double high)
Clamps provided double value between a lower and upper bound.

Parameters:
low - Lower bound value; must be less than your upper bound parameter.
value - Value to clamp.
high - Upper bound value.
Returns:
If the value is lower than the lower bound, this method returns the lower bound; if the value is higher than the higher bound, this method returns the higher bound; otherwise, this method returns the value itself.
Throws:
java.lang.IllegalArgumentException - If you provide a low parameter value that is greater than your high parameter value.
Since:
BBX 1.0.1

exp

public static double exp(double x)
Return the exponential (base e) of x. Interesting cases:

Parameters:
x - the power to raise e to.
Returns:
e raised to the power x.
Since:
BBX 1.0.0

ldexp

public static double ldexp(double x,
                           int exp)
Returns the result of multiplying x (the significand) by 2 raised to the power of exp (the exponent).

Parameters:
x - the significand.
exp - the exponent.
Returns:
x * 2exp.
Since:
BBX 1.0.0

log

public static double log(double x)
Return the natural logarithm (base e) of x. Interesting cases:

Parameters:
x - a number greater than zero.
Returns:
the natural logarithm (base e) of the argument.
Since:
BBX 1.0.0

log2

public static int log2(int value)
Returns the log base 2 of the unsigned value rounded down.

The value zero and one both return zero.

Parameters:
value - The unsigned value to calculate log2 on.
Returns:
The calculation of log2(value) (range: 0-31).
Since:
BBX 1.0.0

log2

public static int log2(long value)
Returns the log base 2 of the unsigned value rounded down.

The value zero and one both return a log2 result of zero.

Parameters:
value - The unsigned value to calculate log2 on.
Returns:
The calculation of log2(value) (range: 0-63).
Since:
BBX 1.0.0

pow

public static double pow(double x,
                         double y)
Return x raised to the power of y.

Parameters:
x - the base value.
y - the exponent.
Returns:
xy.
Since:
BBX 1.0.0

round

public static long round(double a)
Returns the closest long to the argument. The result is rounded to an integer by adding 1/2 if positive or subtracting 1/2 if negative, taking the floor of the result, and casting the result to type long.

Special cases:

Parameters:
a - a floating-point value to be rounded to a long.
Returns:
the value of the argument rounded to the nearest long value.
Since:
BBX 1.0.0
See Also:
Long.MAX_VALUE, Long.MIN_VALUE

round

public static int round(float a)
Returns the closest int to the argument. The result is rounded to an integer by adding 1/2 if positive or subtracting 1/2 if negative, taking the floor of the result, and casting the result to type int.

Special cases:

Parameters:
a - a floating-point value to be rounded to an integer.
Returns:
the value of the argument rounded to the nearest int value.
Since:
BBX 1.0.0
See Also:
Integer.MAX_VALUE, Integer.MIN_VALUE

wrap

public static int wrap(int low,
                       int value,
                       int high)
Wraps provided integer value around a lower and upper bound.

This method does the opposite of clamp(int, int, int).

Parameters:
low - Lower bound value; must be less than your upper bound parameter.
value - Value to wrap.
high - Upper bound value.
Returns:
If the value is lower than the lower bound, this method returns the higher bound; if the value is higher than the higher bound, this method returns the lower bound; otherwise, this method returns the value itself.
Throws:
java.lang.IllegalArgumentException - If you provide a low parameter value that is greater than your high parameter value.
Since:
BBX 1.0.0

wrap

public static long wrap(long low,
                        long value,
                        long high)
Wraps provided long integer value around a lower and upper bound.

This method does the opposite of clamp(long, long, long).

Parameters:
low - Lower bound value; must be less than your upper bound parameter.
value - Value to wrap.
high - Upper bound value.
Returns:
If the value is lower than the lower bound, this method returns the higher bound; if the value is higher than the higher bound, this method returns the lower bound; otherwise, this method returns the value itself.
Throws:
java.lang.IllegalArgumentException - If you provide a low parameter value that is greater than your high parameter value.
Since:
BBX 1.0.1

wrap

public static float wrap(float low,
                         float value,
                         float high)
Wraps provided float value around a lower and upper bound.

This method does the opposite of clamp(float, float, float).

Parameters:
low - Lower bound value; must be less than your upper bound parameter.
value - Value to wrap.
high - Upper bound value.
Returns:
If the value is lower than the lower bound, this method returns the higher bound; if the value is higher than the higher bound, this method returns the lower bound; otherwise, this method returns the value itself.
Throws:
java.lang.IllegalArgumentException - If you provide a low parameter value that is greater than your high parameter value.
Since:
BBX 1.0.1

wrap

public static double wrap(double low,
                          double value,
                          double high)
Wraps provided double value around a lower and upper bound.

This method does the opposite of clamp(double, double, double).

Parameters:
low - Lower bound value; must be less than your upper bound parameter.
value - Value to wrap.
high - Upper bound value.
Returns:
If the value is lower than the lower bound, this method returns the higher bound; if the value is higher than the higher bound, this method returns the lower bound; otherwise, this method returns the value itself.
Throws:
java.lang.IllegalArgumentException - If you provide a low parameter value that is greater than your high parameter value.
Since:
BBX 1.0.1

isNegativeInfinity

public static boolean isNegativeInfinity(float d)
Returns a value indicating whether the specified number evaluates to negative infinity.

Parameters:
d - A single-precision floating point number.
Returns:
true if d evaluates to Float.NEGATIVE_INFINITY; otherwise, false.
Since:
BBX 1.1.0

isPositiveInfinity

public static boolean isPositiveInfinity(float d)
Returns a value indicating whether the specified number evaluates to positive infinity.

Parameters:
d - A single-precision floating point number.
Returns:
true if d evaluates to Float.POSITIVE_INFINITY; otherwise, false.
Since:
BBX 1.1.0

isNegativeInfinity

public static boolean isNegativeInfinity(double d)
Returns a value indicating whether the specified number evaluates to negative infinity.

Parameters:
d - A double-precision floating point number.
Returns:
true if d evaluates to Double.NEGATIVE_INFINITY; otherwise, false.
Since:
BBX 1.1.0

isPositiveInfinity

public static boolean isPositiveInfinity(double d)
Returns a value indicating whether the specified number evaluates to positive infinity.

Parameters:
d - A double-precision floating point number.
Returns:
true if d evaluates to Double.POSITIVE_INFINITY; otherwise, false.
Since:
BBX 1.1.0

bigMul

public static long bigMul(int a,
                          int b)
Produces the full product of two 32-bit numbers.

Parameters:
a - The first int to multiply.
b - The second int to multiply.
Returns:
The long containing the product of the specified numbers.
Since:
BBX 1.1.0

divRem

public static int divRem(int a,
                         int b,
                         RefLong result)
Calculates the quotient of two 64-bit signed integers and also returns the remainder in an output parameter.

Parameters:
a - The long that contains the dividend.
b - The long that contains the divisor.
result - The RefLong that receives the remainder.
Returns:
The long containing the quotient of the specified numbers.
Throws:
java.lang.ArithmeticException - b is zero.
Since:
BBX 1.1.0

log

public static double log(double a,
                         double newBase)
Returns the logarithm of a specified number in a specified base.

Parameters:
a - A number whose logarithm is to be found.
newBase - The base of the logarithm.
Returns:
In the following table +Infinity denotes Double.POSITIVE_INFINITY, -Infinity denotes Double.NEGATIVE_INFINITY, and Double.NaN denotes Double.NaN.

a

newBase

Return Value

a> 0

(0 <newBase < 1) -or-(newBase> 1)

lognewBase(a)

\

a< 0

(any value)

NaN

(any value)

newBase< 0

NaN

a != 1

newBase = 0

NaN

a != 1

newBase = +Infinity

NaN

a = NaN

(any value)

NaN

(any value)

newBase = NaN

NaN

(any value)

newBase = 1

NaN

a = 0

0 <newBase< 1

+Infinity

a = 0

newBase> 1

-Infinity

a = +Infinity

0 <newBase< 1

-Infinity

a = +Infinity

newBase> 1

+Infinity

a = 1

newBase = 0

0

a = 1

newBase = +Infinity

0

Since:
BBX 1.1.0

sign

public static int sign(double value)
Returns a value indicating the sign of a double-precision floating-point number.

Parameters:
value - A signed number.
Returns:
A number indicating the sign of value.Number Description -1 value is less than zero. 0 value is equal to zero. 1 value is greater than zero.
Throws:
java.lang.ArithmeticException - value is equal to Double.NaN.
Since:
BBX 1.1.0

sign

public static int sign(byte value)
Returns a value indicating the sign of a 8-bit signed integer.

Parameters:
value - A signed number.
Returns:
A number indicating the sign of value.Number Description -1 value is less than zero. 0 value is equal to zero. 1 value is greater than zero.
Since:
BBX 1.1.0

sign

public static int sign(short value)
Returns a value indicating the sign of a 16-bit signed integer.

Parameters:
value - A signed number.
Returns:
A number indicating the sign of value.Number Description -1 value is less than zero. 0 value is equal to zero. 1 value is greater than zero.
Since:
BBX 1.1.0

sign

public static int sign(int value)
Returns a value indicating the sign of a 32-bit signed integer.

Parameters:
value - A signed number.
Returns:
A number indicating the sign of value.Number Description -1 value is less than zero. 0 value is equal to zero. 1 value is greater than zero.
Since:
BBX 1.1.0

sign

public static int sign(long value)
Returns a value indicating the sign of a 64-bit signed integer.

Parameters:
value - A signed number.
Returns:
A number indicating the sign of value.Number Description -1 value is less than zero. 0 value is equal to zero. 1 value is greater than zero.
Since:
BBX 1.1.0

sign

public static int sign(float value)
Returns a value indicating the sign of a single-precision floating-point number.

Parameters:
value - A signed number.
Returns:
A number indicating the sign of value.Number Description -1 value is less than zero. 0 value is equal to zero. 1 value is greater than zero.
Throws:
java.lang.ArithmeticException - value is equal to Float.NaN.
Since:
BBX 1.1.0

sinh

public static double sinh(double value)
Returns the hyperbolic sine of the specified angle.

Parameters:
value - An angle, measured in radians.
Returns:
The hyperbolic sine of value. If value is equal to Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, or Double.NaN, this method returns a Double equal to value.
Since:
BBX 1.1.0

cosh

public static double cosh(double value)
Returns the hyperbolic cosine of the specified angle.

Parameters:
value - An angle, measured in radians.
Returns:
The hyperbolic cosine of value.
Since:
BBX 1.1.0

tanh

public static double tanh(double value)
Returns the hyperbolic tangent of the specified angle.

Parameters:
value - An angle, measured in radians.
Returns:
The hyperbolic tangent of value. If value is equal to Double.NEGATIVE_INFINITY, this method returns -1. If value is equal to Double.POSITIVE_INFINITY, this method returns 1. If value is equal to Double.NaN, this method returns Double.NaN.
Since:
BBX 1.1.0

nextDouble

public static double nextDouble(double d)
Finds the least double greater than d.

Parameters:
d - The double to get the next positive value of.
Returns:
The next positive double.
Since:
BBX 1.1.0

previousDouble

public static double previousDouble(double d)
Finds the greatest double less than d.

Parameters:
d - The double to get the next negative value of.
Returns:
The next negative double.
Since:
BBX 1.1.0

nextDouble

public static double nextDouble(double d,
                                boolean positive)
Finds the least double greater than d (if positive == true), or the greatest double less than d (if positive == false).

Parameters:
d - The number to get the next double of.
positive - Should the next value be the next positive value (adding) or negative value (subtraction).
Returns:
The next double.
Since:
BBX 1.1.0

fpToFloat

public static float fpToFloat(int fp)
Convert a 16.16 fixed-point number to a Float. Certain predefined values are returned with calculation.

Parameters:
fp - A 16.16 fixed-point number.
Returns:
A Float that represents a fixed-point number.
Since:
BBX 1.1.0

fpToDouble

public static double fpToDouble(int fp)
Convert a 16.16 fixed-point number to a Double. Certain predefined values are returned with calculation.

Parameters:
fp - A 16.16 fixed-point number.
Returns:
A Double that represents a fixed-point number.
Since:
BBX 1.1.0

doubleToFP

public static int doubleToFP(float value)
Convert a Float to a 16.16 fixed-point number. Certain predefined values are returned with calculation.

Parameters:
value - A Float number.
Returns:
A 16.16 fixed-point number that represents a Float.
Since:
BBX 1.1.0

doubleToFP

public static int doubleToFP(double value)
Convert a Double to a 16.16 fixed-point number. Certain predefined values are returned with calculation.

Parameters:
value - A Double number.
Returns:
A 16.16 fixed-point number that represents a Double.
Since:
BBX 1.1.0