libgdx API

## Class MathUtils

• java.lang.Object

• ```public final class MathUtils
extends java.lang.Object```
Utility and fast math functions.

Thanks to Riven on JavaGaming.org for the basis of sin/cos/floor/ceil.

Author:
Nathan Sweet
• ### Field Summary

Fields
Modifier and Type Field and Description
`static float` `degRad`
`static float` `degreesToRadians`
multiply by this to convert from degrees to radians
`static float` `E`
`static float` `FLOAT_ROUNDING_ERROR`
`static float` `HALF_PI`
`static float` `nanoToSec`
`static float` `PI`
`static float` `PI2`
`static float` `radDeg`
`static float` `radiansToDegrees`
multiply by this to convert from radians to degrees
`static java.util.Random` `random`
• ### Method Summary

All Methods
Modifier and Type Method and Description
`static float` `acos(float a)`
Returns acos in radians; less accurate than Math.acos but may be faster.
`static float` `asin(float a)`
Returns asin in radians; less accurate than Math.asin but may be faster.
`static float` ```atan2(float y, float x)```
Returns atan2 in radians, less accurate than Math.atan2 but may be faster.
`static int` `ceil(float value)`
Returns the smallest integer greater than or equal to the specified float.
`static int` `ceilPositive(float value)`
Returns the smallest integer greater than or equal to the specified float.
`static double` ```clamp(double value, double min, double max)```
`static float` ```clamp(float value, float min, float max)```
`static int` ```clamp(int value, int min, int max)```
`static long` ```clamp(long value, long min, long max)```
`static short` ```clamp(short value, short min, short max)```
`static float` `cos(float radians)`
Returns the cosine in radians from a lookup table.
`static float` `cosDeg(float degrees)`
Returns the cosine in degrees from a lookup table.
`static int` `floor(float value)`
Returns the largest integer less than or equal to the specified float.
`static int` `floorPositive(float value)`
Returns the largest integer less than or equal to the specified float.
`static boolean` ```isEqual(float a, float b)```
Returns true if a is nearly equal to b.
`static boolean` ```isEqual(float a, float b, float tolerance)```
Returns true if a is nearly equal to b.
`static boolean` `isPowerOfTwo(int value)`
`static boolean` `isZero(float value)`
Returns true if the value is zero (using the default tolerance as upper bound)
`static boolean` ```isZero(float value, float tolerance)```
Returns true if the value is zero.
`static float` ```lerp(float fromValue, float toValue, float progress)```
Linearly interpolates between fromValue to toValue on progress position.
`static float` ```lerpAngle(float fromRadians, float toRadians, float progress)```
Linearly interpolates between two angles in radians.
`static float` ```lerpAngleDeg(float fromDegrees, float toDegrees, float progress)```
Linearly interpolates between two angles in degrees.
`static float` ```log(float a, float value)```
`static float` `log2(float value)`
`static float` ```map(float inRangeStart, float inRangeEnd, float outRangeStart, float outRangeEnd, float value)```
Linearly map a value from one range to another.
`static int` `nextPowerOfTwo(int value)`
Returns the next power of two.
`static float` ```norm(float rangeStart, float rangeEnd, float value)```
Linearly normalizes value from a range.
`static float` `random()`
Returns random number between 0.0 (inclusive) and 1.0 (exclusive).
`static float` `random(float range)`
Returns a random number between 0 (inclusive) and the specified value (exclusive).
`static float` ```random(float start, float end)```
Returns a random number between start (inclusive) and end (exclusive).
`static int` `random(int range)`
Returns a random number between 0 (inclusive) and the specified value (inclusive).
`static int` ```random(int start, int end)```
Returns a random number between start (inclusive) and end (inclusive).
`static long` `random(long range)`
Returns a random number between 0 (inclusive) and the specified value (inclusive).
`static long` ```random(long start, long end)```
Returns a random number between start (inclusive) and end (inclusive).
`static boolean` `randomBoolean()`
Returns a random boolean value.
`static boolean` `randomBoolean(float chance)`
Returns true if a random value between 0 and 1 is less than the specified value.
`static int` `randomSign()`
Returns -1 or 1, randomly.
`static float` `randomTriangular()`
Returns a triangularly distributed random number between -1.0 (exclusive) and 1.0 (exclusive), where values around zero are more likely.
`static float` `randomTriangular(float max)`
Returns a triangularly distributed random number between `-max` (exclusive) and `max` (exclusive), where values around zero are more likely.
`static float` ```randomTriangular(float min, float max)```
Returns a triangularly distributed random number between `min` (inclusive) and `max` (exclusive), where the `mode` argument defaults to the midpoint between the bounds, giving a symmetric distribution.
`static float` ```randomTriangular(float min, float max, float mode)```
Returns a triangularly distributed random number between `min` (inclusive) and `max` (exclusive), where values around `mode` are more likely.
`static int` `round(float value)`
Returns the closest integer to the specified float.
`static int` `roundPositive(float value)`
Returns the closest integer to the specified float.
`static float` `sin(float radians)`
Returns the sine in radians from a lookup table.
`static float` `sinDeg(float degrees)`
Returns the sine in degrees from a lookup table.
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Field Detail

• #### nanoToSec

`public static final float nanoToSec`
Constant Field Values
• #### FLOAT_ROUNDING_ERROR

`public static final float FLOAT_ROUNDING_ERROR`
Constant Field Values
• #### HALF_PI

`public static final float HALF_PI`
Constant Field Values

`public static final float radiansToDegrees`
multiply by this to convert from radians to degrees
Constant Field Values

`public static final float radDeg`
Constant Field Values

`public static final float degreesToRadians`
multiply by this to convert from degrees to radians
Constant Field Values

`public static final float degRad`
Constant Field Values
• #### random

`public static java.util.Random random`
• ### Method Detail

• #### sin

`public static float sin(float radians)`
Returns the sine in radians from a lookup table. For optimal precision, use radians between -PI2 and PI2 (both inclusive).
• #### cos

`public static float cos(float radians)`
Returns the cosine in radians from a lookup table. For optimal precision, use radians between -PI2 and PI2 (both inclusive).
• #### sinDeg

`public static float sinDeg(float degrees)`
Returns the sine in degrees from a lookup table. For optimal precision, use radians between -360 and 360 (both inclusive).
• #### cosDeg

`public static float cosDeg(float degrees)`
Returns the cosine in degrees from a lookup table. For optimal precision, use radians between -360 and 360 (both inclusive).
• #### atan2

```public static float atan2(float y,
float x)```
Returns atan2 in radians, less accurate than Math.atan2 but may be faster. Average error of 0.00231 radians (0.1323 degrees), largest error of 0.00488 radians (0.2796 degrees).
• #### acos

`public static float acos(float a)`
Returns acos in radians; less accurate than Math.acos but may be faster. Average error of 0.00002845 radians (0.0016300649 degrees), largest error of 0.000067548 radians (0.0038702153 degrees). This implementation does not return NaN if given an out-of-range input (Math.acos does return NaN), unless the input is NaN.
Parameters:
`a` - acos is defined only when a is between -1f and 1f, inclusive
Returns:
between `0` and `PI` when a is in the defined range
• #### asin

`public static float asin(float a)`
Returns asin in radians; less accurate than Math.asin but may be faster. Average error of 0.000028447 radians (0.0016298931 degrees), largest error of 0.000067592 radians (0.0038727364 degrees). This implementation does not return NaN if given an out-of-range input (Math.asin does return NaN), unless the input is NaN.
Parameters:
`a` - asin is defined only when a is between -1f and 1f, inclusive
Returns:
between `-HALF_PI` and `HALF_PI` when a is in the defined range
• #### random

`public static int random(int range)`
Returns a random number between 0 (inclusive) and the specified value (inclusive).
• #### random

```public static int random(int start,
int end)```
Returns a random number between start (inclusive) and end (inclusive).
• #### random

`public static long random(long range)`
Returns a random number between 0 (inclusive) and the specified value (inclusive).
• #### random

```public static long random(long start,
long end)```
Returns a random number between start (inclusive) and end (inclusive).
• #### randomBoolean

`public static boolean randomBoolean()`
Returns a random boolean value.
• #### randomBoolean

`public static boolean randomBoolean(float chance)`
Returns true if a random value between 0 and 1 is less than the specified value.
• #### random

`public static float random()`
Returns random number between 0.0 (inclusive) and 1.0 (exclusive).
• #### random

`public static float random(float range)`
Returns a random number between 0 (inclusive) and the specified value (exclusive).
• #### random

```public static float random(float start,
float end)```
Returns a random number between start (inclusive) and end (exclusive).
• #### randomSign

`public static int randomSign()`
Returns -1 or 1, randomly.
• #### randomTriangular

`public static float randomTriangular()`
Returns a triangularly distributed random number between -1.0 (exclusive) and 1.0 (exclusive), where values around zero are more likely.

This is an optimized version of `randomTriangular(-1, 1, 0)`

• #### randomTriangular

`public static float randomTriangular(float max)`
Returns a triangularly distributed random number between `-max` (exclusive) and `max` (exclusive), where values around zero are more likely.

This is an optimized version of `randomTriangular(-max, max, 0)`

Parameters:
`max` - the upper limit
• #### randomTriangular

```public static float randomTriangular(float min,
float max)```
Returns a triangularly distributed random number between `min` (inclusive) and `max` (exclusive), where the `mode` argument defaults to the midpoint between the bounds, giving a symmetric distribution.

This method is equivalent of `randomTriangular(min, max, (min + max) * 0.5f)`

Parameters:
`min` - the lower limit
`max` - the upper limit
• #### randomTriangular

```public static float randomTriangular(float min,
float max,
float mode)```
Returns a triangularly distributed random number between `min` (inclusive) and `max` (exclusive), where values around `mode` are more likely.
Parameters:
`min` - the lower limit
`max` - the upper limit
`mode` - the point around which the values are more likely
• #### nextPowerOfTwo

`public static int nextPowerOfTwo(int value)`
Returns the next power of two. Returns the specified value if the value is already a power of two.
• #### isPowerOfTwo

`public static boolean isPowerOfTwo(int value)`
• #### clamp

```public static short clamp(short value,
short min,
short max)```
• #### clamp

```public static int clamp(int value,
int min,
int max)```
• #### clamp

```public static long clamp(long value,
long min,
long max)```
• #### clamp

```public static float clamp(float value,
float min,
float max)```
• #### clamp

```public static double clamp(double value,
double min,
double max)```
• #### lerp

```public static float lerp(float fromValue,
float toValue,
float progress)```
Linearly interpolates between fromValue to toValue on progress position.
• #### norm

```public static float norm(float rangeStart,
float rangeEnd,
float value)```
Linearly normalizes value from a range. Range must not be empty. This is the inverse of `lerp(float, float, float)`.
Parameters:
`rangeStart` - Range start normalized to 0
`rangeEnd` - Range end normalized to 1
`value` - Value to normalize
Returns:
Normalized value. Values outside of the range are not clamped to 0 and 1
• #### map

```public static float map(float inRangeStart,
float inRangeEnd,
float outRangeStart,
float outRangeEnd,
float value)```
Linearly map a value from one range to another. Input range must not be empty. This is the same as chaining `norm(float, float, float)` from input range and `lerp(float, float, float)` to output range.
Parameters:
`inRangeStart` - Input range start
`inRangeEnd` - Input range end
`outRangeStart` - Output range start
`outRangeEnd` - Output range end
`value` - Value to map
Returns:
Mapped value. Values outside of the input range are not clamped to output range
• #### lerpAngle

```public static float lerpAngle(float fromRadians,
float progress)```
Linearly interpolates between two angles in radians. Takes into account that angles wrap at two pi and always takes the direction with the smallest delta angle.
Parameters:
`fromRadians` - start angle in radians
`toRadians` - target angle in radians
`progress` - interpolation value in the range [0, 1]
Returns:
the interpolated angle in the range [0, PI2[
• #### lerpAngleDeg

```public static float lerpAngleDeg(float fromDegrees,
float toDegrees,
float progress)```
Linearly interpolates between two angles in degrees. Takes into account that angles wrap at 360 degrees and always takes the direction with the smallest delta angle.
Parameters:
`fromDegrees` - start angle in degrees
`toDegrees` - target angle in degrees
`progress` - interpolation value in the range [0, 1]
Returns:
the interpolated angle in the range [0, 360[
• #### floor

`public static int floor(float value)`
Returns the largest integer less than or equal to the specified float. This method will only properly floor floats from -(2^14) to (Float.MAX_VALUE - 2^14).
• #### floorPositive

`public static int floorPositive(float value)`
Returns the largest integer less than or equal to the specified float. This method will only properly floor floats that are positive. Note this method simply casts the float to int.
• #### ceil

`public static int ceil(float value)`
Returns the smallest integer greater than or equal to the specified float. This method will only properly ceil floats from -(2^14) to (Float.MAX_VALUE - 2^14).
• #### ceilPositive

`public static int ceilPositive(float value)`
Returns the smallest integer greater than or equal to the specified float. This method will only properly ceil floats that are positive.
• #### round

`public static int round(float value)`
Returns the closest integer to the specified float. This method will only properly round floats from -(2^14) to (Float.MAX_VALUE - 2^14).
• #### roundPositive

`public static int roundPositive(float value)`
Returns the closest integer to the specified float. This method will only properly round floats that are positive.
• #### isZero

`public static boolean isZero(float value)`
Returns true if the value is zero (using the default tolerance as upper bound)
• #### isZero

```public static boolean isZero(float value,
float tolerance)```
Returns true if the value is zero.
Parameters:
`tolerance` - represent an upper bound below which the value is considered zero.
• #### isEqual

```public static boolean isEqual(float a,
float b)```
Returns true if a is nearly equal to b. The function uses the default floating error tolerance.
Parameters:
`a` - the first value.
`b` - the second value.
• #### isEqual

```public static boolean isEqual(float a,
float b,
float tolerance)```
Returns true if a is nearly equal to b.
Parameters:
`a` - the first value.
`b` - the second value.
`tolerance` - represent an upper bound below which the two values are considered equal.
• #### log

```public static float log(float a,
float value)```
Returns:
the logarithm of value with base a
• #### log2

`public static float log2(float value)`
Returns:
the logarithm of value with base 2