Log()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Jul 25, 2023
Contribute to Docs

The Log() function returns the natural logarithm of a given number of type float64. The math library must be imported in order to use this function.

Syntax

import math

result := math.Log(number)

Where result is the logarithmic value of number, returned as type float64, except under the following circumstances:

  • The result of Log(+Inf) is +Inf
  • The result of Log(0) is -Inf
  • The result of Log(x < 0) is NaN
  • The result of Log(NaN) is NaN

Example

The following calculates the logarithm of number and prints out the result:

package main
import (
"fmt"
"math"
)
func main() {
number := 4.2
result := math.Log(number)
fmt.Printf("%.1f\n", result)
}

The above code results in the following output:

1.4

Codebyte Example

The following example is runnable and shows how the Log() function handles special cases where zero or a negative number are supplied as arguments.

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn Go on Codecademy