Back to Slint

Math

docs/astro/src/content/docs/reference/global-functions/math.mdx

1.17.13.4 KB
Original Source

import Link from '@slint/common-files/src/components/Link.astro';

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

slint
// 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

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

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

T type Many of the math functions can be used with any <Link type="NumericTypes" label="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.

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

ceil(float) -> int

Returns the value rounded up to the nearest integer.

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

floor(float) -> int

Returns the value rounded down to the nearest integer.

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

round(float) -> int

Return the value rounded to the nearest integer

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

sign(float) -> float

Returns 1 or -1, indicating the sign of the number passed as argument. Returns 1 if the input is 0 or -0.

slint
Math.sign(10); // returns 1
Math.sign(-30); // returns -1
Math.sign(0); // returns 1

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

ln(float) -> float

Return the natural log of the value. Same as log(e, x)

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.

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

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

exp(float, float) -> float

Return the value of the e raised to the x

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)).