Math Functions
Go has operators for +, -, *, / and %, but none for a power or a square root. For these, Go has ready-made functions in the math package. To use a function, write its name and put the values in parentheses, like math.Sqrt(25). This is called calling the function, and the call gives back a result.
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Sqrt(25))
fmt.Println(math.Sqrt(2))
fmt.Println(math.Pow(2, 10))
}
Output:
5
1.4142135623730951
1024
The program imports math because it uses math.Sqrt and math.Pow. math.Sqrt(x) gives the square root of x. math.Pow(x, y) gives x to the power y, so math.Pow(2, 10) is ten 2s multiplied together.
Do not write 2 ^ 10 for a power. In Go, ^ is a different operator that works on the bits of a number. It gives a wrong answer and no error:
fmt.Println(2 ^ 10)
Output:
8
math works with float64
Almost every function in math takes float64 values and gives back a float64. An int variable does not fit:
package main
import (
"fmt"
"math"
)
func main() {
a := 3
b := 10
fmt.Println(math.Abs(a - b))
}
Output:
# command-line-arguments
./main.go:11:23: cannot use a - b (value of type int) as float64 value in argument to math.Abs
math.Abs gives the absolute value: the number without its minus sign. To use it here, convert first: math.Abs(float64(a - b)).
The same is true for math.Max and math.Min, which give the larger and the smaller of two float64 values. For whole numbers, use the built-in max and min instead. They need no import and work with any number type. They can also take more than two values:
fmt.Println(math.Max(3, 7.5))
fmt.Println(max(3, 7, 5))
fmt.Println(min(3, 7, 5))
Output:
7.5
7
3
Round, Floor and Ceil
These turn a decimal number into a whole number, each in its own way:
| Function | What it does | Example |
|---|---|---|
math.Round(x) | goes to the nearest whole number | math.Round(2.4) is 2 |
math.Floor(x) | always goes down | math.Floor(2.7) is 2 |
math.Ceil(x) | always goes up | math.Ceil(2.1) is 3 |
fmt.Println(math.Round(2.5), math.Round(-2.5))
fmt.Println(math.Floor(2.7), math.Ceil(2.1))
Output:
3 -3
2 3
When a number is exactly in the middle, math.Round goes away from zero, so 2.5 becomes 3 and -2.5 becomes -3. All three give back a float64. Wrap the call in int() when you need an int.
math.Ceil helps when you count things that cannot be split. If 23 people ride in cars with 5 seats, you need 5 cars:
people := 23
seats := 5
exact := float64(people) / float64(seats)
fmt.Println(exact, math.Ceil(exact))
fmt.Println(math.Ceil(float64(people / seats)))
Output:
4.6 5
4
The exact answer is 4.6 cars, and math.Ceil turns it into 5. The last line is wrong. people / seats is an integer division, so it was already 4 before math.Ceil saw it.
Constants in math
The math package also has constants, such as math.Pi and math.MaxInt64, the biggest value an int can hold here:
fmt.Println(math.Pi)
fmt.Println(math.MaxInt64)
Output:
3.141592653589793
9223372036854775807
If a number goes past the biggest value of its type, it overflows. Go does not stop the program. It wraps around to the smallest value:
var level int8 = 127
level++
fmt.Println(level)
big := math.MaxInt64
big++
fmt.Println(big)
Output:
-128
-9223372036854775808
There is no error, just a wrong number. An int is big enough for almost everything, but keep this in mind when you multiply large numbers.
In the exercise, read two numbers and print a square root, a power, a rounded division and the larger number.