kex docs Standard Library

Math

module Math

The Math module provides standard mathematical constants and functions. All trigonometric functions operate in radians. A Kex Float is always finite, so a domain error (Math.sqrt(-1.0)) or an overflow (Math.exp(1000.0)) raises rather than producing NaN or Infinity — the same rule BEAM enforces, where those two values cannot exist at all.

PI constant

Math.PI : Float Ratio of a circle's circumference to its diameter.

Examples
Math.PI   # => 3.141592653589793

E constant

Math.E : Float Base of the natural logarithm.

Examples
Math.E   # => 2.718281828459045

sqrt

sqrt(x: Number) -> Float

Square root of x. Raises for negative input, which has no real root.

Parameters
x
Examples
Math.sqrt(9.0)    # => 3.0
Math.sqrt(-1.0)   # raises: Math.sqrt: undefined result (NaN)

cbrt

cbrt(x: Number) -> Float

Cube root of x.

Parameters
x
Examples
Math.cbrt(27.0)   # => 3.0

sin

sin(x: Number) -> Float

Sine of x (radians).

Parameters
x
Examples
Math.sin(Math.PI / 2.0)   # => 1.0

cos

cos(x: Number) -> Float

Cosine of x (radians).

Parameters
x
Examples
Math.cos(0.0)   # => 1.0

tan

tan(x: Number) -> Float

Tangent of x (radians).

Parameters
x
Examples
Math.tan(Math.PI / 4.0)   # => 1.0

asin

asin(x: Number) -> Float

Inverse sine (arc sine) of x, result in radians.

Parameters
x
must be in [-1, 1]
Examples
Math.asin(1.0)   # => 1.5707963267948966

acos

acos(x: Number) -> Float

Inverse cosine (arc cosine) of x, result in radians.

Parameters
x
must be in [-1, 1]
Examples
Math.acos(1.0)   # => 0.0

atan

atan(x: Number) -> Float

Inverse tangent (arc tangent) of x, result in radians in (-π/2, π/2).

Parameters
x
Examples
Math.atan(1.0)   # => 0.7853981633974483

atan2

atan2(y: Number, x: Number) -> Float

Two-argument arc tangent. Returns the angle of vector (x, y) in (-π, π]. Handles the sign of both arguments to place the result in the correct quadrant.

Parameters
y
x
Examples
Math.atan2(1.0, 1.0)    # => 0.7853981633974483
Math.atan2(1.0, -1.0)   # => 2.356194490192345

sinh

sinh(x: Number) -> Float

Hyperbolic sine.

Parameters
x

cosh

cosh(x: Number) -> Float

Hyperbolic cosine.

Parameters
x

tanh

tanh(x: Number) -> Float

Hyperbolic tangent.

Parameters
x

log

log(x: Number) -> Float
log(x: Number, base: Number) -> Float

Natural logarithm of x (base e). With two arguments, computes log base base.

Parameters
x
base
(optional)
Examples
Math.log(Math.E)     # => 1.0
Math.log(8.0, 2.0)   # => 3.0

log2

log2(x: Number) -> Float

Base-2 logarithm of x.

Parameters
x
Examples
Math.log2(8.0)   # => 3.0

log10

log10(x: Number) -> Float

Base-10 logarithm of x.

Parameters
x
Examples
Math.log10(1000.0)   # => 3.0

exp

exp(x: Number) -> Float

e raised to the power x.

Parameters
x
Examples
Math.exp(1.0)   # => 2.718281828459045

pow

pow(x: Number, y: Number) -> Float

x raised to the power y.

Parameters
x
y
Examples
Math.pow(2.0, 10.0)   # => 1024.0

abs

abs(x: Number) -> Number

Absolute value of x.

Parameters
x

floor

floor(x: Number) -> Integer

Floor of x — largest integer not greater than x.

Parameters
x

ceil

ceil(x: Number) -> Integer

Ceiling of x — smallest integer not less than x.

Parameters
x

hypot

hypot(x: Number, y: Number) -> Float

Euclidean distance — equivalent to +sqrt(x*x + y*y)+ but avoids intermediate overflow for large values.

Parameters
x
y
Examples
Math.hypot(3.0, 4.0)   # => 5.0