Skip to main content

Math Library Reference

The math library provides a comprehensive set of functions for numerical computation. Below is a summary table of all available functions, followed by detailed examples and their outputs.

FunctionParametersDescription
abs(x)x (Number)Returns the absolute value of x.
pow(x, y)x, y (Numbers)Returns x raised to the power of y.
sqrt(x)x (Number, ≥ 0)Returns the square root of x.
sin(x)x (Number, radians)Returns the sine of x.
cos(x)x (Number, radians)Returns the cosine of x.
tan(x)x (Number, radians)Returns the tangent of x.
round(x)x (Number)Rounds x to the nearest integer.
random()Returns a random float r in [0.0, 1.0).
random(start, end)start, end (Integers)Returns a random integer n in [start, end).
max(x, y, ...)one or more NumbersReturns the maximum value among the arguments.
min(x, y, ...)one or more NumbersReturns the minimum value among the arguments.
sum(arr)arr (Array of Numbers)Returns the sum of all elements in the array.
ceiling(x)x (Number)Returns the smallest integer ≥ x.
floor(x)x (Number)Returns the largest integer ≤ x.
log10(x)x (Number)Returns the base‑10 logarithm of x.
average(arr)arr (Array of Numbers)Returns the arithmetic mean of the array.
log(x)x (Number, > 0)Returns the natural (base e) logarithm of x.
exp(x)x (Number)Returns e raised to the power of x.
mean(arr)arr (Array of Numbers)Alias for average(arr).
mode(arr)arr (Array of any values)Returns the most frequent element in the array.
sign(x)x (Number)Returns −1 if x<0, 0 if x=0, 1 if x>0.
log2(x)x (Number)Returns the base‑2 logarithm of x.

Detailed Examples

1. abs(x)

import math

show(math.abs(-5))

Output:

5

2. pow(x, y)

import math

show(math.pow(2, 3))

Output:

8

3. sqrt(x)

import math

show(math.sqrt(9))

Output:

3

4. sin(x)

import math

// 1.5708 radians ≈ π/2
show(math.sin(1.5708))

Output:

1

5. cos(x)

import math

show(math.cos(0))

Output:

1

6. tan(x)

import math

show(math.tan(0))

Output:

0

7. round(x)

import math

show(math.round(3.6))

Output:

4

8. random()

import math

show(math.random())

Output:
(example; actual value varies)

0.7324

9. random(start, end)

import math

show(math.random(1, 5))

Output:
(example; actual value varies between 1 and 4)

3

10. max(x, y, ...)

import math

show(math.max(-1, 3, 2))

Output:

3

11. min(x, y, ...)

import math

show(math.min(-1, 3, 2))

Output:

-1

12. sum(arr)

import math

show(math.sum([1, 2, 3, 4]))

Output:

10

13. ceiling(x)

import math

show(math.ceiling(3.2))

Output:

4

14. floor(x)

import math

show(math.floor(3.8))

Output:

3

15. log10(x)

import math

show(math.log10(100))

Output:

2

16. average(arr)

import math

show(math.average([2, 4, 6]))

Output:

4

17. log(x)

import math

show(math.log(1))

Output:

0

18. exp(x)

import math

show(math.exp(1))

Output:

2.718281828459045

19. mean(arr)

import math

show(math.mean([2, 5, 5, 8]))

Output:

5

20. mode(arr)

import math

show(math.mode([1, 2, 2, 3, 3, 3, 4]))

Output:

3

21. sign(x)

import math

show(math.sign(-10))
show(math.sign(0))
show(math.sign(10))

Output:

-1
0
1

22. log2(x)

import math

show(math.log2(8))

Output:

3