Skip to content

Math

The Math namespace contains functions that are available both in the global scope and in the Math namespace.

// Using the Math namespace
x: Math.abs(-10); // sets x to 10
slint
// Using the functions via global scope. No need for 'Math' prefix.
x: abs(-10); // sets x to 10
slint

Some of the math functions can be used in a postfix style which can make the code more readable.

// Using the postfix style.
x: (-10).abs(); // sets x to 10
slint

T type Many of the math functions can be used with any numeric type such as angle, duration, float, int, length, and percent. These are represented on this page as T.

General Math Functions

abs(T) -> T

Return the absolute value, where T is a numeric type.

Math.abs(-10); // returns 10
abs(-10px); // returns 10px
(-10).abs(); // returns 10
slint

ceil(float) -> int

Returns the value rounded up to the nearest integer.

Math.ceil(4); // returns 4
Math.ceil(2.3); // returns 3
Math.ceil(-1.5); // returns -1
slint

floor(float) -> int

Returns the value rounded down to the nearest integer.

Math.floor(4); // returns 4
Math.floor(2.3); // returns 2
Math.floor(-1.5); // returns -2
slint

round(float) -> int

Return the value rounded to the nearest integer

Math.round(4.5); // returns 5
Math.round(4.4); // returns 4
Math.round(-1.2); // returns -1
slint

clamp(T, T, T) -> T

Takes a value, minimum and maximum and returns maximum if value > maximum, minimum if value < minimum, or value in all other cases.

log(float, float) -> float

Return the log of the first value with a base of the second value

min(T, T) -> T

max(T, T) -> T

Return the arguments with the minimum (or maximum) value. All arguments must be of the same numeric type.

Math.min(1, 2); // returns 1
Math.min(2, 1); // returns 1
Math.max(1, 2); // returns 2
Math.max(2, 1); // returns 2
slint

mod(T, T) -> T

Perform a modulo operation, where T is some numeric type. Returns the remainder of the euclidean division of the arguments. This always returns a positive number between 0 and the absolute value of the second value.

sqrt(float) -> float

Square root

pow(float, float) -> float

Return the value of the first value raised to the second

Trigonometric Functions

acos(float) -> angle

Returns the arccosine, or inverse cosine, of a number. The arccosine is the angle whose cosine is number.

asin(float) -> angle

Returns the arcsine, or inverse sine, of a number. The arcsine is the angle whose sine is number.

atan(float) -> angle

Returns the arctangent, or inverse tangent, of a number.

atan2(float, float) -> angle

cos(angle) -> float

sin(angle) -> float

tan(angle) -> float

The trigonometry function. Note that the should be typed with deg or rad unit (for example cos(90deg) or sin(slider.value * 1deg)).


© 2024 SixtyFPS GmbH