Log2()
Published Aug 9, 2023
Contribute to Docs
The Log2()
function in the Go programming language calculates the base-2 logarithm of a given number. It returns the logarithm value as a floating-point number. The math
package must be imported to use this function.
Syntax
result := Log2(x)
Log2()
accepts an argument of type float64
and returns a value of type float64
.
Note these special cases:
- If
x
is negative orNaN
, the function will returnNaN
(not-a-number). - If
x
is zero, the function will return-Inf
. - If
x
is+Inf
, the function will return+Inf
.
Example
package mainimport ("fmt""math")func main() {x := 8.0result := math.Log2(x)fmt.Printf("Log2(%f) = %f\n", x, result)}
The above code results in the following output:
Log2(8.000000) = 3.000000
Codebyte Example
The example below is runnable and demonstrates Log2()
on a range of values.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Go on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Go
Learn how to use Go (Golang), an open-source programming language supported by Google!Beginner Friendly6 hours