Library contents

Go math.Pow

Raise a growth factor to a power to repeat the same multiplication over several periods.

A simple example

math.Pow

  1. Start with 2 and 3
  2. Do this Raise 2 to the third power
  3. Result 8
Multiplying 2 by 2 by 2 gives 8.
main.go
package main

import "fmt"
import "math"

func main() {
    fmt.Println(math.Pow(2.0, 3.0))
}
Output
8

Syntax

Go 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.go

Project 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.

From the programfactor := 2.0
Result32.000
main.go
package main

import (
    "fmt"
    "math"
)

func main() {
    factor := 2.0
    periods := 5.0

    fmt.Printf("%.3f\n", math.Pow(factor, periods))
}
Output
32.000

Five doublings turn one seedling tray into 32 trays.

Advertisements

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.

From the programcopies := 3.0
Result81.000
main.go
package main

import (
    "fmt"
    "math"
)

func main() {
    copies := 3.0
    rounds := 4.0

    fmt.Printf("%.3f\n", math.Pow(copies, rounds))
}
Output
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.

Given4 children · 3 rounds
Worked result64 cells

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.

Documentation for math.Pow