Maven module :un.api : api-math :
Class : un.api.math.Maths
Extends/Implements : -
Subclasses : -

Convenient methods for numbers and maths.
author  Johann Sorel
author  Bertrand COTE


Variables : PI, TWO_PI, HALF_PI, QUATER_PI, E
Functions : min, min, min, min, min, min, min, min, min, min, min, min, min, min, max, max, max, max, max, max, max, max, max, max, max, max, max, max, sum, sum, sum, sum, sum, sum, sum, product, product, product, product, product, product, product, clamp, clamp, clamp, clamp, clamp, clamp, clamp, wrap, wrap, wrap, wrap, wrap, wrap, round, round, fract, ifract, fract, ifract, fastFloor, fastFloor, pow, pow, lerp, lerp, random, mean, mean, mean, variance, variance, variance, standardDeviation, standardDeviation, standardDeviation, median, median, median


public double PI

public double TWO_PI

public double HALF_PI

public double QUATER_PI

public double E



Give the minimum between v1 and v2.
param  v1 the first value.
param  v2 the second value.
return  the minimum between v1 and v2.
public char min (char v1, char v2)


Give the minimum between v1 and v2.
param  v1 the first value.
param  v2 the second value.
return  the minimum between v1 and v2.
public byte min (byte v1, byte v2)


Give the minimum between v1 and v2.
param  v1 the first value.
param  v2 the second value.
return  the minimum between v1 and v2.
public short min (short v1, short v2)


Give the minimum between v1 and v2.
param  v1 the first value.
param  v2 the second value.
return  the minimum between v1 and v2.
public int min (int v1, int v2)


Give the minimum between v1 and v2.
param  v1 the first value.
param  v2 the second value.
return  the minimum between v1 and v2.
public long min (long v1, long v2)


Give the minimum between v1 and v2.
param  v1 the first value.
param  v2 the second value.
return  the minimum between v1 and v2.
public float min (float v1, float v2)


Give the minimum between v1 and v2.
param  v1 the first value.
param  v2 the second value.
return  the minimum between v1 and v2.
public double min (double v1, double v2)


Give the minimum value in the array
param  a the array.
return  the minimum value in the array.
public double min (double[] a)


Give the minimum value in the array
param  a the array.
return  the minimum value in the array.
public float min (float[] a)


Give the minimum value in the array
param  a the array.
return  the minimum value in the array.
public long min (long[] a)


Give the minimum value in the array
param  a the array.
return  the minimum value in the array.
public int min (int[] a)


Give the minimum value in the array
param  a the array.
return  the minimum value in the array.
public short min (short[] a)


Give the minimum value in the array
param  a the array.
return  the minimum value in the array.
public byte min (byte[] a)


Give the minimum value in the array
param  a the array.
return  the minimum value in the array.
public char min (char[] a)


Give the maximum between v1 and v2.
param  v1 the first value.
param  v2 the second value.
return  the maximum between v1 and v2.
public char max (char v1, char v2)


Give the maximum between v1 and v2.
param  v1 the first value.
param  v2 the second value.
return  the maximum between v1 and v2.
public byte max (byte v1, byte v2)


Give the maximum between v1 and v2.
param  v1 the first value.
param  v2 the second value.
return  the maximum between v1 and v2.
public short max (short v1, short v2)


Give the maximum between v1 and v2.
param  v1 the first value.
param  v2 the second value.
return  the maximum between v1 and v2.
public int max (int v1, int v2)


Give the maximum between v1 and v2.
param  v1 the first value.
param  v2 the second value.
return  the maximum between v1 and v2.
public long max (long v1, long v2)


Give the maximum between v1 and v2.
param  v1 the first value.
param  v2 the second value.
return  the maximum between v1 and v2.
public float max (float v1, float v2)


Give the maximum between v1 and v2.
param  v1 the first value.
param  v2 the second value.
return  the maximum between v1 and v2.
public double max (double v1, double v2)


Give the maximum value in the array
param  a the array.
return  the maximum value in the array.
public double max (double[] a)


Give the maximum value in the array
param  a the array.
return  the maximum value in the array.
public float max (float[] a)


Give the maximum value in the array
param  a the array.
return  the maximum value in the array.
public long max (long[] a)


Give the maximum value in the array
param  a the array.
return  the maximum value in the array.
public int max (int[] a)


Give the maximum value in the array
param  a the array.
return  the maximum value in the array.
public short max (short[] a)


Give the maximum value in the array
param  a the array.
return  the maximum value in the array.
public byte max (byte[] a)


Give the maximum value in the array
param  a the array.
return  the maximum value in the array.
public char max (char[] a)


Computes and returns the sum of all values in the given array.
param  array values
return  the sum of all values in the given array.
public char sum (char[] array)


Computes and returns the sum of all values in the given array.
param  array values
return  the sum of all values in the given array.
public byte sum (byte[] array)


Computes and returns the sum of all values in the given array.
param  array values
return  the sum of all values in the given array.
public short sum (short[] array)


Computes and returns the sum of all values in the given array.
param  array values
return  the sum of all values in the given array.
public int sum (int[] array)


Computes and returns the sum of all values in the given array.
param  array values
return  the sum of all values in the given array.
public long sum (long[] array)


Computes and returns the sum of all values in the given array.
param  array values
return  the sum of all values in the given array.
public float sum (float[] array)


Computes and returns the sum of all values in the given array.
param  array values
return  the sum of all values in the given array.
public double sum (double[] array)


Computes and returns the product of all values in the given array.
param  array values
return  the product of all values in the given array.
public char product (char[] array)


Computes and returns the product of all values in the given array.
param  array values
return  the product of all values in the given array.
public byte product (byte[] array)


Computes and returns the product of all values in the given array.
param  array values
return  the product of all values in the given array.
public short product (short[] array)


Computes and returns the product of all values in the given array.
param  array values
return  the product of all values in the given array.
public int product (int[] array)


Computes and returns the product of all values in the given array.
param  array values
return  the product of all values in the given array.
public long product (long[] array)


Computes and returns the product of all values in the given array.
param  array values
return  the product of all values in the given array.
public float product (float[] array)


Computes and returns the product of all values in the given array.
param  array values
return  the product of all values in the given array.
public double product (double[] array)


Clamp returns:

@param val the value
@param min minimum value
@param max maximum value
@return clamped value
public char clamp (char val, char min, char max)


Clamp returns:

@param val the value
@param min minimum value
@param max maximum value
@return clamped value
public byte clamp (byte val, byte min, byte max)


Clamp returns:

@param val the value
@param min minimum value
@param max maximum value
@return clamped value
public short clamp (short val, short min, short max)


Clamp returns:

@param val the value
@param min minimum value
@param max maximum value
@return clamped value
public int clamp (int val, int min, int max)


Clamp returns:

@param val the value
@param min minimum value
@param max maximum value
@return clamped value
public long clamp (long val, long min, long max)


Clamp returns:

@param val the value
@param min minimum value
@param max maximum value
@return clamped value
public float clamp (float val, float min, float max)


Clamp returns:

@param val the value
@param min minimum value
@param max maximum value
@return clamped value
public double clamp (double val, double min, double max)


Wrap returns the wrapped value in the given range.
This is used for cyclic coordinate systems like angles, color spaces or
geographic coordinates.
Example :
wrap(-190,-180,+180) return +170
param  val the value
param  min lower cyclic value
param  max upper cyclic value
return  wrapped value
public byte wrap (byte val, byte min, byte max)


Wrap returns the wrapped value in the given range.
This is used for cyclic coordinate systems like angles, color spaces or
geographic coordinates.
Example :
wrap(-190,-180,+180) return +170
param  val the value
param  min lower cyclic value
param  max upper cyclic value
return  wrapped value
public short wrap (short val, short min, short max)


Wrap returns the wrapped value in the given range.
This is used for cyclic coordinate systems like angles, color spaces or
geographic coordinates.
Example :
wrap(-190,-180,+180) return +170
param  val the value
param  min lower cyclic value
param  max upper cyclic value
return  wrapped value
public int wrap (int val, int min, int max)


Wrap returns the wrapped value in the given range.
This is used for cyclic coordinate systems like angles, color spaces or
geographic coordinates.
Example :
wrap(-190,-180,+180) return +170
param  val the value
param  min lower cyclic value
param  max upper cyclic value
return  wrapped value
public long wrap (long val, long min, long max)


Wrap returns the wrapped value in the given range.
This is used for cyclic coordinate systems like angles, color spaces or
geographic coordinates.
Example :
wrap(-190,-180,+180) return +170
param  val the value
param  min lower cyclic value
param  max upper cyclic value
return  wrapped value
public float wrap (float val, float min, float max)


Wrap returns the wrapped value in the given range.
This is used for cyclic coordinate systems like angles, color spaces or
geographic coordinates.
Example :
wrap(-190,-180,+180) return +170
param  val the value
param  min lower cyclic value
param  max upper cyclic value
return  wrapped value
public double wrap (double val, double min, double max)

public int round (float val)

public long round (double val)


Get fractional part of given number.
param  val value
return  fractional part
public float fract (float val)


Get inverse fractional part of given number.
param  val value
return  inverse fractional part
public float ifract (float val)


Get fractional part of given number.
param  val value
return  inverse fractional part
public double fract (double val)


Get inverse fractional part of given number.
param  val value
return  inverse fractional part
public double ifract (double val)


Faster version of Math.floor.
test is not as accurate. only making a naive floor operation correct
enough when exact bit number algebra is not needed.
param  x value
return  floor value
public int fastFloor (float x)


Faster version of Math.floor.
test is not as accurate. only making a naive floor operation correct
enough when exact bit number algebra is not needed.
param  x value
return  floor value
public int fastFloor (double x)


Power function for integer powers.
In mathematics, pow(0,0) is undetermined, but here pow(0,0) return 1.
(O( log n ) algorithm.)
@param x value
@param n the power ( can be inferior to 0 )
@return x power n
public double pow (double x, int n)


Power function for integer powers.
In mathematics, pow(0,0) is undetermined, but here pow(0,0) returns 1.
(O( log n ) algorithm.)
@param x value
@param n the power ( can be inferior to 0 )
@return x power n
public float pow (float x, int n)


Linear interpolation between a and b.
param  a start value
param  b end value
param  ratio : 0 is close to start, 1 is on end
return  interpolated value
public double lerp (double a, double b, double ratio)


Linear interpolation between a and b.
param  a start value
param  b end value
param  ratio : 0 is close to start, 1 is on end
return  interpolated value
public float lerp (float a, float b, float ratio)


Generate a random value between given range.
param  min lower threshold
param  max upper threshold
return  random value
public double random (double min, double max)


Computes and returns the mean value of the given array.
param  array values
return  the mean of the given array.
public double mean (int[] array)


Computes and returns the mean value of the given array.
param  array values
return  the mean of the given array.
public float mean (float[] array)


Computes and returns the mean value of the given array.
param  array values
return  the mean of the given array.
public double mean (double[] array)


Computes and returns the variance value of the given array.
param  array values
return  the variance of the given array.
public double variance (int[] array)


Computes and returns the variance value of the given array.
param  array values
return  the variance of the given array.
public double variance (float[] array)


Computes and returns the variance value of the given array.
param  array values
return  the variance of the given array.
public double variance (double[] array)


Computes and returns the standard deviation value of the given array.
param  array values
return  the standard deviation of the given array.
public double standardDeviation (int[] array)


Computes and returns the standard deviation value of the given array.
param  array values
return  the standard deviation of the given array.
public double standardDeviation (float[] array)


Computes and returns the standard deviation value of the given array.
param  array values
return  the standard deviation of the given array.
public double standardDeviation (double[] array)


Computes and returns the median value of the given array.
param  array values
return  the median value of the given array.
public double median (int[] array)


Computes and returns the median value of the given array.
param  array values
return  the median value of the given array.
public double median (float[] array)


Computes and returns the median value of the given array.
param  array values
return  the median value of the given array.
public double median (double[] array)