Use the Log() method in the math package of Golang to get the natural logarithm of a number.
func Log(x float64) float64
Parameters: x of type float64
Return: It returns the natural logarithm of x.
In this article, we will discuss different types of logarithms to find the log of a number with examples.
How to Find Log of a Number in Golang using Natural Log
To find the log of a number in Golang, use the Log() function of the math package.
Let’s understand how to get a natural log of a number in go.
package main import ( "fmt" "math" ) func main() { ans := math.Log(1) fmt.Println("Log of 1 is:", ans) ans = math.Log(10) fmt.Println("Log of number 10 is:", ans) ans = math.Log(-2) fmt.Println("Log of -2 is:", ans) ans = math.Log(0) fmt.Println("Log of 0 is:", ans) }
In the above golang program, we imported a math package and used Log() method to find the natural logarithm for a number.
The output of the above go program is:
Log of 1 is: 0
Log of number 10 is: 2.302585092994046
Log of -2 is: NaN
Log of 0 is: -Inf
Cool Tip: How to get the pi value in golang!
How to Find the Decimal Log of a Number in Golang
To find the decimal log of a number in golang, use the Log10() method of the math package.
Let’s understand how to get a decimal log of a number in go.
package main import ( "fmt" "math" ) func main() { ans := math.Log10(1) fmt.Println("Log10 of 1 is:", ans) ans = math.Log10(5) fmt.Println("Log10 of number 5 is:", ans) ans = math.Log10(-1) fmt.Println("Log10 of -2 is:", ans) ans = math.Log10(0) fmt.Println("Log10 of 0 is:", ans) }
In the above golang program, we imported a math package and used the Log10() method to find the decimal logarithm for a number in go.
The output of the above go program is:
Log10 of 1 is: 0
Log10 of number 5 is: 0.6989700043360187
Log10 of -2 is: NaN
Log10 of 0 is: -Inf
Cool Tip: How to use the Floor function in golang!
How to Get Binary Exponent Log of a Number in Golang
Use the Logb() method of the math package to get the binary exponent log of a number.
Let’s understand how to get a binary exponent log of a number in go.
package main import ( "fmt" "math" ) func main() { ans := math.Logb(2) fmt.Println("Logb of 2 is:", ans) ans = math.Logb(0) fmt.Println("Logb of number 0 is:", ans) ans = math.Logb(-1) fmt.Println("Logb of -1 is:", ans) ans = math.Logb(10) fmt.Println("Logb of 10 is:", ans) }
In the above golang program, we imported a math package and used Logb() method to find the binary exponent logarithm for a number in go.
The output of the above go program is:
Logb of 2 is: 1
Logb of number 0 is: -Inf
Logb of -1 is: 0
Logb of 10 is: 3
Cool Tip: How to get the remainder using Golang Modulus!
How to Find the Binary Log of a Number in Go
Use the Log2() method of the math package to get the binary log of a number or the log to base 2 of a number.
Let’s understand how to get a binary log of a number in go.
package main import ( "fmt" "math" ) func main() { ans := math.Log2(1) fmt.Println("Log2 of 1 is:", ans) ans = math.Log2(0) fmt.Println("Log2 of number 0 is:", ans) ans = math.Log2(-1) fmt.Println("Log2 of -1 is:", ans) ans = math.Log2(10) fmt.Println("Log2 of 10 is:", ans) }
In the above golang program, we imported a math package and used the Log2() method to find the binary logarithm for a number in go.
The output of the above go program is:
Log2 of 1 is: 0
Log2 of number 0 is: -Inf
Log2 of -1 is: NaN
Log2 of 10 is: 3.321928094887362
Conclusion
I hope the above article helped you to understand golang logarithm types, and how to get a decimal log or binary log of a number in golang.
You can find more topics about the Golang tutorials on the GolangSpot Home page.