Go math.Pow
Raise a growth factor to a power to repeat the same multiplication over several periods.
A simple example
math.Pow
- Start with 2 and 3
- Do this Raise 2 to the third power
- Result 8
package main
import "fmt"
import "math"
func main() {
fmt.Println(math.Pow(2.0, 3.0))
}
8
Syntax
math.Pow(factor, periods)
Run these examples in Go
Save one example at a time as main.go. Use Go 1.21 or later.
go run main.goProject repeated doubling
A print job branches into additional copies at each round. Use the starting count and the number of rounds to project the final batch before reserving paper.
package main
import (
"fmt"
"math"
)
func main() {
factor := 2.0
periods := 5.0
fmt.Printf("%.3f\n", math.Pow(factor, periods))
}
32.000
Five doublings turn one seedling tray into 32 trays.
Estimate copies in a branching print run
A print center branches each master copy into several copies in every round. Project the final count from the branching factor so the operator can budget paper before starting the job.
package main
import (
"fmt"
"math"
)
func main() {
copies := 3.0
rounds := 4.0
fmt.Printf("%.3f\n", math.Pow(copies, rounds))
}
81.000
Starting with one page and tripling its copies through four rounds produces 81 pages.
Count cells after repeated subdivision
A designer starts with one square on a poster. Each round replaces every square with four smaller squares, and the design goes through three rounds.
The factor four applies three times to the starting square.
What to watch for
For exact currency, use decimal arithmetic or integer minor units and a stated rounding policy.