math

Mathematical functions and constants. Every function returns a finite Float. A function called outside its domain, or whose result is too large for a Float, raises a runtime error that names the function and the argument: math.sqrt of a negative number, math.log / math.log10 of a number that is not positive, math.asin / math.acos outside -1..1, and math.exp / math.pow overflow.

Summary

NameSignatureDescription
acos(Float) -> FloatArccosine (radians)
asin(Float) -> FloatArcsine (radians)
atan(Float) -> FloatArctangent (radians)
atan2(Float, Float) -> FloatTwo-argument arctangent
cos(Float) -> FloatCosine
eFloatEuler’s number (2.71828…)
exp(Float) -> FloatExponential (e^x)
log(Float) -> FloatNatural logarithm (ln)
log10(Float) -> FloatBase-10 logarithm
piFloatPi (3.14159…)
pow(Float, Float) -> FloatExponentiation
random() -> FloatRandom float in [0.0, 1.0)
sin(Float) -> FloatSine
sqrt(Float) -> FloatSquare root
tan(Float) -> FloatTangent

math.acos

math.acos(x: Float) -> Float

Returns the arccosine of x in radians. Raises a runtime error for inputs outside -1..1.

import math

fn main() {
  let angle = math.acos(1.0)
  println(angle) -- 0.0
}
open in playground

math.asin

math.asin(x: Float) -> Float

Returns the arcsine of x in radians. Raises a runtime error for inputs outside -1..1.

import math

fn main() {
  let angle = math.asin(1.0)
  println(angle) -- 1.5707... (pi/2)
}
open in playground

math.atan

math.atan(x: Float) -> Float

Returns the arctangent of x in radians.

import math

fn main() {
  println(math.atan(1.0)) -- 0.7853... (pi/4)
}
open in playground

math.atan2

math.atan2(y: Float, x: Float) -> Float

Returns the angle in radians between the positive x-axis and the point (x, y). Handles all quadrants correctly.

import math

fn main() {
  println(math.atan2(1.0, 1.0)) -- 0.7853... (pi/4)
}
open in playground

math.cos

math.cos(x: Float) -> Float

Returns the cosine of x (in radians).

import math

fn main() {
  println(math.cos(0.0)) -- 1.0
  println(math.cos(math.pi)) -- -1.0
}
open in playground

math.e

math.e: Float

Euler’s number, approximately 2.718281828459045. This is a constant, not a function.

import math

fn main() {
  println(math.e) -- 2.718281828459045
}
open in playground

math.exp

math.exp(x: Float) -> Float

Returns e raised to the power of x. Raises a runtime error when the result is too large for a Float (x above about 709.78).

import math

fn main() {
  let e_val = math.exp(1.0)
  println(e_val) -- 2.718281828459045
}
open in playground

math.log

math.log(x: Float) -> Float

Returns the natural logarithm (base e) of x. Raises a runtime error when x is zero or negative.

import math

fn main() {
  let ln_e = math.log(math.e)
  println(ln_e) -- 1.0
}
open in playground

math.log10

math.log10(x: Float) -> Float

Returns the base-10 logarithm of x. Raises a runtime error when x is zero or negative.

import math

fn main() {
  let log_100 = math.log10(100.0)
  println(log_100) -- 2.0
}
open in playground

math.pi

math.pi: Float

Pi, approximately 3.141592653589793. This is a constant, not a function.

import math

fn main() {
  let circumference = 2.0 * math.pi * 5.0
  println(circumference)
}
open in playground

math.pow

math.pow(base: Float, exponent: Float) -> Float

Returns base raised to the power of exponent. Raises a runtime error when the result is too large for a Float, when base is zero and exponent is negative, and when a negative base is raised to a fractional exponent.

import math

fn main() {
  let two_to_ten = math.pow(2.0, 10.0)
  println(two_to_ten) -- 1024.0
}
open in playground

math.random

math.random() -> Float

Returns a random Float in the range [0.0, 1.0). The result is always finite.

import math

fn main() {
  let r = math.random()
  println(r) -- e.g. 0.7291035...
}
open in playground

math.sin

math.sin(x: Float) -> Float

Returns the sine of x (in radians).

import math

fn main() {
  println(math.sin(0.0)) -- 0.0
  println(math.sin(1.5707963)) -- 0.9999999999999997 (approximately 1.0, pi/2)
}
open in playground

math.sqrt

math.sqrt(x: Float) -> Float

Returns the square root of x. Raises a runtime error for a negative x.

import math

fn main() {
  let root = math.sqrt(4.0)
  println(root) -- 2.0
}
open in playground

math.tan

math.tan(x: Float) -> Float

Returns the tangent of x (in radians).

import math

fn main() {
  println(math.tan(0.0)) -- 0.0
  println(math.tan(0.7853982)) -- 1.0000000732051062 (approximately 1.0, pi/4)
}
open in playground