Home »
Golang »
Golang Reference
Golang math.Gamma() Function with Examples
Golang | math.Gamma() Function: Here, we are going to learn about the Gamma() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 01, 2021
math.Gamma()
The Gamma() function is an inbuilt function of the math package which is used to get the Gamma function of the given number.
It accepts a parameter (x) and returns the Gamma function of x.
Syntax
func Gamma(x float64) float64
Parameters
- x : The value whose gamma function is to be returned.
Return Value
The return type of Gamma() function is a float64, it returns the Gamma function of the given number.
Special Cases
- Gamma(+Inf) = +Inf
If the parameter is positive infinity (+Inf), the function returns the same (+Inf).
- Gamma(+0) = +Inf
If the parameter is +0, the function returns positive infinity (+Inf).
- Gamma(-0) = -Inf
If the parameter is -0, the function returns negative infinity (-Inf).
- Gamma(x) = NaN for integer x < 0
If the parameter is less than 0, the function returns NaN.
- Gamma(-Inf) = NaN
If the parameter is negative infinity (-Inf), the function returns NaN.
- Gamma(NaN) = NaN
If the parameter is NaN, the function returns the same (NaN).
Example 1
// Golang program to demonstrate the
// example of math.Gamma() Function
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Gamma(1))
fmt.Println(math.Gamma(5.50))
fmt.Println(math.Gamma(120))
fmt.Println(math.Gamma(0))
fmt.Println(math.Gamma(-1))
fmt.Println(math.Gamma(math.Inf(-1)))
fmt.Println(math.Gamma(math.Inf(+1)))
fmt.Println(math.Gamma(math.NaN()))
}
Output:
1
52.34277778455352
5.574585761207607e+196
+Inf
NaN
NaN
+Inf
NaN
Example 2
// Golang program to demonstrate the
// example of math.Gamma() Function
package main
import (
"fmt"
"math"
)
func main() {
var x float64
var GammaX float64
x = 1
GammaX = math.Gamma(x)
fmt.Println("Gamma(", x, ") = ", GammaX)
x = 10.5
GammaX = math.Gamma(x)
fmt.Println("Gamma(", x, ") = ", GammaX)
x = 0
GammaX = math.Gamma(x)
fmt.Println("Gamma(", x, ") = ", GammaX)
x = -1
GammaX = math.Gamma(x)
fmt.Println("Gamma(", x, ") = ", GammaX)
x = math.Inf(1)
GammaX = math.Gamma(x)
fmt.Println("Gamma(", x, ") = ", GammaX)
x = math.NaN()
GammaX = math.Gamma(x)
fmt.Println("Gamma(", x, ") = ", GammaX)
}
Output:
Gamma( 1 ) = 1
Gamma( 10.5 ) = 1.1332783889487856e+06
Gamma( 0 ) = +Inf
Gamma( -1 ) = NaN
Gamma( +Inf ) = +Inf
Gamma( NaN ) = NaN
Golang math Package Constants and Functions »