Open Source Cross-Platform Game Programming

Math

Library for common mathematical functions.

Constants

NameTypeValueDescription
E float 2.718281828459045235360

It's e.

PI float 3.141592653589793238462

It's pi.

function abs(number)[link]

Returns the absolute value of the given numerical input. The type of the output will match the input.

Arguments

NameTypeDescription
number float or integer

Any number

Return Value

TypeDescription
float or integer

A positive number

function arccos(ratio)[link]

Calculates the arc cosine (inverse cosine) of the given ratio.

Arguments

NameTypeDescription
ratio float

A valid cosine ratio.

Return Value

TypeDescription
float

the angle with the given cosine value in radians

function arcsin(ratio)[link]

Calculates the arc sine (inverse sine) of the given ratio.

Arguments

NameTypeDescription
ratio float

A valid sine ratio.

Return Value

TypeDescription
float

the angle with the given sine value in radians

function arctan(yComponent, xComponent)[link]

Calculates the arc tangent (inverse tangent) of the given ratio. This function can be called with either 1 or 2 arguments. If given one argument, that value is the ratio and a theta between -pi / 2 and pi / 2will be returned. If given two arguments, these represent y and x components (respectively) of a vector. By providing two vector components, you will get return values in the full range of -pi to pi.

Arguments

NameTypeOptionalDescription
yComponent float

A y-component value.

xComponent float Optional

An optional x-component value. If ommitted, the default value is 1.0 and the first argument simply becomes a ratio.

Return Value

TypeDescription
float

the angle with the given tangent value

function cos(theta)[link]

Computes the sine of the given angle

Arguments

NameTypeDescription
theta float

An angle in radians

Return Value

TypeDescription
float

The sine ratio

function cos(theta)[link]

Computes the cosine of the given angle

Arguments

NameTypeDescription
theta float

An angle in radians

Return Value

TypeDescription
float

The cosine ratio

function ensureRange(num, lowerBound, upperBound)[link]

Ensures the number falls within the given range. The type of the output will match the number that is chosen. For example, ensureRange(2.0, 1, 3) will be 2.0, but ensureRange of (2, 0, 1) will be 1. If the number matches either the upper or lower bound, the initial input will be returned, so ensureRange(3.0, 3, 4) is unambiguously 3.0, not 3.

Arguments

NameTypeDescription
num float or integer

Any number

lowerBound float or integer

The lower bound

upperBound float or integer

The upper bound

Return Value

TypeDescription
float or integer

A guaranteed to be in the given range.

function floor(number)[link]

Returns the integer less than or equal to the given number.

Arguments

NameTypeDescription
number float or integer

A float to round down. Integers are also valid input, but will return themselves.

Return Value

TypeDescription
integer

The next integer less than or equal to the input.

function ln(number)[link]

Computes the natural logarithm (base e) of the input. Throws Core.InvalidArgumentException for values less than or equal to 0.

Arguments

NameTypeDescription
number float or integer

A positive number

Return Value

TypeDescription
float

The natural logarithm

function log10(number)[link]

Computes the base-10 logarithm of the input. Throws Core.InvalidArgumentException for values less than or equal to 0. Guaranteed to return floating-point-error-free floats for inputs that are exactly powers of 10.

Arguments

NameTypeDescription
number float or integer

A positive number

Return Value

TypeDescription
float

The base 10 logarithm

function log2(number)[link]

Computes the base-2 logarithm of the input. Throws Core.InvalidArgumentException for values less than or equal to 0. Guaranteed to return floating-point-error-free floats for inputs that are exactly powers of 2.

Arguments

NameTypeDescription
number float or integer

A positive number

Return Value

TypeDescription
float

The base 2 logarithm

function max(a, b)[link]

Returns the maximum of two numbers. The type of the output will match the number that is chosen. For example, max(3.0, 2) will be 3.0, but the max of (3, 2.0) will be 3. If they match, the first number is returned, so max(3.0, 3) is unambiguously 3.0, not 3.

Arguments

NameTypeDescription
a float or integer

Any number

b float or integer

Any number

Return Value

TypeDescription
float or integer

The higher number.

function min(a, b)[link]

Returns the minimum of two numbers. The type of the output will match the number that is chosen. For example, min(2.0, 3) will be 2.0, but the min of (2, 3.0) will be 2. If they match, the first number is returned, so min(3.0, 3) is unambiguously 3.0, not 3.

Arguments

NameTypeDescription
a float or integer

Any number

b float or integer

Any number

Return Value

TypeDescription
float or integer

The lower number.

function sign(num)[link]

Determines positive/negative/zero-ness of a number by returning -1, 0, or 1 accordingly. Output is always an integer regardless of input type.

Arguments

NameTypeDescription
num float or integer

Any number

Return Value

TypeDescription
integer

-1 if the number is negative. 1 if it's positive. 0 if it's 0 or 0.0

function tan(theta)[link]

Computes the tangent of the given angle.

Arguments

NameTypeDescription
theta float

An angle in radians

Return Value

TypeDescription
float

The tangent ratio