Go Log()

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.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn how to use Go (Golang), an open-source programming language supported by Google!
    • Beginner Friendly.
      6 hours

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.

Code
Output

All contributors

Contribute to Docs

Learn Go on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn how to use Go (Golang), an open-source programming language supported by Google!
    • Beginner Friendly.
      6 hours