📝 Edit page
➕ Add page
Math
Mathemetical operations
Constants
math.Pi
math.E
Square root
math.Sqrt(my_float)
Note use of float here to avoid error.
package main
import (
"fmt"
"math"
)
func main() {
n := 2.0
fmt.Printf("Number %v Square root %v", n, math.Sqrt(n))
}
If you pass inline without a variable, an integer is allowed as Go can infer it should be a float.
package main
import (
"fmt"
"math"
)
func main() {
fmt.Printf("Number 2 Square root %v", math.Sqrt(2))
}
Cube root
math.Cbrt(value)
Round
math.Round(value)
math.Ceil(value)
math.Floor(value)
Remove decimal places.
math.Trunc(value)
Max and min
math.Max(value1, value2)
math.Min(value1, value2)
Modulus
For integers:
17 % 5
For floats:
math.Mod(17.0, 5.0)
Absolute
math.Abs(value)
Raise to the power
math.Power(value, 2.0)