What is the Lgamma function in Golang?

The Lgamma function in the Go programming language is used to find the value of the natural logarithm and sign of Gamma(xx), where xx is the input value to the Lgamma function.

To use the Lgamma function, you must import the math package in your file and access the Lgamma function within it using the . notation (math.Lgamma). Here, Lgamma is the actual function, while math is the Go package that stores the definition of this function.

Syntax

The definition of the Lgamma function inside the math package is shown below:

Parameters

The Lgamma function takes a single argument of type float64, whose gamma value and sign you wish to find.

Return value

The Lgamma function returns two values:

  • lgamma: This return value has type float64 and represents the natural logarithm of Gamma(aa) ( here, aa represents the input value).

  • sign: This return value has type int and represents the sign of the above value. This return value is restricted to either +1 or -1.

Special cases of return values:

  • +Inf: Positive infinity is returned if the input given is positive infinity, 0, or a negative integer.

  • -Inf: Negative infinity is returned if the input given is negative infinity.

  • NAN: A NAN is returned if the input is NAN.

Examples

Below is a simple example in which we use the Lgamma function on 25:

package main
import "fmt"
import "math"
func main() {
inp := 25.0
x, y := math.Lgamma(inp)
fmt.Print("natural log of Gamma(",inp, "):", x,"\n")
fmt.Println("sign:", y)
}

Free Resources