Math
This section describes how to use mathematical operators and functions.
Operators
Section titled “Operators”Math operators follow the PEMDAS rule order: Parenthesis first, then Exponents, then Multiplication or Division, then Addition or Subtraction.
Implicit casting is used for math operations between integers and floats by converting both the integer and float to a f64 first. Division will cast both operands to f64 first.
println( 1 + 2.2 ); // 3.200000 println( 5.4 - 8 ); // -2.600000 println( 2 * 3.3 ); // 6.600000 println( 10 / 2 ); // 5 println( 2 ^ 3 ); // 8
i32 a = 2 * (1 + 3); println(a); // 8
i32 b = 4 / 2 * 7; println(b); // 14
f32 c = (1 + 4 * 6 - 2 * 8) / 3; println(c); // 3
i32 d = -6 + 3 ^ 2 * 2; println(d); // 12
f32 e = c * d / a + b; println(e); // 18.5Modulus
Section titled “Modulus”The modulus operator ”%” is used to return the remainder from division. If both operands are integers then integer modulus will be used. If one or both are floats, then floating point modulus with f64 will be used.
i32 a = 7 % 5; // 2 f32 b = 7.7 % 5; // 2.7 f32 c = 7.7 % 5.1; // 2.6Absolute Value
Section titled “Absolute Value”Use the abs() function to get the absolute value of a number.
f32 a = -5.89; f32 b = abs(a); println(a); // -5.890000 println(b); // 5.890000
f64 x = 0.1; f64 y = -0.006; f64 z = abs(y - x); println(z); // 0.106
i32 c = 5; i32 d = 33; i32 e = abs(c - d); println(e); // 28Trigonometry Operations
Section titled “Trigonometry Operations”Tenta supports standard trigonometry functions such as sin, cos, tan, etc. You can also use the “pi” keyword for the number pi.
Quickly convert between radians and degrees using rad2deg() and deg2rad().
println(pi); // 3.141593
f32 x = 90; f32 y = deg2rad(x); // 1.570796 f32 z = rad2deg(y); // 90 f32 w = rad2deg(pi); // 180
f32 a = cos( 1.1 ); // 0.453596 f32 b = sin( 1.1 ); // 0.891207 f32 c = tan( 1.1 ); // 1.964760
f32 d = acos( 0.453596 ); // 1.1 f32 e = asin( 0.891207 ); // 1.1 f32 f = atan( 1.964760 ); // 1.1
f32 g = atan2( 30, 10 ); // 1.249046 f32 h = rad2deg(g); // 71.565048Math Utilities
Section titled “Math Utilities”Tenta supports many common mathematical utility functions as follows.
// return -1.0 if a number is negative, or else return +1.0. f32 a = sgn(-5); // -1.0 f32 b = sgn(6.7); // 1.0
// round a number downward. f32 c = floor(3.7); // 3.0 f32 d = floor(-3.7); // -4.0
// round a number upward. f32 e = ceil(2.1); // 3.0 f32 f = ceil(-2.1); // -2.0
// round a number down if fractional part is less than 0.5, or else round up. f32 g = round(7.49); // 7.0 f32 h = round(7.50); // 8.0
// get the fractional part of a number. f32 i = fract(7.854931); // 0.854931
// take the square root of a number. f32 j = sqrt(2); // 1.414214
// raise a number to a power, same as a ^ b. i32 k0 = pow(3, 2); // 9.0 f32 k1 = pow(3.3, 2); // 10.89 f32 k2 = pow(3, 2.2); // 11.211579 // Warning: if a power operation creates a complex number, the compiler may crash (as of 2025-08-13).
// raise Euler's Number "e" (2.718282) to a power. f32 e0 = exp(1.0); // 2.718282 f32 e1 = exp(2); // 7.389056
// get the natural logarithm of a number. f32 n0 = log(exp(1.1)); // 1.1 f32 n1 = log(7.7); // 2.041220
// get the log base 10 of a number. f32 t0 = log10(100); // 2 f32 t1 = log10(0.1); // -1
// get a logarithm using a specified base. f32 base = 2; f32 t2 = log(78.6) / log(base); // 6.296457
// return the minimum between two values. f32 p0 = min(5.7, 5.8); // 5.7 f32 p1 = min(-5.7, -5.8); // -5.8
// return the maximum between two values. f32 q0 = max(5.7, 5.8); // 5.8 f32 q1 = max(-5.7, -5.8); // -5.7
// linear mix between two numbers using a ratio from 0 to 1. // Signature: f64 f = mix(f64 a, f64 b, f64 ratio) f32 m0 = mix(1, 3, 0.5); // 2.0 f32 m1 = mix(0, 1, 0.25); // 0.25 f32 m2 = mix(-7, 3, 0.95); // 2.50
// Calculate the smoothstep function between a and b at location x. // Return value is from 0 to 1. // Signature: f64 value = smoothstep(f64 a, f64 b, f64 x) f32 f = smoothstep(1, 2, 1.7); // 0.78400
// Calculate the smootherstep function between a and b at location x. // Return value is from 0 to 1. // Signature: f64 value = smootherstep(f64 a, f64 b, f64 x) f32 f = smootherstep(1, 2, 1.7); // 0.836920
// restrict a number between two bounds. // Signature: f64 value = clamp(f64 x, f64 lower, f64 upper) f32 c0 = clamp(3, 2, 4); // 3 f32 c1 = clamp(1, 2, 4); // 2 f32 c2 = clamp(5, 2, 4); // 4
f32 c3 = clamp(-3, -4, -2); // -3 f32 c4 = clamp(-1, -4, -2); // -2 f32 c5 = clamp(-5, -4, -2); // -4