find the base 2 logarithm of a number in go

To find the base 2 logarithm of a number in Go, we can use the Log2 function from the math package.

main.go
package main

import (
	"fmt"
	"math"
)

func main() {
	number := 8.0
	logBase2 := math.Log2(number)
	fmt.Printf("Log base 2 of %v is %v", number, logBase2)
}
159 chars
13 lines

The output of the code above will be:

main.go
Log base 2 of 8 is 3
21 chars
2 lines

In this example, we pass the number 8 into the Log2 function and store the result in the logBase2 variable. We then use Printf to print out the result.

related categories

gistlibby LogSnag