math
math provides mathematical operations and functions. It requires the presence of the cast library. Expect some slight rounding errors when using functions which rely on Maclaurin series, such as pow
or tan
- and expect them to be rather slow too!
Constant functions
e
$ e -> num
e = 2.718281828459045
This returns the value of e, the base of natural logarithms.
pi
$ pi -> num
pi = 3.141592653589793
This returns the value of π.
Powers, roots and logarithms
square
$ square num -> num
square x = * x x
This returns the square of its argument.
cube
$ cube num -> num
cube x = * * x x x
This returns the cube of its argument.
_powint
$ _powint num whole -> num
_powint base index =
| 1 if == index 0
| * base _powint base - index 1 otherwise
This raises a number to a whole-number power using repeated multiplication. This is necessary for many other functions.
pow
$ pow num num -> num
pow base index = exp * index ln base
This raises a number to the power of another number using ax = ex ln(a).
_exp_maclaurin_limit
$ _exp_maclaurin_limit -> whole
_exp_maclaurin_limit = 50
This defines how many terms to sum when computing exponentials using a Maclaurin series expansion.
_exp_maclaurin
$ _exp_maclaurin num num whole whole -> num
_exp_maclaurin sum base n lim =
| sum if == n lim
| _exp_maclaurin + sum / _powint base n factorial n base + n 1 lim otherwise
This recursively exponentiates a number using the Maclaurin series expansion for ex. exp
calls this with the correct initial arguments and _exp_maclaurin_limit
defines the amount of terms to sum.
exp
$ exp num -> num
exp x = _exp_maclaurin 0 x 0 _exp_maclaurin_limit
This returns ex
. It acts as a wrapper for _exp_maclaurin
by calling it with the correct initial values to calculate ex
_ln_maclaurin_limit
$ _ln_maclaurin_limit -> whole
_ln_maclaurin_limit = 50
This defines how many terms to sum when computing natural logarithms using a Maclaurin series expansion.
_ln_maclaurin
$ _ln_maclaurin num num whole whole -> num
_ln_maclaurin sum x n lim =
| sum if == n lim
| _ln_maclaurin + sum / _powint / - x 1 + x 1 + * 2 n 1 + * 2 n 1 x + n 1 lim otherwise
This recursively computes the natural logarithm of a number using aMaclaurin series expansion for ln(x). ln
calls this with the correct initial arguments and _ln_maclaurin_limit
defines the amount of terms to sum.
ln
$ ln num -> num
ln x =
| error mathErr "Undefined result" if == x 0
| error mathErr "No real solution" if < x 0
| * 2 _ln_maclaurin 0 x 0 _ln_maclaurin_limit otherwise
This returns the natural logarithm of x
. It acts as a wrapper for _ln_maclaurin
by calling it with the correct initial values to calculate ln(x), and by performing a final necessary multiplication by 2.
Trigonometric functions
_shift_value_to_range_trig
$ _shift_value_to_range_trig num -> num
_shift_value_to_range_trig x =
| _shift_value_to_range_trig - x * 2 pi if > x pi
| _shift_value_to_range + x * 2 pi if < x - 0 pi
| x otherwise
This shifts any value to the interval [π, π] by recursively adding or subtracting 2π. This means that trigonometric operations on values shifted by this functions will give more precise results, as the Maclaurin series expansion won't have had the chance to drift as far away from the true value. This is called automatically by sin
and cos
, and by extension, tan
.
_sin_maclaurin_limit
$ _sin_maclaurin_limit -> whole
_sin_maclaurin_limit = 20
This defines how many terms to sum when computing the sine of a number using a Maclaurin series expansion.
_sin_maclaurin
$ _sin_maclaurin num num whole int whole -> num
_sin_maclaurin sum base n sign lim =
| sum if == n lim
| _sin_maclaurin + sum * sign / _powint base + * 2 n 1 factorial + * 2 n 1 base + n 1 * -1 sign lim otherwise
This recursively computes the sine of a number using the Maclaurin series expansion for sin(x). sin
calls this with the correct initial arguments and _sin_maclaurin_limit
defines the amount of terms to sum.
sin
$ sin num -> num
sin x = _sin_maclaurin 0 _shift_value_to_range_trig x 0 1 _sin_maclaurin_limit
This returns the sine of x
. It acts as a wrapper for _sin_maclaurin
by calling it with the correct initial values to calculate sin(x), and by calling _shift_value_to_range_trig
to move x
into the interval [-π, π] in order to improve accuracy.
_cos_maclaurin_limit
$ _cos_maclaurin_limit -> whole
_cos_maclaurin_limit = 20
This defines how many terms to sum when computing the cosine of a number using a Maclaurin series expansion
_cos_maclaurin
$ _cos_maclaurin num num whole int whole -> num
_cos_maclaurin sum base n sign lim =
| sum if == n lim
| _cos_maclaurin + sum * sign / _powint base * 2 n factorial * 2 n base + n 1 * -1 sign lim otherwise
>This recursively computes the cosine of a number using the Maclaurin series expansion for cos(x). cos
calls this with the correct initial arguments and _cos_maclaurin_limit
defines the amount of terms to sum.
cos
$ cos num -> num
cos x = _cos_maclaurin 0 _shift_value_to_range_trig x 0 1 _cos_maclaurin_limit
>This returns the cosine of x
. It acts as a wrapper for _cos_maclaurin
by calling it with the correct initial values to calculate cos(x), and by calling _shift_value_to_range_trig
to move x
into the interval [-π, π] in order to improve accuracy.
tan
$ tan num -> num
tan x =
| error mathErr "Undefined result" if == x / pi 2
| / sin x cos x otherwise
This returns the tangent of x
by dividing the output of sin x
by the output of cos x
.
Miscellaneous functions
factorial
$ factorial whole -> whole
factorial x =
| * x factorial - x 1 if > x 0
| 1 if == x 0
This returns the factorial of x
. It computes it recursively by multiplying x
by every natural number below it. It returns 1 if x
is 0.
abs
$ abs num -> num
abs x =
| x if >= x 0
| - 0 x otherwise
This returns the absolute value of x
.
floor
$ floor num -> int
floor x = int x
This rounds x
down to the nearest integer.
ceil
$ ceil num -> int
ceil x = int + x 1
This rounds x
up to the nearest integer.
round
$ round num -> int
round x =
| int x if < - x int x 0.5
| int + x 1 otherwise
This rounds x
either up or down to the nearest integer. It will round 0.5, 1.5, 2.5, etc. up.
gcd
$ gcd int int -> int
gcd a b =
| b if == a 0
| gcd int % b a a otherwise
This returns the GCD (Greatest Common Divisor) of a
and b
.