## toxi.math Class MathUtils

```java.lang.Object
toxi.math.MathUtils
```

`public final class MathUtilsextends java.lang.Object`

Miscellaneous math utilities.

Field Summary
`static float` `DEG2RAD`
`static float` `EPS`
Epsilon value
`static float` `HALF_PI`
PI/2
`static float` `INV_PI`
The reciprocal of PI: (1/PI)
`static float` `LOG2`
Log(2)
`static float` `PI`
PI
`static float` `PI_SQUARED`
PI*PI
`static float` `QUARTER_PI`
PI/4
`static float` `RAD2DEG`
`static java.util.Random` `RND`
Default random number generator used by random methods of this class which don't use a passed in `Random` instance.
`static float` `SQRT2`
Square root of 2
`static float` `SQRT3`
Square root of 3
`static float` `THIRD_PI`
PI/3
`static float` `THREE_HALVES_PI`
PI*1.5
`static float` `TWO_PI`
PI*2

Constructor Summary
`MathUtils()`

Method Summary
`static double` `abs(double x)`

`static float` `abs(float x)`

`static int` `abs(int x)`

`static int` `ceilPowerOf2(int x)`
Rounds up the value to the nearest higher power^2 value.
`static double` ```clip(double a, double min, double max)```

`static float` ```clip(float a, float min, float max)```

`static int` ```clip(int a, int min, int max)```

`static float` `clipNormalized(float a)`
Clips the value to the 0.0 ..
`static double` `cos(double theta)`

`static float` `cos(float theta)`
Returns fast cosine approximation of a value.
`static float` `degrees(float radians)`

`static double` `fastCos(double x)`
Fast cosine approximation.
`static int` `fastFloor(float x)`
Deprecated. renamed into `floor(float)`
`static float` `fastInverseSqrt(float x)`
Deprecated.
`static float` ```fastPow(float a, float b)```
Computes a fast approximation to `Math.pow(a, b)`.
`static double` `fastSin(double x)`
Fast sine approximation.
`static boolean` `flipCoin()`

`static boolean` `flipCoin(java.util.Random rnd)`

`static int` `floor(double x)`

`static int` `floor(float x)`
This method is a *lot* faster than using (int)Math.floor(x).
`static int` `floorPowerOf2(int x)`
Rounds down the value to the nearest lower power^2 value.
`static double` ```max(double a, double b)```

`static double` ```max(double a, double b, double c)```

`static float` ```max(float a, float b)```

`static float` ```max(float a, float b, float c)```
Returns the maximum value of three floats.
`static int` ```max(int a, int b)```

`static int` ```max(int a, int b, int c)```
Returns the maximum value of three ints.
`static double` ```min(double a, double b)```

`static double` ```min(double a, double b, double c)```

`static float` ```min(float a, float b)```

`static float` ```min(float a, float b, float c)```
Returns the minimum value of three floats.
`static int` ```min(int a, int b)```

`static int` ```min(int a, int b, int c)```
Returns the minimum value of three ints.
`static float` `normalizedRandom()`
Returns a random number in the interval -1 ..
`static float` `normalizedRandom(java.util.Random rnd)`
Returns a random number in the interval -1 ..
`static float` `radians(float degrees)`

`static float` `random(float max)`

`static float` ```random(float min, float max)```

`static int` `random(int max)`

`static int` ```random(int min, int max)```

`static double` ```random(java.util.Random rnd, double max)```

`static double` ```random(java.util.Random rnd, double min, double max)```

`static float` ```random(java.util.Random rnd, float max)```

`static float` ```random(java.util.Random rnd, float min, float max)```

`static int` ```random(java.util.Random rnd, int max)```

`static int` ```random(java.util.Random rnd, int min, int max)```

`static double` `reduceAngle(double theta)`

`static float` `reduceAngle(float theta)`
Reduces the given angle into the -PI/4 ...
`static void` `setDefaultRandomGenerator(java.util.Random rnd)`
Sets the default Random number generator for this class.
`static int` `sign(double x)`

`static int` `sign(float x)`

`static int` `sign(int x)`

`static double` `sin(double theta)`

`static float` `sin(float theta)`
Returns a fast sine approximation of a value.
`static float` `sqrt(float x)`
Deprecated.

Methods inherited from class java.lang.Object
`equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### SQRT2

`public static final float SQRT2`
Square root of 2

### SQRT3

`public static final float SQRT3`
Square root of 3

### LOG2

`public static final float LOG2`
Log(2)

### PI

`public static final float PI`
PI

Constant Field Values

### INV_PI

`public static final float INV_PI`
The reciprocal of PI: (1/PI)

Constant Field Values

### HALF_PI

`public static final float HALF_PI`
PI/2

Constant Field Values

### THIRD_PI

`public static final float THIRD_PI`
PI/3

Constant Field Values

### QUARTER_PI

`public static final float QUARTER_PI`
PI/4

Constant Field Values

### TWO_PI

`public static final float TWO_PI`
PI*2

Constant Field Values

### THREE_HALVES_PI

`public static final float THREE_HALVES_PI`
PI*1.5

Constant Field Values

### PI_SQUARED

`public static final float PI_SQUARED`
PI*PI

Constant Field Values

### EPS

`public static final float EPS`
Epsilon value

Constant Field Values

`public static final float DEG2RAD`

Constant Field Values

`public static final float RAD2DEG`

Constant Field Values

### RND

`public static java.util.Random RND`
Default random number generator used by random methods of this class which don't use a passed in `Random` instance.

Constructor Detail

### MathUtils

`public MathUtils()`
Method Detail

### abs

`public static final double abs(double x)`
Parameters:
`x` -
Returns:
absolute value of x

### abs

`public static final float abs(float x)`
Parameters:
`x` -
Returns:
absolute value of x

### abs

`public static final int abs(int x)`
Parameters:
`x` -
Returns:
absolute value of x

### ceilPowerOf2

`public static final int ceilPowerOf2(int x)`
Rounds up the value to the nearest higher power^2 value.

Parameters:
`x` -
Returns:
power^2 value

### clip

```public static final double clip(double a,
double min,
double max)```

### clip

```public static final float clip(float a,
float min,
float max)```

### clip

```public static final int clip(int a,
int min,
int max)```

### clipNormalized

`public static final float clipNormalized(float a)`
Clips the value to the 0.0 .. 1.0 interval.

Parameters:
`a` -
Returns:
clipped value
Since:
0012

### cos

`public static final double cos(double theta)`

### cos

`public static final float cos(float theta)`
Returns fast cosine approximation of a value. Note: code from wiki posting on java.net by jeffpk

Parameters:
`theta` - angle in radians.
Returns:
cosine of theta.

### degrees

`public static final float degrees(float radians)`

### fastCos

`public static final double fastCos(double x)`
Fast cosine approximation.

Parameters:
`x` - angle in -PI/2 .. +PI/2 interval
Returns:
cosine

### fastFloor

```@Deprecated
public static final int fastFloor(float x)```
Deprecated. renamed into `floor(float)`

### fastInverseSqrt

```@Deprecated
public static final float fastInverseSqrt(float x)```
Deprecated.

### fastPow

```public static final float fastPow(float a,
float b)```
Computes a fast approximation to `Math.pow(a, b)`. Adapted from http://www.dctsystems.co.uk/Software/power.html.

Parameters:
`a` - a positive number
`b` - a number
Returns:
a^b

### fastSin

`public static final double fastSin(double x)`
Fast sine approximation.

Parameters:
`x` - angle in -PI/2 .. +PI/2 interval
Returns:
sine

### flipCoin

`public static final boolean flipCoin()`

### flipCoin

`public static final boolean flipCoin(java.util.Random rnd)`

### floor

`public static final int floor(double x)`

### floor

`public static final int floor(float x)`
This method is a *lot* faster than using (int)Math.floor(x).

Parameters:
`x` - value to be floored
Returns:
floored value as integer
Since:
0012

### floorPowerOf2

`public static final int floorPowerOf2(int x)`
Rounds down the value to the nearest lower power^2 value.

Parameters:
`x` -
Returns:
power^2 value

### max

```public static final double max(double a,
double b)```

### max

```public static final double max(double a,
double b,
double c)```

### max

```public static final float max(float a,
float b)```

### max

```public static final float max(float a,
float b,
float c)```
Returns the maximum value of three floats.

Parameters:
`a` -
`b` -
`c` -
Returns:
max val

### max

```public static final int max(int a,
int b)```

### max

```public static final int max(int a,
int b,
int c)```
Returns the maximum value of three ints.

Parameters:
`a` -
`b` -
`c` -
Returns:
max val

### min

```public static final double min(double a,
double b)```

### min

```public static final double min(double a,
double b,
double c)```

### min

```public static final float min(float a,
float b)```

### min

```public static final float min(float a,
float b,
float c)```
Returns the minimum value of three floats.

Parameters:
`a` -
`b` -
`c` -
Returns:
min val

### min

```public static final int min(int a,
int b)```

### min

```public static final int min(int a,
int b,
int c)```
Returns the minimum value of three ints.

Parameters:
`a` -
`b` -
`c` -
Returns:
min val

### normalizedRandom

`public static final float normalizedRandom()`
Returns a random number in the interval -1 .. +1.

Returns:
random float

### normalizedRandom

`public static final float normalizedRandom(java.util.Random rnd)`
Returns a random number in the interval -1 .. +1 using the `Random` instance provided.

Returns:
random float

`public static final float radians(float degrees)`

### random

`public static final float random(float max)`

### random

```public static final float random(float min,
float max)```

### random

`public static final int random(int max)`

### random

```public static final int random(int min,
int max)```

### random

```public static final double random(java.util.Random rnd,
double max)```

### random

```public static final double random(java.util.Random rnd,
double min,
double max)```

### random

```public static final float random(java.util.Random rnd,
float max)```

### random

```public static final float random(java.util.Random rnd,
float min,
float max)```

### random

```public static final int random(java.util.Random rnd,
int max)```

### random

```public static final int random(java.util.Random rnd,
int min,
int max)```

### reduceAngle

`public static final double reduceAngle(double theta)`

### reduceAngle

`public static final float reduceAngle(float theta)`
Reduces the given angle into the -PI/4 ... PI/4 interval for faster computation of sin/cos. This method is used by `sin(float)` & `cos(float)`.

Parameters:
`theta` - angle in radians
Returns:
reduced angle
`sin(float)`, `cos(float)`

### setDefaultRandomGenerator

`public static void setDefaultRandomGenerator(java.util.Random rnd)`
Sets the default Random number generator for this class. This generator is being reused by all future calls to random() method versions which don't explicitly ask for a `Random` instance to be used.

Parameters:
`rnd` -

### sign

`public static int sign(double x)`

### sign

`public static int sign(float x)`

### sign

`public static int sign(int x)`

### sin

`public static final double sin(double theta)`

### sin

`public static final float sin(float theta)`
Returns a fast sine approximation of a value. Note: code from wiki posting on java.net by jeffpk

Parameters:
`theta` - angle in radians.
Returns:
sine of theta.

### sqrt

```@Deprecated
public static final float sqrt(float x)```
Deprecated.