TruWorlds

Math

Accessed via math

The math module provides common mathematical functions and constants.

Properties

math.pi double read-only

The mathematical constant π (pi).

math.e double read-only

The mathematical constant e (Euler's number).

math.tau double read-only

The mathematical constant τ (tau), equal to 2π.

math.huge double read-only

A value equal to the largest number in TruScript that can be represented before infinity.

Functions

math.sin(x: double) Returns double

Returns the sine of x (in radians).

math.cos(x: double) Returns double

Returns the cosine of x (in radians).

math.tan(x: double) Returns double

Returns the tangent of x (in radians).

math.abs(x: double) Returns double

Returns the absolute value of x.

math.pow(x: double, y: double) Returns double

Returns x raised to the power of y.

math.asin(x: double) Returns double

Returns the arcsine of x (in radians).

math.acos(x: double) Returns double

Returns the arccosine of x (in radians).

math.atan(x: double) Returns double

Returns the arctangent of x (in radians).

math.atan2(y: double, x: double) Returns double

Returns the arctangent of y/x (in radians), using the signs of both arguments to determine the quadrant.

math.sinh(x: double) Returns double

Returns the hyperbolic sine of x.

math.cosh(x: double) Returns double

Returns the hyperbolic cosine of x.

math.tanh(x: double) Returns double

Returns the hyperbolic tangent of x.

math.asinh(x: double) Returns double

Returns the inverse hyperbolic sine of x.

math.acosh(x: double) Returns double

Returns the inverse hyperbolic cosine of x.

math.atanh(x: double) Returns double

Returns the inverse hyperbolic tangent of x.

math.exp(x: double) Returns double

Returns e raised to the power of x.

math.ln(x: double) Returns double

Returns the natural logarithm of x.

math.log10(x: double) Returns double

Returns the base-10 logarithm of x.

math.log2(x: double) Returns double

Returns the base-2 logarithm of x.

math.log(x: double, base: double) Returns double

Returns the logarithm of x to the given base.

math.sqrt(x: double) Returns double

Returns the square root of x.

math.cbrt(x: double) Returns double

Returns the cube root of x.

math.floor(x: double) Returns double

Returns the largest integer less than or equal to x.

math.ceil(x: double) Returns double

Returns the smallest integer greater than or equal to x.

math.round(x: double) Returns double

Returns the value of x rounded to the nearest integer.

math.round_digits(x: double, num_digits: int) Returns double

Returns the value of x rounded to the specified number of decimal places.

math.trunc(x: double) Returns double

Returns the integer part of x, removing any fractional digits.

math.sign(x: double) Returns double

Returns the sign of x: -1 if x is negative, 0 if x is zero, and 1 if x is positive.

math.max(x: double, y: double) Returns double

Returns x if it is greater than y, or y if it is greater than x.

math.min(x: double, y: double) Returns double

Returns x if it is less than y, or y if it is less than x.

math.clamp(x: double, min_value: double, max_value: double) Returns double

Returns x clamped to the range [min_value, max_value].

math.lerp(a: double, b: double, t: double) Returns double

Returns the linear interpolation between a and b by t. If t is outside the range [0, 1], the result is extrapolated.

math.inverse_lerp(a: double, b: double, value: double) Returns double

Returns the parameter t that produces the given value when linearly interpolating between a and b. In other words, this solves for t in the equation value = lerp(a, b, t). If value is outside the range [a, b], the result is extrapolated.

math.deg_to_rad(degrees: double) Returns double

Converts an angle from degrees to radians.

math.rad_to_deg(radians: double) Returns double

Converts an angle from radians to degrees.

math.smoothstep(edge0: double, edge1: double, x: double) Returns double

Returns a smooth interpolation between 0 and 1 when x is in the range [edge0, edge1]. The result is 0 when x is less than or equal to edge0, and 1 when x is greater than or equal to edge1. The function uses a cubic Hermite interpolation to ensure smoothness at the edges.

math.wrap(x: double, min_value: double, max_value: double) Returns double

Returns x wrapped to the range [min_value, max_value). If x is less than min_value, it wraps around to the upper end of the range. If x is greater than or equal to max_value, it wraps around to the lower end of the range.

math.ping_pong(x: double, length: double) Returns double

Returns a value that oscillates between 0 and length as x increases. The function is similar to the wrap function, but it "bounces" back and forth between the two ends of the range instead of wrapping around.

math.repeat(x: double, length: double) Returns double

Returns a value that repeats every length as x increases. The function is similar to the wrap function, but it always wraps around to the lower end of the range instead of bouncing back and forth.

math.is_nan(x: double) Returns bool

Returns true if x is NaN, and false otherwise.

math.is_infinite(x: double) Returns bool

Returns true if x is positive or negative infinity, and false otherwise.

math.fract(x: double) Returns double

Returns the fractional (decimal) part of x.

math.move_toward(current: double, target: double, max_delta: double) Returns double

Returns the value that is moved from current towards target by the maximum amount of max_delta.