Math is the class for all mathematical constants and functions. Most of these functions take Float arguments and return Float values, unless specified otherwise.

Constants

E

The famous \(e\) constant.

IO.println(Math.E) #> 2.718281828459

INV_PI, INV_PI2

Constants for \(\frac{1}{\pi}\) and \(\frac{2}{\pi}\).

IO.println(Math.INV_PI) #> 0.31830988618379
IO.println(Math.INV_PI2) #> 0.63661977236758

INV_SQRT2

A constant for \(\frac{1}{\sqrt{2}}\).

IO.println(Math.INV_SQRT2) #> 0.70710678118655

LN2, LN10

Constants for \(\ln{2}\) and \(\ln{10}\).

IO.println(Math.LN2) #> 0.69314718055995
IO.println(Math.LN10) #> 2.302585092994

LOG2E, LOG10E

Constants for \(\log_2{e}\) and \(\log_{10}{e}\).

IO.println(Math.LOG2E) #> 1.442695040889
IO.println(Math.LOG10E) #> 0.43429448190325

PI

The famous \(\pi\) constant.

IO.println(Math.PI) #> 3.1415926535898

PI2, PI4

Constants for \(\frac{\pi}{2}\) and \(\frac{\pi}{4}\).

IO.println(Math.PI2) #> 1.5707963267949
IO.println(Math.PI4) #> 0.78539816339745

SQRT2

The famous \(\sqrt{2}\) constant.

IO.println(Math.SQRT2) #> 1.4142135623731

Static Methods

abs(num)

Get the absolute value of a Float or an Int, denoted \(|x|\).

IO.println(Math.abs(42))    #> 42
IO.println(Math.abs(-42))   #> 42
IO.println(Math.abs(42.0))  #> 42
IO.println(Math.abs(-42.0)) #> 42

acos(x)

Get the arc cosine of x.

IO.println(Math.acos(-1.0) == Math.PI)  #> true
IO.println(Math.acos(0.0) == Math.PI2)  #> true
IO.println(Math.acos(1.0) == 0.0)       #> true

asin(x)

Get the arc sine of x.

IO.println(Math.asin(-1.0) == -Math.PI2)  #> true
IO.println(Math.asin(0.0) == 0.0)         #> true
IO.println(Math.asin(1.0) == Math.PI2)    #> true

atan(x)

Get the arc tangent of x.

IO.println(Math.atan(-Float.INFINITY) == -Math.PI2) #> true
IO.println(Math.atan(-1.0) == -Math.PI4)            #> true
IO.println(Math.atan(0.0) == 0.0)                   #> true
IO.println(Math.atan(1.0) == Math.PI4)              #> true
IO.println(Math.atan(Float.INFINITY) == Math.PI2)   #> true

atan2(y, x)

Get the arc tangent of y/x, taking care of all the special cases.

IO.println(Math.atan2(+0.0, +1.0) == +0.0)      #> true
IO.println(Math.atan2(-0.0, +1.0) == -0.0)      #> true
IO.println(Math.atan2(+0.0, -1.0) == Math.PI)   #> true
IO.println(Math.atan2(-0.0, -1.0) == -Math.PI)  #> true

IO.println(Math.atan2(+1.0, 0.0) == Math.PI2)   #> true
IO.println(Math.atan2(-1.0, 0.0) == -Math.PI2)  #> true

IO.println(Math.atan2(+0.0, +0.0) == +0.0)      #> true
IO.println(Math.atan2(-0.0, +0.0) == -0.0)      #> true
IO.println(Math.atan2(+0.0, -0.0) == Math.PI)   #> true
IO.println(Math.atan2(-0.0, -0.0) == -Math.PI)  #> true

IO.println(Math.atan2(+1.0, Float.INFINITY) == +0.0)      #> true
IO.println(Math.atan2(-1.0, Float.INFINITY) == -0.0)      #> true
IO.println(Math.atan2(+1.0, -Float.INFINITY) == Math.PI)  #> true
IO.println(Math.atan2(-1.0, -Float.INFINITY) == -Math.PI) #> true

IO.println(Math.atan2(Float.INFINITY, 0.0) == Math.PI2)   #> true
IO.println(Math.atan2(-Float.INFINITY, 0.0) == -Math.PI2) #> true

IO.println(Math.atan2(Float.INFINITY, Float.INFINITY) == Math.PI4)          #> true
IO.println(Math.atan2(-Float.INFINITY, Float.INFINITY) == -Math.PI4)        #> true
IO.println(Math.atan2(Float.INFINITY, -Float.INFINITY) == 3.0 * Math.PI4)   #> true
IO.println(Math.atan2(-Float.INFINITY, -Float.INFINITY) == -3.0 * Math.PI4) #> true

See also: acos(x), asin(x), atan(x)

cbrt(x)

Get the cube root of x, denoted \(\sqrt[3]{x}\).

IO.println(Math.cbrt(1.0))  #> 1
IO.println(Math.cbrt(2.0))  #> 1.2599210498949
IO.println(Math.cbrt(8.0))  #> 2
IO.println(Math.cbrt(27.0)) #> 3

See also: sqrt(x)

ceil(x)

Get the ceil of x, i.e the least integer greater than or equal to \(x\), denoted \(\lceil x \rceil\).

IO.println(Math.ceil(4.2))  #> 5
IO.println(Math.ceil(-4.2)) #> -4

See also: floor(x)

clamp(v, lo, hi)

Clamps v between lo and hi. Return lo if x is less than lo. Return hi if x is greater than hi. Otherwise return x itself. v, lo and hi must be of the same type, Int or Float.

IO.println(Math.clamp(4, 0, 10))  #> 4
IO.println(Math.clamp(-6, 0, 10)) #> 0
IO.println(Math.clamp(12, 0, 10)) #> 10

See also: max(x, y), min(x, y)

cos(x)

Get the cosine of x.

IO.println(Math.cos(0.0))     #> 1
IO.println(Math.cos(Math.PI)) #> -1
IO.println(Float.almost_equals(Math.cos(-Math.PI2), 0.0)) #> true
IO.println(Float.almost_equals(Math.cos( Math.PI2), 0.0)) #> true

See also: sin(x), tan(x)

exp(x)

Get the exponential value of x, denoted \(e^x\).

IO.println(Math.exp(0.0))       #> 1
IO.println(Math.exp(1.0))       #> 2.718281828459
IO.println(Math.exp(Math.LN2))  #> 2
IO.println(Math.exp(Math.LN10)) #> 10
IO.println(Math.exp(-Float.INFINITY)) #> 0

See also: log(x), exp2(x)

exp2(x)

Get the base-2 exponential value of x, i.e. \(2^x\).

IO.println(Math.exp2(0.0)) #> 1
IO.println(Math.exp2(1.0)) #> 2
IO.println(Math.exp2(2.0)) #> 4
IO.println(Math.exp2(3.0)) #> 8
IO.println(Math.exp2(-Float.INFINITY)) #> 0

See also: log2(x), exp(x)

floor(x)

Get the floor of x, i.e the greatest integer less than or equal to \(x\), denoted \(\lfloor x \rfloor\).

IO.println(Math.floor(4.2))  #> 4
IO.println(Math.floor(-4.2)) #> -5

See also: ceil(x)

gcd(x, y)

Get the greatest common divisor of x and y. x and y must be of type Int.

IO.println(Math.gcd(54, 24))    #> 6
IO.println(Math.gcd(9, 28))     #> 1
IO.println(Math.gcd(-42, -56))  #> 14

See also: lcm(x, y)

hypot(x, y)

Get the length of the hypotenuse of a right-angled triangle with sides of length x and y, or the distance of the point \((x,y)\) from the origin. The value is \(\sqrt{x^2 + y^2}\).

IO.println(Math.hypot(0.0, 0.0))  #> 0
IO.println(Math.hypot(1.0, 1.0))  #> 1.4142135623731
IO.println(Math.hypot(4.0, -3.0)) #> 5

lcm(x, y)

Get the least common multiple of x and y. x and y must be of type Int.

IO.println(Math.lcm(21, 6))    #> 42
IO.println(Math.lcm(9, 7))     #> 63
IO.println(Math.lcm(-8, -10))  #> 40

See also: gcd(x, y)

log(x)

Get the natural logarithm of x, denoted \(\ln x\).

IO.println(Math.log(0.0)) #> -inf
IO.println(Math.log(1.0)) #> 0
IO.println(Math.log(Math.E)) #> 1
IO.println(Math.log(Float.INFINITY)) #> inf

See also: exp(x), log2(x), log10(x)

log2(x)

Get the binary logarithm of x, i.e. the base 2 logarithm of x, denoted \(\log_2 x\).

IO.println(Math.log2(0.0))    #> -inf
IO.println(Math.log2(1.0))    #> 0
IO.println(Math.log2(2.0))    #> 1
IO.println(Math.log2(1024.0)) #> 10
IO.println(Math.log2(Float.INFINITY)) #> inf

See also: exp2(x), log(x), log10(x)

log10(x)

Get the common logarithm of x, i.e. the base 10 logarithm of x, denoted \(\log_{10} x\).

IO.println(Math.log10(0.0))    #> -inf
IO.println(Math.log10(1.0))    #> 0
IO.println(Math.log10(10.0))   #> 1
IO.println(Math.log10(1000.0)) #> 3
IO.println(Math.log10(Float.INFINITY)) #> inf

See also: log(x), log2(x)

max(x, y)

Get the maximum value between x and y. x and y must be of the same type, Int or Float. If one of the argument is not a number, the other is returned.

IO.println(Math.max(1, 2))        #> 2
IO.println(Math.max(-1, -2))      #> -1
IO.println(Math.max(1.0, 2.0))    #> 2
IO.println(Math.max(-1.0, -2.0))  #> -1

min(x, y)

Get the minimum value between x and y. x and y must be of the same type, Int or Float. If one of the argument is not a number, the other is returned.

IO.println(Math.min(1, 2))        #> 1
IO.println(Math.min(-1, -2))      #> -2
IO.println(Math.min(1.0, 2.0))    #> 1
IO.println(Math.min(-1.0, -2.0))  #> -2

pow(x, y)

Get the value of x raised to the power of y, denoted \(x^y\). x and y must be of the same type, Int or Float.

IO.println(Math.pow(3.0, 2.0))  #> 9
IO.println(Math.pow(4.0, -1.0)) #> 0.25

IO.println(Math.pow(3, 2))  #> 9
IO.println(Math.pow(4, -1)) #> 0

round(x)

Round x to the nearest integer, but round halfway cases away from zero.

IO.println(Math.round(4.2)) #> 4
IO.println(Math.round(4.5)) #> 5
IO.println(Math.round(4.8)) #> 5
IO.println(Math.round(-4.2)) #> -4
IO.println(Math.round(-4.5)) #> -5
IO.println(Math.round(-4.8)) #> -5

See also: trunc(x)

sign(num)

Get the sign of a Float or an Int. Return -1, 0 or 1 if num is negative, zero or positive respectively.

IO.println(Math.sign(-4.2)) #> -1
IO.println(Math.sign(0.0))  #> 0
IO.println(Math.sign(4.2))  #> 1

IO.println(Math.sign(-42))  #> -1
IO.println(Math.sign(0))    #> 0
IO.println(Math.sign(42))   #> 1

sin(x)

Get the sine of x.

IO.println(Math.sin(-Math.PI2)) #> -1
IO.println(Math.sin(0.0))       #> 0
IO.println(Math.sin(Math.PI2))  #> 1
IO.println(Float.almost_equals(Math.sin(Math.PI), 0.0)) #> true

See also: cos(x), tan(x)

sqrt(x)

Get the square root of x, denoted \(\sqrt{x}\).

IO.println(Math.sqrt(1.0)) #> 1
IO.println(Math.sqrt(2.0)) #> 1.4142135623731
IO.println(Math.sqrt(4.0)) #> 2
IO.println(Math.sqrt(9.0)) #> 3

See also: cbrt(x)

tan(x)

Get the tangent of x.

IO.println(Math.tan(0.0)) #> 0
IO.println(Float.almost_equals(Math.tan(Math.PI), 0.0)) #> true

See also: cos(x), sin(x)

trunc(x)

Round x to the nearest integer value that is not larger in magnitude than x.

IO.println(Math.trunc(4.2))  #> 4
IO.println(Math.trunc(4.8))  #> 4
IO.println(Math.trunc(-4.2)) #> -4
IO.println(Math.trunc(-4.8)) #> -4

See also: round(x)