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.14159import 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
endE
const E: f64 = 2.71828The example for this item is shown with the ones above: math-Sqrt.salam
TAU
const TAU: f64 = 6.28319The example for this item is shown with the ones above: math-Sqrt.salam
Functions
Sqrt
func Sqrt(x: f64): f64The example for this item is shown with the ones above: math-Sqrt.salam
Sin
func Sin(x: f64): f64import 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)
endCos
func Cos(x: f64): f64The example for this item is shown with the ones above: math-Sin.salam
Tan
func Tan(x: f64): f64The example for this item is shown with the ones above: math-Sin.salam
Asin
func Asin(x: f64): f64The example for this item is shown with the ones above: math-Sin.salam
Acos
func Acos(x: f64): f64The example for this item is shown with the ones above: math-Sin.salam
Atan
func Atan(x: f64): f64The example for this item is shown with the ones above: math-Sin.salam
Atan2
func Atan2(y: f64, x: f64): f64Atan 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): f64The example for this item is shown with the ones above: math-Sqrt.salam
Log10
func Log10(x: f64): f64The example for this item is shown with the ones above: math-Sqrt.salam
Log2
func Log2(x: f64): f64The example for this item is shown with the ones above: math-Sqrt.salam
Exp
func Exp(x: f64): f64The example for this item is shown with the ones above: math-Sqrt.salam
Floor
func Floor(x: f64): f64import 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)
endCeil
func Ceil(x: f64): f64The example for this item is shown with the ones above: math-Floor.salam
Round
func Round(x: f64): f64Math.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): f64The example for this item is shown with the ones above: math-Floor.salam
Pow
func Pow(base: f64, power: f64): f64base raised to the power power.The example for this item is shown with the ones above: math-Sqrt.salam
Cbrt
func Cbrt(x: f64): f64The example for this item is shown with the ones above: math-Sqrt.salam
Hypot
func Hypot(x: f64, y: f64): f64The example for this item is shown with the ones above: math-Sqrt.salam
Sign
func Sign(x: f64): f64The example for this item is shown with the ones above: math-Floor.salam
Min
func Min(a: f64, b: f64): f64import 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)
endMax
func Max(a: f64, b: f64): f64The example for this item is shown with the ones above: math-Min.salam
ClampF
func ClampF(x: f64, lo: f64, hi: f64): f64lo to hi.The example for this item is shown with the ones above: math-Min.salam
MinI
func MinI(a: int, b: int): intThe example for this item is shown with the ones above: math-Min.salam
MaxI
func MaxI(a: int, b: int): intThe example for this item is shown with the ones above: math-Min.salam
AbsI
func AbsI(x: int): intThe example for this item is shown with the ones above: math-Min.salam
ClampI
func ClampI(x: int, lo: int, hi: int): intlo 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): intimport 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)
endLcm
func Lcm(a: int, b: int): intThe example for this item is shown with the ones above: math-Gcd.salam
AbsI64
func AbsI64(x: i64): i64The example for this item is shown with the ones above: math-Min.salam
Mod
func Mod(x: f64, y: f64): f64x.The example for this item is shown with the ones above: math-Floor.salam
Trunc
func Trunc(x: f64): f64The example for this item is shown with the ones above: math-Floor.salam
IsNaN
func IsNaN(x: f64): bool== cannot detect it.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
endInf
func Inf(): f64The example for this item is shown with the ones above: math-IsNaN.salam
NaN
func NaN(): f64The example for this item is shown with the ones above: math-IsNaN.salam
IsInf
func IsInf(x: f64): boolThe example for this item is shown with the ones above: math-IsNaN.salam
Sinh
func Sinh(x: f64): f64The example for this item is shown with the ones above: math-Sin.salam
Cosh
func Cosh(x: f64): f64The example for this item is shown with the ones above: math-Sin.salam
Tanh
func Tanh(x: f64): f64The example for this item is shown with the ones above: math-Sin.salam
Pow10
func Pow10(n: int): f64n.The example for this item is shown with the ones above: math-Sqrt.salam
Lerp
func Lerp(a: f64, b: f64, t: f64): f64a 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): f64The example for this item is shown with the ones above: math-Sin.salam
Degrees
func Degrees(rad: f64): f64The example for this item is shown with the ones above: math-Sin.salam
Factorial
func Factorial(n: int): i64n. 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): i64The example for this item is shown with the ones above: math-Gcd.salam
MinI64
func MinI64(a: i64, b: i64): i64The example for this item is shown with the ones above: math-Min.salam
MaxI64
func MaxI64(a: i64, b: i64): i64The example for this item is shown with the ones above: math-Min.salam