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
| Name | Signature | Description |
|---|---|---|
acos | (Float) -> Float | Arccosine (radians) |
asin | (Float) -> Float | Arcsine (radians) |
atan | (Float) -> Float | Arctangent (radians) |
atan2 | (Float, Float) -> Float | Two-argument arctangent |
cos | (Float) -> Float | Cosine |
e | Float | Euler’s number (2.71828…) |
exp | (Float) -> Float | Exponential (e^x) |
log | (Float) -> Float | Natural logarithm (ln) |
log10 | (Float) -> Float | Base-10 logarithm |
pi | Float | Pi (3.14159…) |
pow | (Float, Float) -> Float | Exponentiation |
random | () -> Float | Random float in [0.0, 1.0) |
sin | (Float) -> Float | Sine |
sqrt | (Float) -> Float | Square root |
tan | (Float) -> Float | Tangent |
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 playgroundmath.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 playgroundmath.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 playgroundmath.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 playgroundmath.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 playgroundmath.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 playgroundmath.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 playgroundmath.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 playgroundmath.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 playgroundmath.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 playgroundmath.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 playgroundmath.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 playgroundmath.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 playgroundmath.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 playgroundmath.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