Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

math

use Core.math

The math module provides math functions which are used very frequently in math-heavy workloads.

Function NameParameter TypesReturn TypesPossible Errors
sinf32f32No
sinf64f64No

cosf32f32No
cosf64f64No

sqrtf32f32No
sqrtf64f64No

absi32i32No
absi64i64No
absf32f32No
absf64f64No

minu32, u32u32No
minu64, u64u64No
mini32, i32i32No
mini64, i64i64No
minf32, f32f32No
minf64, f64f64No

maxu32, u32u32No
maxu64, u64u64No
maxi32, i32i32No
maxi64, i64i64No
maxf32, f32f32No
maxf64, f64f64No

error sets

These are the error sets this Core module provides.

sin

The sin function executes the sine function on the given parameter. The parameter is in radians, so you need to convert it from degrees to radians before using it.

use Core.print
use Core.math

def main():
    f32 x = sin(0.5236)
    print($"x = {x}\n");

This program will print this line to the console:

x = 0.5

cos

The cos function executes the cosine function on the given parameter. The parameter is in radians, so you need to convert it from degrees to radians before using it.

use Core.print
use Core.math

def main():
    f32 x = sin(1.0472)
    print($"x = {x}\n");

This program will print this line to the console:

x = 0.5

sqrt

The sqrt function executes the sqare root on the given parameter.

use Core.print
use Core.math

def main():
    f32 x = sqrt(9.0);
    print($"x = {x}\n");

This program will print this line to the console:

x = 3

abs

The abs function returns the absolute value of the given signed value parameter. Note that this function only has overloads for signed integers and floating point types. If the input of the integer overloads of the abs function is I32_MIN or I64_MIN then I32_MAX or I64_MAX is returned instead (one smaller than the "real" absolute value would be).

use Core.print
use Core.math

def main():
    i32 x = -2_147;
    print($"abs({x}) = {abs(x)}\n");

This program will print this line to the console:

abs(-2147) = 2147

min

The min function simply returns the minimum of two given values.

use Core.print
use Core.math

def main():
    i32 x = 121;
    i32 y = 234;
    print($"min({x}, {y}) = {min(x, y)}\n");

This program will print this line to the console:

min(121, 234) = 121

max

The max function simply returns the maximum of two given values.

use Core.print
use Core.math

def main():
    i32 x = 121;
    i32 y = 234;
    print($"max({x}, {y}) = {max(x, y)}\n");

This program will print this line to the console:

max(121, 234) = 234