TruWorlds

Math

Accessed via math

The math module provides common mathematical functions and constants.

Properties

math.pi number read-only

The mathematical constant π (pi).

math.e number read-only

The mathematical constant e (Euler's number).

math.tau number read-only

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

math.huge number read-only

A value equal to the largest number in TruScript that can be represented before infinity. To get the inverse sign (approaching negative infinity), use -math.huge.

Functions

math.sin(x: number) Returns number

Returns the sine of x (in radians).

math.cos(x: number) Returns number

Returns the cosine of x (in radians).

math.tan(x: number) Returns number

Returns the tangent of x (in radians).

math.abs(x: number) Returns number

Returns the absolute value of x.

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

Returns x raised to the power of y.

math.asin(x: number) Returns number

Returns the arcsine of x (in radians).

math.acos(x: number) Returns number

Returns the arccosine of x (in radians).

math.atan(x: number) Returns number

Returns the arctangent of x (in radians).

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

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

math.sinh(x: number) Returns number

Returns the hyperbolic sine of x.

math.cosh(x: number) Returns number

Returns the hyperbolic cosine of x.

math.tanh(x: number) Returns number

Returns the hyperbolic tangent of x.

math.asinh(x: number) Returns number

Returns the inverse hyperbolic sine of x.

math.acosh(x: number) Returns number

Returns the inverse hyperbolic cosine of x.

math.atanh(x: number) Returns number

Returns the inverse hyperbolic tangent of x.

math.exp(x: number) Returns number

Returns e raised to the power of x.

math.ln(x: number) Returns number

Returns the natural logarithm of x.

math.log10(x: number) Returns number

Returns the base-10 logarithm of x.

math.log2(x: number) Returns number

Returns the base-2 logarithm of x.

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

Returns the logarithm of x to the given base.

math.sqrt(x: number) Returns number

Returns the square root of x.

math.cbrt(x: number) Returns number

Returns the cube root of x.

math.floor(x: number) Returns number

Returns the largest integer less than or equal to x.

math.ceil(x: number) Returns number

Returns the smallest integer greater than or equal to x.

math.round(x: number) Returns number

Returns the value of x rounded to the nearest integer.

math.round_digits(x: number, digits: number) Returns number

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

math.trunc(x: number) Returns number

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

math.sign(x: number) Returns number

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

math.max(a: number, b: number) Returns number

Returns a if it is greater than b, or b otherwise.

math.min(a: number, b: number) Returns number

Returns a if it is less than b, or b otherwise.

math.clamp(value: number, min: number, max: number) Returns number

Returns value clamped to the range [min, max].

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

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: number, b: number, value: number) Returns number

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.lerp_angle(from: number, to: number, t: number) Returns number

Returns the linear interpolation between the angles from and to (in radians) by t, taking the shortest path around the circle.

math.normalize_angle(angle: number) Returns number

Returns the angle (in radians) normalized to the range [-π, π].

math.angle_difference(from: number, to: number) Returns number

Returns the shortest signed angular difference (in radians) from one angle to another, in the range [-π, π].

math.deg_to_rad(degrees: number) Returns number

Converts an angle from degrees to radians.

math.rad_to_deg(radians: number) Returns number

Converts an angle from radians to degrees.

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

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.smootherstep(edge0: number, edge1: number, x: number) Returns number

Returns a smooth interpolation between 0 and 1 when x is in the range [edge0, edge1], like smoothstep but with an even smoother curve whose first and second derivatives are both zero at the edges.

math.distance(x1: number, y1: number, x2: number, y2: number) Returns number

Returns the distance between the 2D points (x1, y1) and (x2, y2).

math.distance3d(x1: number, y1: number, z1: number, x2: number, y2: number, z2: number) Returns number

Returns the distance between the 3D points (x1, y1, z1) and (x2, y2, z2).

math.magnitude(x: number, y: number) Returns number

Returns the length of the 2D vector (x, y).

math.magnitude3d(x: number, y: number, z: number) Returns number

Returns the length of the 3D vector (x, y, z).

math.wrap(value: number, min: number, max: number) Returns number

Returns value wrapped to the range [min, max). If value is less than min, it wraps around to the upper end of the range. If value is greater than or equal to max, it wraps around to the lower end of the range.

math.ping_pong(value: number, length: number) Returns number

Returns a value that oscillates between 0 and length as value 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(value: number, length: number) Returns number

Returns a value that repeats every length as value 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.move_toward(current: number, target: number, max_delta: number) Returns number

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

math.approximately(a: number, b: number, epsilon: number = 1e-10) Returns boolean

Returns true if a and b are equal to within the given epsilon, and false otherwise. Useful for comparing numbers that may carry floating-point error.

math.is_nan(value: number) Returns boolean

Returns true if value is NaN, and false otherwise.

math.is_infinite(value: number) Returns boolean

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

math.fract(x: number) Returns number

Returns the fractional (decimal) part of x.