Home »
Golang »
Golang Reference
Golang math.Log() Function with Examples
Golang | math.Log() Function: Here, we are going to learn about the Log() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 01, 2021
math.Log()
The Log() function is an inbuilt function of the math package which is used to get the natural logarithm of the given number.
It accepts a parameter (x) and returns the natural logarithm of x.
Syntax
func Log(x float64) float64
Parameters
- x : The value whose natural logarithm is to be found.
Return Value
The return type of Log() function is a float64, it returns the natural logarithm of the given value.
Special Cases
- Log(+Inf) = +Inf
If the parameter is positive infinity (+Inf), the function returns the same (+Inf).
- Log(0) = -Inf
If the parameter is 0, the function returns the -Inf.
- Log(x < 0) = NaN
If the parameter is less than 0, the function returns NaN.
- Log(NaN) = NaN
If the parameter is NaN, the function returns NaN.
Example 1
// Golang program to demonstrate the
// example of math.Log() Function
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Log(1))
fmt.Println(math.Log(2))
fmt.Println(math.Log(10))
fmt.Println(math.Log(10.23))
fmt.Println(math.Log(0))
fmt.Println(math.Log(-1))
fmt.Println(math.Log(math.NaN()))
fmt.Println(math.Log(math.Inf(1)))
}
Output:
0
0.6931471805599453
2.302585092994046
2.325324579963535
-Inf
NaN
NaN
+Inf
Example 2
// Golang program to demonstrate the
// example of math.Log() Function
package main
import (
"fmt"
"math"
)
func main() {
var x float64
var LogX float64
x = 1
LogX = math.Log(x)
fmt.Println("Log(", x, ") =", LogX)
x = 2
LogX = math.Log(x)
fmt.Println("Log(", x, ") =", LogX)
x = 10
LogX = math.Log(x)
fmt.Println("Log(", x, ") =", LogX)
x = 10.58
LogX = math.Log(x)
fmt.Println("Log(", x, ") =", LogX)
x = math.Inf(1)
LogX = math.Log(x)
fmt.Println("Log(", x, ") =", LogX)
x = math.NaN()
LogX = math.Log(x)
fmt.Println("Log(", x, ") =", LogX)
}
Output:
Log( 1 ) = 0
Log( 2 ) = 0.6931471805599453
Log( 10 ) = 2.302585092994046
Log( 10.58 ) = 2.3589654264301534
Log( +Inf ) = +Inf
Log( NaN ) = NaN
Golang math Package Constants and Functions »