Standard library · Numbers and collections

math

The basic mathematics: powers and roots, logarithms, trigonometry, rounding, minimum and maximum, and the greatest common divisor.

import math

The math package holds what you would expect from a scientific calculator. Most of its functions work on f64; the ones whose names end in I or I64 are the whole-number versions.

The angles in the trigonometric functions are in radians, not degrees. Use Radians and Degrees to convert.

Constants and variables

PI

const PI: f64 = 3.14159
Pi (π), the ratio of a circle's circumference to its diameter.
math-Sqrt.salam
import math

func main:
    println math.Sqrt(2.0), math.Cbrt(27.0), math.Pow(2.0, 10.0), math.Pow10(3)
    println math.Hypot(3.0, 4.0), math.Exp(1.0)
    println math.Log(math.E), math.Log10(1000.0), math.Log2(1024.0)
    println math.PI, math.TAU
end
Output1.4142135623730951 3 1024 1000 5 2.718281828459045 1 3 10 3.141592653589793 6.283185307179586

E

const E: f64 = 2.71828
Euler's number (e), the base of the natural logarithm.

The example for this item is shown with the ones above: math-Sqrt.salam

TAU

const TAU: f64 = 6.28319
Twice pi (τ); one full turn in radians.

The example for this item is shown with the ones above: math-Sqrt.salam

Functions

Sqrt

func Sqrt(x: f64): f64
The square root of a number. For a negative number it gives NaN.

The example for this item is shown with the ones above: math-Sqrt.salam

Sin

func Sin(x: f64): f64
The sine of an angle given in radians.
math-Sin.salam
import math
import fmt

func main:
    angle := math.Radians(30.0)
    println fmt.FloatPrec(math.Sin(angle), 4), fmt.FloatPrec(math.Cos(angle), 4), fmt.FloatPrec(math.Tan(angle), 4)
    println fmt.FloatPrec(math.Degrees(math.Asin(0.5)), 4), fmt.FloatPrec(math.Degrees(math.Acos(0.0)), 4), fmt.FloatPrec(math.Degrees(math.Atan(1.0)), 4)
    println fmt.FloatPrec(math.Degrees(math.Atan2(1.0, -1.0)), 4)
    println fmt.FloatPrec(math.Sinh(1.0), 4), fmt.FloatPrec(math.Cosh(1.0), 4), fmt.FloatPrec(math.Tanh(1.0), 4)
end
Output0.5000 0.8660 0.5774 30.0000 90.0000 45.0000 135.0000 1.1752 1.5431 0.7616

Cos

func Cos(x: f64): f64
The cosine of an angle given in radians.

The example for this item is shown with the ones above: math-Sin.salam

Tan

func Tan(x: f64): f64
The tangent of an angle given in radians.

The example for this item is shown with the ones above: math-Sin.salam

Asin

func Asin(x: f64): f64
The inverse sine, an angle in radians between minus π/2 and π/2.

The example for this item is shown with the ones above: math-Sin.salam

Acos

func Acos(x: f64): f64
The inverse cosine, an angle in radians between zero and π.

The example for this item is shown with the ones above: math-Sin.salam

Atan

func Atan(x: f64): f64
The inverse tangent, an angle in radians.

The example for this item is shown with the ones above: math-Sin.salam

Atan2

func Atan2(y: f64, x: f64): f64
The angle of the point (x, y) from the horizontal axis. Unlike Atan it works out the right quadrant. Note that y comes first.

The example for this item is shown with the ones above: math-Sin.salam

Log

func Log(x: f64): f64
The natural logarithm, to base e.

The example for this item is shown with the ones above: math-Sqrt.salam

Log10

func Log10(x: f64): f64
The logarithm to base ten.

The example for this item is shown with the ones above: math-Sqrt.salam

Log2

func Log2(x: f64): f64
The logarithm to base two.

The example for this item is shown with the ones above: math-Sqrt.salam

Exp

func Exp(x: f64): f64
e raised to the power of the number.

The example for this item is shown with the ones above: math-Sqrt.salam

Floor

func Floor(x: f64): f64
The largest whole number not greater than the value; for negatives it goes down (−2.5 gives −3).
math-Floor.salam
import math

func main:
    each x in [2.5, -2.5, 2.49]:
        println x, math.Floor(x), math.Ceil(x), math.Round(x), math.Trunc(x)
    end
    println math.Abs(-3.5), math.Sign(-3.5), math.Sign(0.0), math.Mod(7.5, 2.0)
end
Output2.5 2 3 3 2 -2.5 -3 -2 -2 -2 2.49 2 3 2 2 3.5 -1 0 1.5

Ceil

func Ceil(x: f64): f64
The smallest whole number not less than the value.

The example for this item is shown with the ones above: math-Floor.salam

Round

func Round(x: f64): f64
Rounds to the nearest whole number, taking a half upwards, as JavaScript's Math.round does: 2.5 gives 3 and −2.5 gives −2. For money and exact rounding see the decimal package.

The example for this item is shown with the ones above: math-Floor.salam

Abs

func Abs(x: f64): f64
The absolute value of a decimal number.

The example for this item is shown with the ones above: math-Floor.salam

Pow

func Pow(base: f64, power: f64): f64
base raised to the power power.

The example for this item is shown with the ones above: math-Sqrt.salam

Cbrt

func Cbrt(x: f64): f64
The cube root; it gives the correct negative answer for a negative number.

The example for this item is shown with the ones above: math-Sqrt.salam

Hypot

func Hypot(x: f64, y: f64): f64
The hypotenuse of a right triangle, that is √(x² + y²), without overflowing on very large numbers.

The example for this item is shown with the ones above: math-Sqrt.salam

Sign

func Sign(x: f64): f64
The sign of a number: minus one, zero or one.

The example for this item is shown with the ones above: math-Floor.salam

Min

func Min(a: f64, b: f64): f64
The smaller of two decimal numbers.
math-Min.salam
import math

func main:
    println math.Min(2.5, 1.5), math.Max(2.5, 1.5), math.ClampF(1.7, 0.0, 1.0)
    println math.MinI(3, 8), math.MaxI(3, 8), math.AbsI(-9), math.ClampI(120, 0, 100)
    big := 9000000000 as i64
    println math.MinI64(big, 5 as i64), math.MaxI64(big, 5 as i64), math.AbsI64(-big)
    println math.Lerp(10.0, 20.0, 0.25)
end
Output1.5 2.5 1 3 8 9 100 5 9000000000 9000000000 12.5

Max

func Max(a: f64, b: f64): f64
The larger of two decimal numbers.

The example for this item is shown with the ones above: math-Min.salam

ClampF

func ClampF(x: f64, lo: f64, hi: f64): f64
Keeps a decimal number within the range lo to hi.

The example for this item is shown with the ones above: math-Min.salam

MinI

func MinI(a: int, b: int): int
The smaller of two whole numbers.

The example for this item is shown with the ones above: math-Min.salam

MaxI

func MaxI(a: int, b: int): int
The larger of two whole numbers.

The example for this item is shown with the ones above: math-Min.salam

AbsI

func AbsI(x: int): int
The absolute value of a whole number.

The example for this item is shown with the ones above: math-Min.salam

ClampI

func ClampI(x: int, lo: int, hi: int): int
Keeps a whole number within the range lo to hi, for holding a percentage between zero and a hundred, for instance.

The example for this item is shown with the ones above: math-Min.salam

Gcd

func Gcd(a: int, b: int): int
The greatest common divisor of two whole numbers.
math-Gcd.salam
import math

func main:
    println math.Gcd(12, 18), math.Lcm(4, 6)
    println math.GcdI64(1200000000 as i64, 1800000000 as i64)
    println math.Factorial(5), math.Factorial(20)
end
Output6 12 600000000 120 2432902008176640000

Lcm

func Lcm(a: int, b: int): int
The least common multiple of two whole numbers.

The example for this item is shown with the ones above: math-Gcd.salam

AbsI64

func AbsI64(x: i64): i64
The absolute value of a 64-bit whole number.

The example for this item is shown with the ones above: math-Min.salam

Mod

func Mod(x: f64, y: f64): f64
The remainder of dividing two decimal numbers; its sign follows the sign of x.

The example for this item is shown with the ones above: math-Floor.salam

Trunc

func Trunc(x: f64): f64
Drops the fractional part without rounding (−2.5 gives −2).

The example for this item is shown with the ones above: math-Floor.salam

IsNaN

func IsNaN(x: f64): bool
True when the number is "not a number". NaN is not equal to itself, so == cannot detect it.
math-IsNaN.salam
import math

func main:
    inf := math.Inf()
    nan := math.NaN()
    println inf, -inf, math.IsInf(inf), math.IsInf(1.0)
    println math.IsNaN(nan), math.IsNaN(1.0), nan == nan
end
Outputinf -inf true false true false false

Inf

func Inf(): f64
Returns positive infinity.

The example for this item is shown with the ones above: math-IsNaN.salam

NaN

func NaN(): f64
Returns a "not a number" value, the result of a meaningless operation such as zero divided by zero.

The example for this item is shown with the ones above: math-IsNaN.salam

IsInf

func IsInf(x: f64): bool
True when the number is positive or negative infinity.

The example for this item is shown with the ones above: math-IsNaN.salam

Sinh

func Sinh(x: f64): f64
The hyperbolic sine.

The example for this item is shown with the ones above: math-Sin.salam

Cosh

func Cosh(x: f64): f64
The hyperbolic cosine.

The example for this item is shown with the ones above: math-Sin.salam

Tanh

func Tanh(x: f64): f64
The hyperbolic tangent, also used in neural networks.

The example for this item is shown with the ones above: math-Sin.salam

Pow10

func Pow10(n: int): f64
Ten raised to the power n.

The example for this item is shown with the ones above: math-Sqrt.salam

Lerp

func Lerp(a: f64, b: f64, t: f64): f64
Linear interpolation: the point that lies between a and b at the fraction t. A t of zero is a and one is b.

The example for this item is shown with the ones above: math-Min.salam

Radians

func Radians(deg: f64): f64
Converts degrees to radians.

The example for this item is shown with the ones above: math-Sin.salam

Degrees

func Degrees(rad: f64): f64
Converts radians to degrees.

The example for this item is shown with the ones above: math-Sin.salam

Factorial

func Factorial(n: int): i64
The factorial of n. Up to 20 fits in an i64; for larger ones see the bigint package.

The example for this item is shown with the ones above: math-Gcd.salam

GcdI64

func GcdI64(a: i64, b: i64): i64
The greatest common divisor of two 64-bit whole numbers.

The example for this item is shown with the ones above: math-Gcd.salam

MinI64

func MinI64(a: i64, b: i64): i64
The smaller of two 64-bit whole numbers.

The example for this item is shown with the ones above: math-Min.salam

MaxI64

func MaxI64(a: i64, b: i64): i64
The larger of two 64-bit whole numbers.

The example for this item is shown with the ones above: math-Min.salam