math.Lgamma() Function in Golang with Examples

Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. This package provides Lgamma() function which is used to find the natural logarithm and sign (either -1 or +1) of Gamma(a). So, you need to add a math package in your program with the help of the import keyword to access the Lgamma() function.

Syntax:

func Lgamma(a float64) (lgamma float64, sign int)
  • If Lgamma(+Inf), then this function will return +Inf.
  • If Lgamma(0), then this function will return +Inf.
  • If Lgamma(-integer), then this function will return +Inf.
  • If Lgamma(-Inf), then this function will return -Inf.
  • If Lgamma(NaN), then this function will return NaN.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate
// math.Lgamma() Function
package main
  
import (
    "fmt"
    "math"
)
  
// Main function
func main() {
  
    // Finding the natural logarithm of Gamma()
    // Using Lgamma() function
    res_1, s1 := math.Lgamma(0)
    res_2, s2 := math.Lgamma(-2.3)
    res_3, s3 := math.Lgamma(math.Inf(-2))
    res_4, s4 := math.Lgamma(-2)
    res_5, s5 := math.Lgamma(math.NaN())
  
    // Displaying the result
    fmt.Printf("\nResult 1: %f and sign: %d", res_1, s1)
    fmt.Printf("\nResult 2: %f and sign: %d", res_2, s2)
    fmt.Printf("\nResult 3: %f and sign: %d", res_3, s3)
    fmt.Printf("\nResult 4: %f and sign: %d", res_4, s4)
    fmt.Printf("\nResult 5: %f and sign: %d", res_5, s5)
}

chevron_right


Output:

Result 1: +Inf and sign: 1
Result 2: 0.369567 and sign: -1
Result 3: -Inf and sign: 1
Result 4: +Inf and sign: 1
Result 5: NaN and sign: 1

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate
// math.Lgamma() Function
  
package main
  
import (
    "fmt"
    "math"
)
  
// Main function
func main() {
  
    // Finding the natural logarithm of Gamma()
    // Using Lgamma() function
    nvalue_1, sign_1 := math.Lgamma(1.5)
    nvalue_2, sign_2 := math.Lgamma(3.45)
  
    // Finding the sum of the 
    // natural logarithm of Gamma()
    res := nvalue_1 + nvalue_2
    fmt.Printf("Result 1: %f and sign: %d", nvalue_1, sign_1)
    fmt.Printf("\nResult 2: %f and sign: %d \n", nvalue_2, sign_2)
    fmt.Println("Sum of Result 1 and Result 2: ", res)
  
}

chevron_right


Output:

Result 1: -0.120782 and sign: 1
Result 2: 1.146231 and sign: 1 
Sum of Result 1 and Result 2:  1.0254487526135667



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.