math.mtr

native range() native space() native sin() native cos() native tan() native asin() native acos() native atan() native random() native round() native floor() native ceil() native abs() native sqrt() native root() native log() native ln() native toNum() native toNumber() native toStr() native identity() PI = 3.1415926535897932384626433832795028841971 E = 2.71828182845904523536028747135266249775724709369995 export [ range, space, sin, cos, tan, asin, acos, atan, random, round, floor, ceil, abs, sqrt, root, log, ln, PI, E, toNum, toNumber, toStr, identity ] as math

range

The range method will create a list of integers from a start value to an end value. If a third parameter is given, the list will only include every n-th value.

range(1, 10);;;range(1, 10, 2)

space

Similar to the range method, the space method will create a list of integers from a start value to an end value. However, the list will contain evenly spaced values. The default number of values is 120, but can be changed by passing a third parameter.

space(1, 10).size();;;space(1, 100, 5)

sin, cos, tan, asin, acos, atan

These methods will calculate the sine, cosine, tangent, arcsine, arccosine and arctangent of a value in radians.

sin(1);;;cos(PI * 2);;;tan(PI / 4);;;asin(1);;;acos(0);;;atan(1)

random

The random method will return a random number between 0 and 1. The bounds of the random number can be changed by passing a start and end value as parameters. Both bounds are inclusive.

If you need integers, you can use the floor method to round down to the nearest integer. In this case, the upper bound will be exclusive, since you are rounding down.

random();;;random(1, 10);;;range(1, 10000) .map(x -> floor(random(1, 11))) .frequency() .sortKey() # or: sortKey((a, b) -> a - b)

round, floor, ceil

The rounding methods work as you would expect. The round method will round to the nearest integer, the floor method will round down to the nearest integer and the ceil method will round up to the nearest integer.

round(3.14);;;floor(3.14);;;ceil(3.14)

abs

The abs method will return the absolute value of a number.

abs(3);;;abs(-3)

sqrt, root

The sqrt method will calculate the square root of a number. The root method will calculate the n-th root of a number.

sqrt(9);;;root(8, 3);;;root(16, 4)

log, ln

The log method will calculate the logarithm of a number with a given base. The ln method will calculate the natural logarithm of a number.

log(100, 10);;;log(10, 10);;;ln(E)

toNumber

The toNumber method will convert a string to a number. If the string cannot be converted, an error will be thrown.

Constants

The math module also contains the constants PI and E.

PI;;;E