dreamworkAPI
Ctrl K
GitHub
libraryDocs / math

math

librarysharedmenusource

The powerful math library.

Modules

Fields

NameTypeDescription
enumberA variable containing the mathematical constant e. [2.7182818284590]
ln2numberA variable containing the mathematical constant natural logarithm of 2. [0.69314718055995]
nannumberA variable containing number "not a number". [nan]
pinumberA variable containing the mathematical π constant. [3.1415926535898]
hugenumberA variable that effectively represents infinity, in the sense that in any numerical comparison every number will be less than this. [inf]
tinynumberA variable that effectively represents negative infinity, in the sense that in any numerical comparison every number will be greater than this. [-inf]
ln10numberA variable containing the mathematical constant natural logarithm of 10. (2.3025850929940)
log10enumberA variable containing the mathematical constant logarithm of 10 to the base e. (0.43429448190325)
log2enumberA variable containing the mathematical constant logarithm of 2 to the base e. (1.4426950408889)
sqrt2numberA variable containing the mathematical constant square root of 2. (1.4142135623731)
sqrt1_2numberA variable containing the mathematical constant square root of 1/2. (0.70710678118655)
randomseednumberA variable containing the current random seed and can be changed to set a new seed.

Functions

math.angleCalculates the angle between two points.
math.angleDifferenceReturns the difference between two angles.
math.angleNormalizeReturns the normalised angle between two points.
math.approachGradually approaches the target value by the specified amount.
math.atanReturns the arc tangent of y/x.
math.averageReturns the arithmetic mean of the given numbers.
math.bucketizeReturns a function that bucketizes a number.
math.cbrtReturns the cube root of the given number.
math.clampClamps a number between a minimum and maximum value.
math.copySignReturns x with the same sign as y.
math.cotReturns the cotangent of the given angle.
math.directionCalculates the direction between two points.
math.euclideanModCalculates the euclidean modulus.
math.fdivReturns the floor division of two numbers.
math.frexpReturns m and e such that x = m2e, e is an integer and the absolute value of m is in the range ((0.5, 1) (or zero when…
math.hypotReturns the square root of the sum of squares of its arguments.
math.ilerpPerforms an inverse linear interpolation from the start number to the end number.
math.inRangeChecks if a number is in a range.
math.isDivideableChecks if a number is divisible by another number without remainder.
math.isEvenChecks if a number is even.
math.isFiniteChecks if a number is finite.
math.isFloatChecks if a number is a float point number.
math.isInfChecks if a number is positive or negative infinity.
math.isIntegerChecks if a number is an signed integer.
math.isNaNChecks if a number is NaN.
math.isNegativeChecks if a number is negative.
math.isOddChecks if a number is odd.
math.isPositiveChecks if a number is positive.
math.isPowerOfTwoCheck if a value is a power-of-two.
math.isUnsignedIntegerChecks if a number is an unsigned integer.
math.ldexpTakes a normalised number and returns the floating point representation.
math.lenReturns the length of the integer.
math.lerpPerforms a linear interpolation from the start number to the end number.
math.log1pReturns the natural logarithm of the given number.
math.lowerReturns x if its absolute value is equal to or greater than size, otherwise returns 0.
math.magnitudeCalculates the magnitude (distance) between two points.
math.randomfReturns a random floating point number in the range [a, b).
math.rangeNormalizes a start_position/end_position pair against length, same rules as string.sub.
math.relativeTranslate a relative position, negative means back from end.
math.remapRemaps a number from one range to another.
math.rootReturns the root of a given number with a given base.
math.roundRounds the given value to the nearest whole number or to the given decimal places.
math.smoothPerforms a smooth interpolation from the start number to the end number.
math.snapReturns the smallest integer greater than or equal to the given number.
math.thresholdCheck if value is equal or greater than threshold.
math.toFloat32Converts a number to a float32.
math.toIntConverts an signed integer with a sign to an integer.
math.toInt32Converts a number to a 32-bit signed integer.
math.toleranceCheck if value is equal or less than threshold.
math.toUIntConverts an integer with a sign to an unsigned integer.
math.toUInt32Converts a number to a 32-bit unsigned integer.
math.truncReturns the integer part of the given number, rounding towards zero.
math.upperReturns x if its absolute value is equal to or less than size, otherwise returns 0.
math.wrapWraps a number to keep it within a specific minimum and maximum range.

#math.angle

sharedmenunodiscardsource
function math.angle(x1: number, y1: number, x2: number, y2: number): number

Calculates the angle between two points.

Parameters
NameTypeDescription
x1numberThe x coordinate of the first point.
y1numberThe y coordinate of the first point.
x2numberThe x coordinate of the second point.
y2numberThe y coordinate of the second point.
Returns
TypeNameDescription
numberangleThe angle between the two points.

#math.angleDifference

sharedmenunodiscardsource
function math.angleDifference(a: number, b: number): number

Returns the difference between two angles.

Parameters
NameTypeDescription
anumberThe first angle.
bnumberThe second angle.
Returns
TypeNameDescription
numberdiffThe difference between the angles.

#math.angleNormalize

sharedmenunodiscardsource
function math.angleNormalize(angle: number): number

Returns the normalised angle between two points.

Parameters
NameTypeDescription
anglenumberThe angle to normalise.
Returns
TypeNameDescription
numbernormalisedThe normalised angle.

#math.approach

sharedmenunodiscardsource
function math.approach(current: number, target: number, change: number): number

Gradually approaches the target value by the specified amount.

Parameters
NameTypeDescription
currentnumberThe current value.
targetnumberThe target value.
changenumberThe amount that the current value is allowed to change by to approach the target.
Returns
TypeNameDescription
numberapproachedThe approached value.

#math.atan

sharedmenunodiscardsource
function math.atan(y: number, x: number): number

Returns the arc tangent of y/x.

Parameters
NameTypeDescription
ynumberThe y coordinate.
xnumberThe x coordinate.
Returns
TypeNameDescription
numberarc_tanThe arc tangent of y/x.

#math.average

sharedmenunodiscardsource
function math.average(...: number): number

Returns the arithmetic mean of the given numbers.

Parameters
NameTypeDescription
...numberThe numbers to average.
Returns
TypeNameDescription
numberaverageThe arithmetic mean of the given numbers.

#math.bucketize

sharedmenunodiscardsource
function math.bucketize(y: T): fun( x: number ): T

Returns a function that bucketizes a number.

Parameters
NameTypeDescription
yTThe bucket size.
Returns
TypeNameDescription
fun( x: number ): TbucketizerThe bucketizer function.

#math.cbrt

sharedmenunodiscardsource
function math.cbrt(x: number): number

Returns the cube root of the given number.

Parameters
NameTypeDescription
xnumberThe number to calculate the cube root of.
Returns
TypeNameDescription
numbercubeThe cube root of the number.

#math.clamp

sharedmenunodiscardsource
function math.clamp(number: number, min: number, max: number): number

Clamps a number between a minimum and maximum value.

Parameters
NameTypeDescription
numbernumberThe number to clamp.
minnumberThe minimum value.
maxnumberThe maximum value.
Returns
TypeNameDescription
numberclampedThe clamped number.

#math.copySign

sharedmenunodiscardsource
function math.copySign(x: T, y: number): T

Returns x with the same sign as y.

Parameters
NameTypeDescription
xTThe number to copy the sign of.
ynumberThe number to get the sign from.
Returns
TypeNameDescription
TnumberThe number with the sign of y.

#math.cot

sharedmenunodiscardsource
function math.cot(x: number): number

Returns the cotangent of the given angle.

Parameters
NameTypeDescription
xnumberThe angle in radians.
Returns
TypeNameDescription
numbercotangentThe cotangent of the given angle.

#math.direction

sharedmenunodiscardsource
function math.direction(x1: number, y1: number, x2: number, y2: number): number, number

Calculates the direction between two points.

Parameters
NameTypeDescription
x1numberThe x coordinate of the first point.
y1numberThe y coordinate of the first point.
x2numberThe x coordinate of the second point.
y2numberThe y coordinate of the second point.
Returns
TypeNameDescription
numberxThe x coordinate of the direction.
numberyThe y coordinate of the direction.

#math.euclideanMod

sharedmenunodiscardsource
function math.euclideanMod(numerator: number, denominator: number): number

Calculates the euclidean modulus.

Parameters
NameTypeDescription
numeratornumberThe numerator.
denominatornumberThe denominator.
Returns
TypeNameDescription
numbermodThe euclidean modulus.

#math.fdiv

sharedmenunodiscardsource
function math.fdiv(a: T, b: T): T

Returns the floor division of two numbers.

Parameters
NameTypeDescription
aTThe first number.
bTThe second number.
Returns
TypeNameDescription
TxThe floor division of the two numbers.

#math.frexp

sharedmenunodiscardsource
function math.frexp(x: number): number, number

Returns m and e such that x = m2e, e is an integer and the absolute value of m is in the range ((0.5, 1) (or zero when x is zero).

Used to split the number value into a normalized fraction and an exponent. Two values are returned: the first is a multiplier in the range 1/2 (inclusive) to 1 (exclusive) and the second is an integer exponent.

The result is such that x = m*2^e.

Parameters
NameTypeDescription
xnumberThe number to split.
Returns
TypeNameDescription
numbermThe normalized fraction.
numbereThe exponent.

#math.hypot

sharedmenunodiscardsource
function math.hypot(numbers: number[], number_count?: integer): number

Returns the square root of the sum of squares of its arguments.

Parameters
NameTypeDescription
numbersnumber[]The numbers to calculate the square root of.
number_countoptionalintegerThe number of numbers to calculate the square root of.
Returns
TypeNameDescription
numbervalueThe square root of the sum of squares of its arguments.

#math.ilerp

sharedmenunodiscardsource
function math.ilerp(result: number, from: number, to: number): number

Performs an inverse linear interpolation from the start number to the end number.

Parameters
NameTypeDescription
resultnumberThe interpolated value.
fromnumberThe start number.
tonumberThe end number.
Returns
TypeNameDescription
numberlerpedThe fraction of the way between the start and end numbers.

#math.inRange

sharedmenunodiscardsource
function math.inRange(number: number, from: number, to: number): boolean

Checks if a number is in a range.

Parameters
NameTypeDescription
numbernumberThe number to check.
fromnumberThe minimum value of the range.
tonumberThe maximum value of the range.
Returns
TypeNameDescription
booleanin_rangetrue if the number is in the range, otherwise false.

#math.isDivideable

sharedmenunodiscardsource
function math.isDivideable(a: number, b: number): boolean

Checks if a number is divisible by another number without remainder.

Parameters
NameTypeDescription
anumberThe first number to check.
bnumberThe second number to check.
Returns
TypeNameDescription
booleanis_divideabletrue if the first number is divisible by the second number, otherwise false.

#math.isEven

sharedmenunodiscardsource
function math.isEven(x: number): boolean

Checks if a number is even.

Parameters
NameTypeDescription
xnumberThe number to check.
Returns
TypeNameDescription
booleanis_eventrue if the number is even, otherwise false.

#math.isFinite

sharedmenunodiscardsource
function math.isFinite(x: number): boolean

Checks if a number is finite.

Parameters
NameTypeDescription
xnumberThe number to check.
Returns
TypeNameDescription
booleanis_finitetrue if the number is finite, otherwise false.

#math.isFloat

sharedmenunodiscardsource
function math.isFloat(x: number): boolean

Checks if a number is a float point number.

Parameters
NameTypeDescription
xnumberThe number to check.
Returns
TypeNameDescription
booleanis_floattrue if the number is a float, otherwise false.

#math.isInf

sharedmenunodiscardsource
function math.isInf(x: number): boolean

Checks if a number is positive or negative infinity.

Parameters
NameTypeDescription
xnumberThe number to check.
Returns
TypeNameDescription
booleanis_inftrue if the number is positive or negative infinity, otherwise false.

#math.isInteger

sharedmenunodiscardsource
function math.isInteger(x: number): boolean

Checks if a number is an signed integer.

Parameters
NameTypeDescription
xnumberThe number to check.
Returns
TypeNameDescription
booleanis_inttrue if the number is an integer, otherwise false.

#math.isNaN

sharedmenunodiscardsource
function math.isNaN(x: number): boolean

Checks if a number is NaN.

Parameters
NameTypeDescription
xnumberThe number to check.
Returns
TypeNameDescription
booleanis_nantrue if the number is NaN, otherwise false.

#math.isNegative

sharedmenunodiscardsource
function math.isNegative(x: number): boolean

Checks if a number is negative.

Parameters
NameTypeDescription
xnumberThe number to check.
Returns
TypeNameDescription
booleanis_negativetrue if the number is negative, otherwise false.

#math.isOdd

sharedmenunodiscardsource
function math.isOdd(x: number): boolean

Checks if a number is odd.

Parameters
NameTypeDescription
xnumberThe number to check.
Returns
TypeNameDescription
booleanis_oddtrue if the number is odd, otherwise false.

#math.isPositive

sharedmenunodiscardsource
function math.isPositive(x: number): boolean

Checks if a number is positive.

Parameters
NameTypeDescription
xnumberThe number to check.
Returns
TypeNameDescription
booleanis_positivetrue if the number is positive, otherwise false.

#math.isPowerOfTwo

sharedmenunodiscardsource
function math.isPowerOfTwo(x: number): boolean

Check if a value is a power-of-two.

Parameters
NameTypeDescription
xnumberThe number to check.
Returns
TypeNameDescription
booleanis_pottrue if a number is a valid power-of-two, otherwise false.

#math.isUnsignedInteger

sharedmenunodiscardsource
function math.isUnsignedInteger(x: number): boolean

Checks if a number is an unsigned integer.

Parameters
NameTypeDescription
xnumberThe number to check.
Returns
TypeNameDescription
booleanis_uinttrue if the number is an integer, otherwise false.

#math.ldexp

sharedmenunodiscardsource
function math.ldexp(x: number, exponent: number): number

Takes a normalised number and returns the floating point representation.

Effectively it returns the result of normalizedFraction * 2.0 ^ exponent.

Parameters
NameTypeDescription
xnumberThe base value.
exponentnumberThe exponent.
Returns
TypeNameDescription
numberfloatThe floating point representation.
See also

#math.len

sharedmenunodiscardsource
function math.len(x: integer): integer

Returns the length of the integer.

Parameters
NameTypeDescription
xintegerThe integer to check.
Returns
TypeNameDescription
integerlengthThe length of the integer.

#math.lerp

sharedmenunodiscardsource
function math.lerp(fraction: number, from: number, to: number): number

Performs a linear interpolation from the start number to the end number.

Parameters
NameTypeDescription
fractionnumberThe fraction of the way between the start and end numbers.
fromnumberThe start number.
tonumberThe end number.
Returns
TypeNameDescription
numberlerpedThe interpolated value.

#math.log1p

sharedmenunodiscardsource
function math.log1p(x: number): number

Returns the natural logarithm of the given number.

Parameters
NameTypeDescription
xnumberThe number to calculate the logarithm of.
Returns
TypeNameDescription
numberlogThe natural logarithm of the number.

#math.lower

sharedmenunodiscardsource
function math.lower(x: number, size: number): number

Returns x if its absolute value is equal to or greater than size, otherwise returns 0.

Parameters
NameTypeDescription
xnumberThe number to check.
sizenumberThe minimum allowed absolute value before snapping to 0.
Returns
TypeNameDescription
numberresultThe original number, or 0.

#math.magnitude

sharedmenunodiscardsource
function math.magnitude(x1: number, y1: number, x2: number, y2: number): number

Calculates the magnitude (distance) between two points.

Parameters
NameTypeDescription
x1numberThe x coordinate of the first point.
y1numberThe y coordinate of the first point.
x2numberThe x coordinate of the second point.
y2numberThe y coordinate of the second point.
Returns
TypeNameDescription
numbermagnitudeThe magnitude between the two points.

#math.randomf

sharedmenunodiscardsource
function math.randomf(a: number, b: number): number

Returns a random floating point number in the range [a, b).

Parameters
NameTypeDescription
anumberThe minimum value.
bnumberThe maximum value.
Returns
TypeNameDescription
numberfloatThe random floating point number.

#math.range

sharedmenunodiscardsource
function math.range(length: integer, start_position?: integer, end_position?: integer): integer, integer

Normalizes a start_position/end_position pair against length, same rules as string.sub.

Negative positions count back from the end, start_position is clamped to >= 1 and end_position to [0, length].

An empty range is returned as start_position > end_position.

Parameters
NameTypeDescription
lengthintegerThe length of the sequence.
start_positionoptionalintegerThe start position, defaults to 1.
end_positionoptionalintegerThe end position, defaults to length.
Returns
TypeNameDescription
integerstart_positionThe normalized start position.
integerend_positionThe normalized end position.

#math.relative

sharedmenunodiscardsource
function math.relative(position: integer, length: integer, fallback?: integer): integer

Translate a relative position, negative means back from end.

Parameters
NameTypeDescription
positionintegerThe position to check.
lengthintegerThe length to check.
fallbackoptionalintegerThe fallback position.
Returns
TypeNameDescription
integerrelativeThe relative position.

#math.remap

sharedmenunodiscardsource
function math.remap(number: number, inMin: number, inMax: number, outMin: number, outMax: number): number

Remaps a number from one range to another.

Parameters
NameTypeDescription
numbernumberThe number to remap.
inMinnumberThe minimum value of the input range.
inMaxnumberThe maximum value of the input range.
outMinnumberThe minimum value of the output range.
outMaxnumberThe maximum value of the output range.
Returns
TypeNameDescription
numbervalueThe remapped value.

#math.root

sharedmenunodiscardsource
function math.root(a: number, b: number): number

Returns the root of a given number with a given base.

Parameters
NameTypeDescription
anumberThe number to calculate the root of.
bnumberThe base of the root.
Returns
TypeNameDescription
numberrootThe root of the number.

#math.round

sharedmenunodiscardsource
function math.round(number: number, decimals?: integer): number

Rounds the given value to the nearest whole number or to the given decimal places.

Parameters
NameTypeDescription
numbernumberThe number to round.
decimalsoptionalintegerThe number of decimal places to round to.
Returns
TypeNameDescription
numberroundedThe rounded number.

#math.smooth

sharedmenunodiscardsource
function math.smooth(previous: number, next: number, alpha: number): number

Performs a smooth interpolation from the start number to the end number.

Parameters
NameTypeDescription
previousnumberThe previous value.
nextnumberThe next value.
alphanumberThe amount of smoothing.
Returns
TypeNameDescription
numbervalueThe interpolated value.

#math.snap

sharedmenunodiscardsource
function math.snap(number: number, step: number): number

Returns the smallest integer greater than or equal to the given number.

Parameters
NameTypeDescription
numbernumberThe number to round.
stepnumberThe step size to round to.
Returns
TypeNameDescription
numbersnappedThe rounded number.

#math.threshold

sharedmenunodiscardsource
function math.threshold(x: number, threshold?: number): boolean

Check if value is equal or greater than threshold.

NOTE: This is useful to mitigate accuracy issues in floating point numbers.

Examples:

lua
local a = 10
local b = 13.3

if math.threshold( a - b, 4 ) then
    print( "yes" )
else
    print( "no" )
end

-- print: `no` because difference between a and b is not so far from 4
Parameters
NameTypeDescription
xnumberThe number to check.
thresholdoptionalnumberThe maximum difference between numbers at which they are considered equal; by default 1e-8.
Returns
TypeNameDescription
booleanis_greater_equaltrue if the numbers are equal or greater, otherwise false.

#math.toFloat32

sharedmenunodiscardsource
function math.toFloat32(number: number): number

Converts a number to a float32.

Parameters
NameTypeDescription
numbernumberThe number to convert.
Returns
TypeNameDescription
numberfloatThe float32 number.

#math.toInt

sharedmenunodiscardsource
function math.toInt(x: T, bit_count: integer): T

Converts an signed integer with a sign to an integer.

Parameters
NameTypeDescription
xTThe number to convert.
bit_countintegerThe bit count of the signed integer.
Returns
TypeNameDescription
TsignedThe integer with a sign.

#math.toInt32

sharedmenunodiscardsource
function math.toInt32(x: T): T

Converts a number to a 32-bit signed integer.

Parameters
NameTypeDescription
xTThe number to convert.
Returns
TypeNameDescription
TsignedThe 32-bit signed integer.

#math.tolerance

sharedmenunodiscardsource
function math.tolerance(x: number, threshold?: number): boolean

Check if value is equal or less than threshold.

NOTE: This is useful to mitigate accuracy issues in floating point numbers.

Examples:

lua
local a = 10
local b = 13.3

if math.tolerance( a - b, 4 ) then
    print( "yes" )
else
    print( "no" )
end

-- print: `yes` because difference between a and b is near of 4
Parameters
NameTypeDescription
xnumberThe number to check.
thresholdoptionalnumberThe maximum difference between numbers at which they are considered equal; by default 1e-8.
Returns
TypeNameDescription
booleanis_nearly_equaltrue if the numbers are equal or less, otherwise false.

#math.toUInt

sharedmenunodiscardsource
function math.toUInt(x: T, bit_count: integer): T

Converts an integer with a sign to an unsigned integer.

Parameters
NameTypeDescription
xTThe number to convert.
bit_countintegerThe bit count of the unsigned integer.
Returns
TypeNameDescription
TunsignedThe unsigned integer.

#math.toUInt32

sharedmenunodiscardsource
function math.toUInt32(x: T): T

Converts a number to a 32-bit unsigned integer.

Parameters
NameTypeDescription
xTThe number to convert.
Returns
TypeNameDescription
TunsignedThe 32-bit unsigned integer.

#math.trunc

sharedmenunodiscardsource
function math.trunc(x: number): integer

Returns the integer part of the given number, rounding towards zero.

NOTE: Same result as (math.modf( x )), but math.modf is ~10x slower under JIT.

Parameters
NameTypeDescription
xnumberThe number to truncate.
Returns
TypeNameDescription
integertruncatedThe integer part of the number.

#math.upper

sharedmenunodiscardsource
function math.upper(x: number, size: number): number

Returns x if its absolute value is equal to or less than size, otherwise returns 0.

Useful for ignoring extreme spikes or outliers in values.

Parameters
NameTypeDescription
xnumberThe number to check.
sizenumberThe maximum allowed absolute value before snapping to 0.
Returns
TypeNameDescription
numberresultThe original number, or 0.

#math.wrap

sharedmenunodiscardsource
function math.wrap(value: number, min: number, max: number): number

Wraps a number to keep it within a specific minimum and maximum range.

Parameters
NameTypeDescription
valuenumberThe number to wrap.
minnumberThe lower limit of the wrap range.
maxnumberThe upper limit of the wrap range.
Returns
TypeNameDescription
numberwrappedThe number wrapped within [min, max).
Generated from the dreamwork meta branch.