Mathematical Functions

Mathematical Constants

The mathematical constants that are available are

e The base of exponentials, \(e\)
pi The constant pi, \(\pi\)
gamma Euler’s Constant, \(\gamma\)

Elementary Functions

neg(x)

The negation of \(x\), i.e., \(-x\)

>>> -sin(3)
= -0.1411200080598672
frexp(x)

Splits the number \(x\) into its normalized fraction \(f\) and exponent \(e\), such that \(x = f \times 2^{e}\) and \(0.5 \le f < 1\)

>>> frexp(10)
= (5/8, 4) = (0.625, 4)
ldexp(x, e)

Computes the number \(x \times 2^{e}\)

>>> ldexp(0.625, 4)
= 10
hex(x)

Converts the number \(x\) into its hex form

>>> hex(253)
= 0xfd
bin(x)

Converts the number \(x\) into its two’s complement binary form

>>> bin(253)
= 0b11111101
abs(x)

Returns the magnitude of the number \(x\), i.e., \(|x|\)

>>> abs(-4)
= 4
sqr(x)

Returns the square of the number \(x\), i.e., \(x^2\)

>>> sqr(-4)
= 16
sqrt(x)

Returns the square root of the number \(x\), i.e., \(\sqrt{x}\)

>>> sqrt(16)
= 4
cb(x)

Returns the cube of the number \(x\), i.e., \(x^3\)

>>> cb(-4)
= -64
cbrt(x)

Returns the cube root of the number \(x\), i.e., \(\sqrt[3]{x}\)

>>> cbrt(-64)
= -4
rad(x)

Returns the radians form of the number \(x\). Note: Assumes number is in degrees.

>>> rad(180)
= 3.141592653589793
deg(x)

Returns the degrees form of the number \(x\). Note: Assumes number is in radians.

>>> deg(pi)
= 180
hypot(a...)

Computes euclidean distance for the variable number of arguments passed in. For two and three numbers, it computes it in a way that avoids overflow.

>>> hypot(3, 4)
= 5
>>> hypot(3, 4, 5)
= 7.071067811865476
>>> hypot(3, 4, 5, 6, 7, 8, 9)
= 16.73320053068151
fcmp(x, y, tol=1e-8)

Determines whether \(x\) and \(y\) are approximately equal with relative tolerance \(tol\). If they are approximately equal, the function returns 0. Otherwise, if \(x < y\), the function returns \(-1\), or if \(x > y\), the function returns \(+1\).

>>> fcmp(sin(3 + pi/2), cos(3))
= 0
>>> fcmp(1, 1.0001)
= -1
>>> fcmp(1, 1.0001, tol=0.1)
= 0
>>> fcmp(1.0001, 1)
= 1
>>> fcmp(1.0001, 1, tol=0.1)
= 0
gcd(a...)

Determines greatest common denominator among all passed in numbers.

>>> gcd(20, 100)
= 20
>>> gcd(2, 20, 100)
= 2
lcm(a...)

Determines least common multiple among all passed in numbers.

>>> lcm(20, 100)
= 100
>>> lcm(3, 7, 13)
= 273