org.openmali
Class FastMath

java.lang.Object
  extended by org.openmali.FastMath

public class FastMath
extends java.lang.Object

The FastMath class provides fast and optimized mathematical functions.


Nested Class Summary
static class FastMath.FRExpResultf
          Helper class to return result tuple of frexp()
 
Field Summary
static float DEG_TO_RAD
           
static float E
           
static float INV_PI
           
static float INV_TWO_PI
           
static float LOG10_TO_LOG2
           
static double LOG10_TO_LOG2d
           
static float PI
           
static float PI_HALF
           
protected static int precision
           
static float RAD_TO_DEG
           
static float TWO_PI
           
 
Constructor Summary
FastMath()
           
 
Method Summary
static float acos(float x)
           
static float asin(float x)
           
static float atan(float x)
           
static float atan2(float y, float x)
           
static float cbrt(float x)
           
static float ceil(float x)
           
static float cos(float x)
           
static float cosh(float x)
           
static boolean epsilonEquals(float v1, float v2, float epsilon)
           
static float floor(float x)
           
static FastMath.FRExpResultf frexp(float value)
          An implementation of the C standard library frexp() function.
static FastMath.FRExpResultf frexp(float value, FastMath.FRExpResultf result)
          An implementation of the C standard library frexp() function.
static int getPrecision()
           
static java.util.Random getRandom()
          Gets the Random generator.
static float hypot(float a, float b)
          sqrt(a^2 + b^2) without under/overflow.
static float invSqrt(float x)
           
static float log(float x)
           
static float log10(float x)
           
static float log1p(float x)
           
static float log2(float x)
           
static float pow(float base, float exp)
           
static int pow(int base, int exp)
           
static float pow2(float base)
           
static float pow3(float base)
           
static float randomFloat()
          Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.
static float randomFloat(float max)
          Generates the next random float (minimum: 0 inclusive).
static float randomFloat(float min, float max)
          Generates the next random float.
static int randomInt()
          Generates the next random int (minimum: 0 inclusive, maximum: Integer.MAX_VALUE exclusive).
static int randomInt(int max)
          Generates the next random int (minimum: 0 inclusive).
static int randomInt(int min, int max)
          Generates the next random int.
static long randomLong()
          Generates the next random long (minimum: 0 inclusive, maximum: Long.MAX_VALUE exclusive).
static long randomLong(long max)
          Generates the next random long (minimum: 0 inclusive).
static long randomLong(long min, long max)
          Generates the next random long.
static void setPrecision(int precision)
           
static float sin(float x)
           
static float sinh(float x)
           
static float sqrt(float x)
           
static float tan(float x)
           
static float tanh(float x)
           
static float toDeg(float x)
           
static float toRad(float x)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PI

public static final float PI
See Also:
Constant Field Values

PI_HALF

public static final float PI_HALF
See Also:
Constant Field Values

TWO_PI

public static final float TWO_PI
See Also:
Constant Field Values

INV_PI

public static final float INV_PI
See Also:
Constant Field Values

INV_TWO_PI

public static final float INV_TWO_PI
See Also:
Constant Field Values

E

public static final float E
See Also:
Constant Field Values

DEG_TO_RAD

public static final float DEG_TO_RAD
See Also:
Constant Field Values

RAD_TO_DEG

public static final float RAD_TO_DEG
See Also:
Constant Field Values

LOG10_TO_LOG2d

public static final double LOG10_TO_LOG2d
See Also:
Constant Field Values

LOG10_TO_LOG2

public static final float LOG10_TO_LOG2
See Also:
Constant Field Values

precision

protected static int precision
Constructor Detail

FastMath

public FastMath()
Method Detail

setPrecision

public static final void setPrecision(int precision)

getPrecision

public static final int getPrecision()

sin

public static final float sin(float x)

asin

public static final float asin(float x)

sinh

public static final float sinh(float x)

cos

public static final float cos(float x)

acos

public static final float acos(float x)

cosh

public static final float cosh(float x)

tan

public static final float tan(float x)

atan

public static final float atan(float x)

atan2

public static final float atan2(float y,
                                float x)

tanh

public static final float tanh(float x)

cbrt

public static final float cbrt(float x)

sqrt

public static final float sqrt(float x)

invSqrt

public static final float invSqrt(float x)

ceil

public static final float ceil(float x)

floor

public static final float floor(float x)

log

public static final float log(float x)
Parameters:
x -
Returns:
the natural logarithm (base e)
See Also:
Math.log(double)

log10

public static final float log10(float x)

log1p

public static final float log1p(float x)

log2

public static final float log2(float x)
Parameters:
x -
Returns:
the logarithm to base 2

toDeg

public static final float toDeg(float x)

toRad

public static final float toRad(float x)

pow

public static final float pow(float base,
                              float exp)

pow2

public static final float pow2(float base)

pow3

public static final float pow3(float base)

pow

public static final int pow(int base,
                            int exp)

epsilonEquals

public static final boolean epsilonEquals(float v1,
                                          float v2,
                                          float epsilon)

hypot

public static final float hypot(float a,
                                float b)
sqrt(a^2 + b^2) without under/overflow.


frexp

public static final FastMath.FRExpResultf frexp(float value,
                                                FastMath.FRExpResultf result)
An implementation of the C standard library frexp() function.

Parameters:
value - the number to split
result -
Returns:
a touple of normalised mantissa and exponent

frexp

public static final FastMath.FRExpResultf frexp(float value)
An implementation of the C standard library frexp() function.

Parameters:
value - the number to split
Returns:
a touple of normalised mantissa and exponent

getRandom

public static final java.util.Random getRandom()
Gets the Random generator.

Returns:
the Random generator.

randomFloat

public static final float randomFloat()
Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

The general contract of nextFloat is that one float value, chosen (approximately) uniformly from the range 0.0f (inclusive) to 1.0f (exclusive), is pseudorandomly generated and returned. All 224 possible float values of the form m x 2-24, where m is a positive integer less than 224 , are produced with (approximately) equal probability. The method nextFloat is implemented by class Random as follows:

 public float nextFloat() {
      return next(24) / ((float)(1 << 24));
 }
The hedge "approximately" is used in the foregoing description only because the next method is only approximately an unbiased source of independently chosen bits. If it were a perfect source or randomly chosen bits, then the algorithm shown would choose float values from the stated range with perfect uniformity.

[In early versions of Java, the result was incorrectly calculated as:

 return next(30) / ((float)(1 << 30));
This might seem to be equivalent, if not better, but in fact it introduced a slight nonuniformity because of the bias in the rounding of floating-point numbers: it was slightly more likely that the low-order bit of the significand would be 0 than that it would be 1.]

Returns:
the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

randomFloat

public static final float randomFloat(float max)
Generates the next random float (minimum: 0 inclusive).

Parameters:
max - the maximum number (exclusive)
Returns:
the generated number.

randomFloat

public static final float randomFloat(float min,
                                      float max)
Generates the next random float.

Parameters:
min - the minimum number (inclusive)
max - the maximum number (exclusive)
Returns:
the generated number.

randomInt

public static final int randomInt()
Generates the next random int (minimum: 0 inclusive, maximum: Integer.MAX_VALUE exclusive).

Returns:
the generated number.

randomInt

public static final int randomInt(int max)
Generates the next random int (minimum: 0 inclusive).

Parameters:
max - the maximum number (exclusive)
Returns:
the generated number.

randomInt

public static final int randomInt(int min,
                                  int max)
Generates the next random int.

Parameters:
min - the minimum number (inclusive)
max - the maximum number (exclusive)
Returns:
the generated number.

randomLong

public static final long randomLong()
Generates the next random long (minimum: 0 inclusive, maximum: Long.MAX_VALUE exclusive).

Returns:
the generated number.

randomLong

public static final long randomLong(long max)
Generates the next random long (minimum: 0 inclusive).

Parameters:
max - the maximum number (exclusive)
Returns:
the generated number.

randomLong

public static final long randomLong(long min,
                                    long max)
Generates the next random long.

Parameters:
min - the minimum number (inclusive)
max - the maximum number (exclusive)
Returns:
the generated number.