ADVERTISEMENT
ADVERTISEMENT

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)

Parameter(s):

  • 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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.