Math
On this page
- random
- exp
- ln
- ceil
- floor
- round
- NaN?
- Inf?
- cos
- sin
- tan
- arccos
- arcsin
- arctan
- cosh
- sinh
- tanh
- acosh
- asinh
- atanh
- pi
- e
- tau
- Inf
- NaN
- abs
- even?
- even
- odd?
- odd
- min
- max
- increment
- decrement
- negate
- pow
- sqrt
- fibo
- prime?
- divs
- log
- log2
- log10
- floordiv
- complex
- complex-add
- complex-sub
- complex-mul
- complex-conjugate
- complex-module
- complex-div
- clamp
- lerp
- dotProduct
- improvementRatioPercentage
- copySign
- radians
- degrees
- integer?
- factorial
- binomialCoeff
- permutations
- close?
- euclideanDistance
- gcd
- lcm
random
Builtin (random min max)
Compute a random number in [-2147483648, 2147483647] or in a custom range passed to the function
Author: @SuperFola
Parameters
min: optional inclusive lower boundmax: optional inclusive upper bound. Must be present ifminis passed
Example
(print (random)) # a number in [-2147483648, 2147483647]
(print (random 0 10)) # a number between 0 and 10
exp
(let exp (fun (_x) (...)))
Calculate e^number
Author: @SuperFola
Parameter
value: the Number
Example
(math:exp 1) # 2.7182...
ln
(let ln (fun (_x) (...)))
Calculate the logarithm of a number
Author: @SuperFola
Parameter
value: the Number
Example
(math:ln 1) # 0
ceil
(let ceil (fun (_x) (...)))
Get the smallest possible integer greater than the number
Author: @SuperFola
Parameter
value: the Number
Example
(math:ceil 0.2) # 1
floor
(let floor (fun (_x) (...)))
Get the smallest possible integer equal to the given number
Author: @SuperFola
Parameter
value: the Number
Example
(math:floor 1.7) # 1
round
(let round (fun (_x) (...)))
Get the smallest possible integer equal to or greater than the given number
Author: @SuperFola
Parameter
value: the Number
Example
(math:round 0.2) # 0
(math:round 0.6) # 1
NaN?
(let NaN? (fun (_x) (...)))
Check if a Number is NaN
Author: @SuperFola
Parameter
value: the Number
Example
(math:NaN? 2) # false
(math:NaN? math:NaN) # true
Inf?
(let Inf? (fun (_x) (...)))
Check if a Number if Inf
Author: @SuperFola
Parameter
value: the Number
Example
(math:Inf? 1) # false
(math:Inf? math:NaN) # false
cos
(let cos (fun (_x) (...)))
Calculate the cosinus of a number
Author: @SuperFola
Parameter
value: the Number (radians)
Example
(math:cos 0) # 1
(math:cos math:pi) # -1
sin
(let sin (fun (_x) (...)))
Calculate the sinus of a number
Author: @SuperFola
Parameter
value: the Number (radians)
Example
(math:sin 0) # 0
(math:cos (/ math:pi 2)) # 1
tan
(let tan (fun (_x) (...)))
Calculate the tangent of a number
Author: @SuperFola
Parameter
value: the Number (radians)
Example
(math:tan 0) # 0
(math:cos (/ math:pi 4)) # 1
arccos
(let arccos (fun (_x) (...)))
Calculate the arc cosinus of a number
Author: @SuperFola
Parameter
value: the Number
Example
(math:arccos 1) # 0
arcsin
(let arcsin (fun (_x) (...)))
Calculate the arc sinus of a number
Author: @SuperFola
Parameter
value: the Number
Example
(math:arcsin 1) # 1.570796326794897 (/ math:pi 2)
arctan
(let arctan (fun (_x) (...)))
Calculate the arc tangent of a number
Author: @SuperFola
Parameter
value: the Number
Example
(math:arctan 0) # 0
cosh
(let cosh (fun (_x) (...)))
Calculate the hyperbolic cosinus of a number
Author: @Gryfenfer97
Parameter
value: the Number
sinh
(let sinh (fun (_x) (...)))
Calculate the hyperbolic sinus of a number
Author: @Gryfenfer97
Parameter
value: the Number
tanh
(let tanh (fun (_x) (...)))
Calculate the hyperbolic tangent of a number
Author: @Gryfenfer97
Parameter
value: the Number
acosh
(let acosh (fun (_x) (...)))
Calculate the hyperbolic arc cosinus of a number
Author: @Gryfenfer97
Parameter
value: the Number
asinh
(let asinh (fun (_x) (...)))
Calculate the hyperbolic arc sinus of a number
Author: @Gryfenfer97
Parameter
value: the Number
atanh
(let atanh (fun (_x) (...)))
Calculate the hyperbolic arc tangent of a number
Author: @Gryfenfer97
Parameter
value: the Number
pi
(let pi <value>)
Pi value (3.14159…)
Author: @SuperFola
e
(let e <value>)
E value (2.7182…)
Author: @SuperFola
tau
(let tau <value>)
Tau, the ratio of the circumference to the radius of a circle, which is equal to 2*pi (6.28318…)
Author: @SuperFola
Inf
(let Inf <value>)
Float infinite value
Author: @SuperFola
NaN
(let NaN <value>)
Float not-a-number value
Author: @SuperFola
abs
(let abs (fun (_x) (...)))
Return the absolute value of a number
Author: @rstefanic
Parameter
_x: the number to get the absolute value of
even?
(let even? (fun (_n) (...)))
Return true if the number is even, false otherwise
Author: @rstefanic
Parameter
_n: the number
even
(let even <value>)
Return true if the number is even, false otherwise
Note: Deprecated, use even?
Author: @rstefanic
Parameter
_n: the number
odd?
(let odd? (fun (_n) (...)))
Return true if the number is odd, false otherwise
Author: @rstefanic
Parameter
_n: the number
odd
(let odd <value>)
Return true if the number is odd, false otherwise
Note: Deprecated, use odd?
Author: @rstefanic
Parameter
_n: the number
min
(let min (fun (_a _b) (...)))
Get the minimum between two numbers
Author: @rstefanic
Parameters
_a: the first number_b: the second number
max
(let max (fun (_a _b) (...)))
Get the maximum between two numbers
Author: @rstefanic
Parameters
_a: the first number_b: the second number
increment
(let increment (fun (_x) (...)))
Increment a given number by 1
Author: @SuperFola
Parameter
_x: number
decrement
(let decrement (fun (_x) (...)))
Decrement a given number by 1
Author: @SuperFola
Parameter
_x: number
negate
(let negate (fun (_x) (...)))
Multiply a number by -1, turning positive numbers negative and negative numbers positive
Author: @SuperFola
pow
(let pow (fun (_x _a) (...)))
Get a number to a given power
Note: Note that it’s defined as exp(a * ln(x)), thus won’t work for negative numbers
Author: @SuperFola
Parameters
_x: the number to pow_a: the exponent
sqrt
(let sqrt (fun (_x) (...)))
Get the square root of a number
Note: Square roots can’t be taken for negative numbers for obvious reasons.
Author: @SuperFola
Parameter
_x: the number
fibo
(let fibo (fun (n) (...)))
Run the fibonacci function on a number
Author: @SuperFola
Parameter
n: the number
prime?
(let prime? (fun (n) (...)))
Check if a given number is prime
Author: @SuperFola
Parameter
n: the number
divs
(let divs (fun (n) (...)))
Returns the list of a number’s divisors
Author: @Wafelack
Parameter
n: the number
Example
(math:divs 6) # Returns [1 2 3 6]
log
(let log (fun (x n) (...)))
Returns the logarithm base n of a number
Author: @Gryfenfer97
Parameters
x: the numbern: the base
Example
(math:log 81 3) # Returns 4
log2
(let log2 (fun (x) (...)))
Returns the logarithm base 2 of a number
Author: @Gryfenfer97
Parameter
x: the number
Example
(math:log2 128) # Returns 7
log10
(let log10 (fun (x) (...)))
Returns the logarithm base 10 of a number
Author: @Gryfenfer97
Parameter
x: the number
Example
(math:log10 1000) # Returns 3
floordiv
(let floordiv (fun (a b) (...)))
Returns the quotient of the euclidean division of a and b
Author: @fabien-zoccola
Parameters
a: the dividendb: the divisor
Example
(math:floordiv 14 6) # Returns 2
complex
(let complex (fun (real imag) (...)))
Create a complex number
Author: @SuperFola
Parameters
real: the real part of the complex numberimag: the imaginary value
Example
(let c (math:complex 1 2))
(print c.real " " c.imag) # 1 2
complex-add
(let complex-add (fun (_c0 _c1) (...)))
Compute the addition of two complex number
Author: @SuperFola
Parameters
_c0: the first complex number_c1: the second complex number
Example
(let c (math:complex-add (math:complex 1 2) (math:complex 3 4)))
(print c.real " " c.imag) # 4 6
complex-sub
(let complex-sub (fun (_c0 _c1) (...)))
Compute the subtraction of two complex number
Author: @SuperFola
Parameters
_c0: the first complex number_c1: the second complex number
Example
(let c (math:complex-sub (math:complex 1 2) (math:complex 3 4)))
(print c.real " " c.imag) # -2 -2
complex-mul
(let complex-mul (fun (_c0 _c1) (...)))
Compute the multiplication of two complex number
Author: @SuperFola
Parameters
_c0: the first complex number_c1: the second complex number
Example
(let c (math:complex-mul (math:complex 1 2) (math:complex 3 4)))
(print c.real " " c.imag) # -5 10
complex-conjugate
(let complex-conjugate (fun (_c) (...)))
Compute the conjugate of a complex number
Author: @SuperFola
Parameter
_c: the complex number
Example
(let c (math:complex-conjugate (math:complex 1 2)))
(print c.real " " c.imag) # 1 -2
complex-module
(let complex-module (fun (_c) (...)))
Compute the module of a complex number
Author: @SuperFola
Parameter
_c: the complex number
Example
(let c (math:complex-module (math:complex 1 2)))
(print c) # 2.2360679774997896964...
complex-div
(let complex-div (fun (_c0 _c1) (...)))
Compute the division of two complex number
Author: @SuperFola
Parameters
_c0: the first complex number_c1: the second complex number
Example
(let c (math:complex-div (math:complex 1 2) (math:complex 3 4)))
(print c.real " " c.imag) # 0.44 0.08
clamp
(let clamp (fun (_x _min _max) (...)))
Limit a given value to a range
Author: @SuperFola
Parameters
_x: value to limit_min: minimum_max: maximum
Example
(print (math:clamp 5 0 2)) # 2
(print (math:clamp 6 0 10)) # 6
lerp
(let lerp (fun (_x _v0 _v1) (...)))
Linearly interpolate a value in [0; 1] between two bounds
Author: @SuperFola
Parameters
_x: value to interpolate (must be between 0 and 1)_v0: lower bound_v1: upper bound
Example
(print (math:lerp 0.22 15 132)) # 40.74
dotProduct
(let dotProduct (fun (_v1 _v2) (...)))
Compute the dot product of two vectors
Note: The vectors must have the same length
Author: @SuperFola
Parameters
_v1: vector 1_v2: vector 2
Example
(print (math:dotProduct [1 2 3] [4 5 6])) # 32
improvementRatioPercentage
(let improvementRatioPercentage (fun (_a _b) (...)))
Compute a percentage of how much b is better than a
Note: Returns a positive number when _a is bigger than _b, negative otherwise
Author: @SuperFola
Parameters
_a: the base_b: new measure to compare
Example
(let base 55) # something takes 55ms to run
(let new 43) # now it takes 43ms
(print (math:improvementRatioPercentage base new)) # 27.9069767442
# 'base' is 27%~ slower than 'new'
copySign
(let copySign (fun (_x) (...)))
Copy the sign of a given number
Author: @SuperFola
Parameter
_x: number
Example
(print (math:copySign 5)) # 1
(print (math:copySign -3)) # -1
radians
(let radians (fun (_degrees) (...)))
Convert an angle in degrees to radians
Author: @SuperFola
Parameter
_degrees: angle in degrees
Example
(print (math:radians 90)) # pi/2
degrees
(let degrees (fun (_radians) (...)))
Convert an angle in radians to degrees
Author: @SuperFola
Parameter
_radians: angle in radians
Example
(print (math:radians (/ math:pi 2))) # 90
integer?
(let integer? (fun (_x) (...)))
Check if a given number is an integer
Author: @SuperFola
Parameter
_x: number
Example
(print (math:integer 1)) # true
(print (math:integer 1.000001)) # false
factorial
(let factorial (fun ((mut _n)) (...)))
Compute the factorial of a number
Author: @SuperFola
Parameter
_n: integer
Example
(print (math:factorial 5)) # 120
binomialCoeff
(let binomialCoeff (fun (_n _k) (...)))
Compute the binomial coefficient (n k)
Note: Evaluates to n! / (k! * (n - k)!) when k <= n and evaluates to zero when k > n.
Author: @SuperFola
Parameters
_n: total number of items_k: number of items that will be picked
permutations
(let permutations (fun (_n _k) (...)))
Compute the number of ways to choose k items from n items without repetition and with order
Note: Evaluates to n! / (n - k)! when k <= n and evaluates to zero when k > n.
Author: @SuperFola
Parameters
_n: total number of items_k: number of items to pick
close?
(let close? (fun (_n _target) (...)))
Compare two real numbers and return true if the first one is near the second one (1e-7 precision)
Author: @SuperFola
Parameters
_n: first number_target: target number
euclideanDistance
(let euclideanDistance (fun (_a _b) (...)))
Compute the euclidean distance between two vectors of the same size
Author: @SuperFola
Parameters
_a: first vector, eg [1 4]_b: second vector, eg [5 8]
Example
(let v1 [1 2 3])
(let v2 [7 9 8])
(print (math:euclideanDistance v1 v2)) # 10.488088481701517
gcd
(let gcd (fun (_a _b) (...)))
Compute the greatest common divisor of two numbers
Author: @SuperFola
Parameters
_a: number_b: number
Example
(print (math:gcd 48 18)) # 6
lcm
(let lcm (fun (_a _b) (...)))
Compute the least common multiplier of two numbers
Author: @SuperFola
Parameters
_a: number_b: number
Example
(print (math:lcm 4 6)) # 12