class Primitives · Computations

Values

Arithmetic on reactive numbers: add, subtract, multiply, divide, modulus, round, clamp, interpolate and remap.

Static methods

absstatic

static abs(value: Real | NumberLike, result?: Real | null): Real

Returns the absolute value of a real number.

value
The value to take the absolute value of.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The absolute value of value.

addstatic

static add(value1: Real | NumberLike, value2: Real | NumberLike, result?: Real | null): Real

Adds two real values.

value1
The first operand.
value2
The second operand.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The sum of value1 and value2.

ceilstatic

static ceil(value: Real | NumberLike, result?: Real | null): Real

Returns the smallest integer value greater than or equal to the specified real number.

value
The value to round up.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The ceiling of value.

clampstatic

static clamp(value: Real | NumberLike, min: Real | NumberLike, max: Real | NumberLike, result?: Real | null): Real

Clamps a value to the inclusive range [min, max].

value
The value to clamp.
min
The lower bound of the range.
max
The upper bound of the range.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns value if it lies within the range; otherwise the nearest bound. If min is greater than max, the result is max.

dividestatic

static divide(value1: Real | NumberLike, value2: Real | NumberLike, result?: Real | null): Real

Divides one real value by another.

value1
The dividend.
value2
The divisor.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The result of dividing value1 by value2.

floorstatic

static floor(value: Real | NumberLike, result?: Real | null): Real

Returns the largest integer value less than or equal to the specified real number.

value
The value to round down.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The floor of value.

inverseLerpstatic

static inverseLerp(a: Real | NumberLike, b: Real | NumberLike, value: Real | NumberLike, result?: Real | null): Real

Computes the inverse of a linear interpolation, returning the factor t such that Lerp(a, b, t) == value.

a
The start of the range, corresponding to t = 0.
b
The end of the range, corresponding to t = 1.
value
The value within the range to invert.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The interpolation factor that maps a–b to value. Returns 0 if a equals b.

isNearlyEqualstatic

static isNearlyEqual(value1: Real | NumberLike, value2: Real | NumberLike, tolerance: Real | NumberLike, result?: Bool | null): Bool

Determines whether two real values are equal within a specified tolerance.

value1
The first value.
value2
The second value.
tolerance
The maximum allowed difference for the values to be considered equal.
result optional
Optional Bool to write the result into; it is returned. Without it, a new Bool is returned.

Returns true if the absolute difference between the values is at most tolerance; otherwise false.

isZerostatic

static isZero(value: Real | NumberLike, result?: Bool | null): Bool

Determines whether a real value is exactly zero.

value
The value to test.
result optional
Optional Bool to write the result into; it is returned. Without it, a new Bool is returned.

Returns true if value is zero; otherwise false.

lerpstatic

static lerp(value1: Real | NumberLike, value2: Real | NumberLike, t: Real | NumberLike, result?: Real | null): Real

Linearly interpolates between two real values by the specified factor.

value1
The start value, returned when t is 0.
value2
The end value, returned when t is 1.
t
The interpolation factor.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The value interpolated between value1 and value2 at t.

maxstatic

static max(value1: Real | NumberLike, value2: Real | NumberLike, result?: Real | null): Real

Returns the larger of two real values.

value1
The first value.
value2
The second value.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The larger of value1 and value2.

minstatic

static min(value1: Real | NumberLike, value2: Real | NumberLike, result?: Real | null): Real

Returns the smaller of two real values.

value1
The first value.
value2
The second value.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The smaller of value1 and value2.

modulusstatic

static modulus(value1: Real | NumberLike, value2: Real | NumberLike, result?: Real | null): Real

Computes the modulus (remainder) of two real values.

value1
The dividend.
value2
The divisor.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The remainder of value1 divided by value2, with the sign of value1 (7.5 mod 2 = 1.5, −7.5 mod 2 = −1.5); NaN if value2 is 0.

multiplystatic

static multiply(value1: Real | NumberLike, value2: Real | NumberLike, result?: Real | null): Real

Multiplies two real values.

value1
The first operand.
value2
The second operand.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The product of value1 and value2.

negatestatic

static negate(value: Real | NumberLike, result?: Real | null): Real

Returns the arithmetic negation of a real value.

value
The value to negate.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The negation of value.

powerstatic

static power(baseVal: Real | NumberLike, exponent: Real | NumberLike, result?: Real | null): Real

Raises a base value to the specified exponent.

baseVal
The base.
exponent
The exponent.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns baseVal raised to the power of exponent.

remapstatic

static remap(value: Real | NumberLike, fromMin: Real | NumberLike, fromMax: Real | NumberLike, toMin: Real | NumberLike, toMax: Real | NumberLike, result?: Real | null): Real

Remaps a value from one range to another.

value
The value to remap.
fromMin
The lower bound of the source range.
fromMax
The upper bound of the source range.
toMin
The lower bound of the target range.
toMax
The upper bound of the target range.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns value mapped from [fromMin, fromMax] into [toMin, toMax]. Returns toMin if the source range has zero width.

roundstatic

static round(value: Real | NumberLike, result?: Real | null): Real

Rounds a real value to the nearest integer. Halves are rounded up (2.5 -> 3, -2.5 -> -2), like JavaScript's Math.round.

value
The value to round.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns value rounded to the nearest integer.

signstatic

static sign(value: Real | NumberLike, result?: Real | null): Real

Returns the sign of a real value as −1, 0, or 1.

value
The value to evaluate.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns −1 if value is negative, 0 if zero, or 1 if positive.

smoothstepstatic

static smoothstep(edge0: Real | NumberLike, edge1: Real | NumberLike, value: Real | NumberLike, result?: Real | null): Real

Applies a smooth Hermite interpolation between 0 and 1 over the range [edge0, edge1]. Returns 0 for values at or below edge0 and 1 for values at or above edge1.

edge0
The lower edge of the interpolation range.
edge1
The upper edge of the interpolation range.
value
The value to evaluate.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns A smoothly interpolated value in [0, 1].

sqrtstatic

static sqrt(value: Real | NumberLike, result?: Real | null): Real

Computes the square root of a real value.

value
The value to compute the square root of. Must be non-negative.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The positive square root of value.

sqrtScalestatic

static sqrtScale(value: Real | NumberLike, maxValue: Real | NumberLike, maxSize: Real | NumberLike, result?: Real | null): Real

A size whose square is proportional to the value: sqrt(value / maxValue) * maxSize. As the radius of a circle or the side of a square, the area is proportional to the value, and maxValue gets maxSize.

Values ≤ 0 give 0. Values above maxValue give more than maxSize (no clamping). NaN if maxValue is not positive and finite.

value
The value, e.g. a population.
maxValue
The value that gets maxSize, usually the largest one.
maxSize
The size for maxValue, e.g. the largest radius.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The size for value.

subtractstatic

static subtract(value1: Real | NumberLike, value2: Real | NumberLike, result?: Real | null): Real

Subtracts one real value from another.

value1
The value to subtract from.
value2
The value to subtract.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The result of value1 minus value2.

wrapstatic

static wrap(value: Real | NumberLike, min: Real | NumberLike, max: Real | NumberLike, result?: Real | null): Real

Wraps a value into the range [min, max] using modular arithmetic. Unlike clamp, values that exceed the range wrap around to the other end.

value
The value to wrap.
min
The lower bound of the range (inclusive).
max
The upper bound of the range (exclusive).
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns value wrapped into [min, max). Returns min if the range has zero width.