×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang math.Lgamma() Function with Examples

Golang | math.Lgamma() Function: Here, we are going to learn about the Lgamma() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 01, 2021

math.Lgamma()

The Lgamma() function is an inbuilt function of the math package which is used to get the natural logarithm and sign (-1 or +1) of Gamma(x). Where x is the given parameter.

It accepts a parameter (x) and returns the natural logarithm and sign (-1 or +1) of Gamma(x).

Syntax

func Lgamma(x float64) (lgamma float64, sign int)

Parameters

  • x : The value to be used to get the natural logarithm and sign (-1 or +1) of Gamma(x).

Return Value

The return type of Lgamma() function is (lgamma float64, sign int), it returns the natural logarithm and sign (-1 or +1) of Gamma(x).

Special Cases

  • Lgamma(+Inf) = +Inf
    If the parameter is +Inf, the function returns the same (+Inf).
  • Lgamma(0) = +Inf
    If the parameter is 0, the function returns the +Inf.
  • Lgamma(-integer) = +Inf
    If the parameter is -integer, the function returns the +Inf.
  • Lgamma(-Inf) = -Inf
    If the parameter is -Inf, the function returns the same (-Inf).
  • Lgamma(NaN) = NaN
    If the parameter is NaN, the function returns the same (NaN).

Example 1

// Golang program to demonstrate the
// example of math.Lgamma() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Lgamma(0))
	fmt.Println(math.Lgamma(1))
	fmt.Println(math.Lgamma(2))
	fmt.Println(math.Lgamma(32))
	fmt.Println(math.Lgamma(120.5))

	fmt.Println(math.Lgamma(-1))
	fmt.Println(math.Lgamma(-2))
	fmt.Println(math.Lgamma(-32))
	fmt.Println(math.Lgamma(-120.5))

	fmt.Println(math.Lgamma(math.Inf(-1)))
	fmt.Println(math.Lgamma(math.Inf(+1)))
	fmt.Println(math.Lgamma(math.NaN()))
}

Output:

+Inf 1
0 1
0 1
78.0922235533153 1
455.4176004462345 1
+Inf 1
+Inf 1
+Inf 1
-459.06452031331577 -1
-Inf 1
+Inf 1
NaN 1

Example 2

// Golang program to demonstrate the
// example of math.Lgamma() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64

	x = 0
	lgamma, sign := math.Lgamma(x)
	fmt.Println("Lgamma(", x, ") = ", lgamma, sign)

	x = 1
	lgamma, sign = math.Lgamma(x)
	fmt.Println("Lgamma(", x, ") = ", lgamma, sign)

	x = 2
	lgamma, sign = math.Lgamma(x)
	fmt.Println("Lgamma(", x, ") = ", lgamma, sign)

	x = 32
	lgamma, sign = math.Lgamma(x)
	fmt.Println("Lgamma(", x, ") = ", lgamma, sign)

	x = -32
	lgamma, sign = math.Lgamma(x)
	fmt.Println("Lgamma(", x, ") = ", lgamma, sign)

	x = math.Inf(1)
	lgamma, sign = math.Lgamma(x)
	fmt.Println("Lgamma(", x, ") = ", lgamma, sign)

	x = math.NaN()
	lgamma, sign = math.Lgamma(x)
	fmt.Println("Lgamma(", x, ") = ", lgamma, sign)
}

Output:

Lgamma( 0 ) =  +Inf 1
Lgamma( 1 ) =  0 1
Lgamma( 2 ) =  0 1
Lgamma( 32 ) =  78.0922235533153 1
Lgamma( -32 ) =  +Inf 1
Lgamma( +Inf ) =  +Inf 1
Lgamma( NaN ) =  NaN 1

Golang math Package Constants and Functions »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.