# Problem with Gamma function

**URL:** <https://discourse.julialang.org/t/problem-with-gamma-function/117941>\
**Category:** General Usage\
**Created:** [August 8, 2024, 5:38am UTC](https://discourse.julialang.org/t/problem-with-gamma-function/117941 "2024-08-08T05:38:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charles\_Nutter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charles_nutter/32/24365_2.png) [@Charles\_Nutter](https://discourse.julialang.org/u/Charles_Nutter)\
**Post date:** [August 8, 2024, 5:38am UTC](https://discourse.julialang.org/t/problem-with-gamma-function/117941/1 "2024-08-08T05:38:35Z")

</div>

Invoking the Gamma function fails. Although the Pkg.add downloads the algorithms. I had to write out the gamma function:  
gamma=quadgk(z-\> (t^(z-1)\*exp(z-t)),0,1,rtol=1e-4)  
(0.17730992497288517, 2.7755575615628914e-17)  
If ‘t’ is known, then this works every time. Gamma in Julia seems to be a problem. What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [August 8, 2024, 5:44am UTC](https://discourse.julialang.org/t/problem-with-gamma-function/117941/2 "2024-08-08T05:44:08Z")

</div>

Can’t really tell what the error is you’re describing since you’ve not shown any of the details, but I think you are wanting to do `using SpecialFunctions` to get the gamma function.

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [August 8, 2024, 6:01am UTC](https://discourse.julialang.org/t/problem-with-gamma-function/117941/3 "2024-08-08T06:01:05Z")

</div>

Hi and welcome to the Julia discourse.

Could you maybe provide code that is not working? As Daniel wrote, you might have (just) added a package (installed it on hard drive) but not loaded it (`using`).  
Also – which package you did add would be good to know (see also [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) for more details).

Two formatting tips: You can put code in backticks ``` to have inline code (or three of them to have a code block) – and you can also put math in `$...$` to have it rendered like `$\alpha$` renders nicely as \alpha.

---

<div class="post-metadata">

**Author:** ![Rudi79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rudi79/32/3884_2.png) [@Rudi79](https://discourse.julialang.org/u/Rudi79)\
**Post date:** [August 8, 2024, 11:13am UTC](https://discourse.julialang.org/t/problem-with-gamma-function/117941/4 "2024-08-08T11:13:02Z")

</div>

I am not an expert on gamma functions, but it seems that your formulation is not correct.  
First, the integration in your formula is over z whereas according to [Gamma function - Wikipedia](https://en.wikipedia.org/wiki/Gamma_function) the integration is over t. Moreover, you are integrating between 0 and 1 and again the formula I know integrates between 0 and \infty.  
For your original question, as other have already pointed out, this should work:

```julia
using SpecialFunctions
res = gamma(1) # = 1
res = gamma(0.5) # = 1.77

```

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [August 8, 2024, 1:11pm UTC](https://discourse.julialang.org/t/problem-with-gamma-function/117941/5 "2024-08-08T13:11:25Z")

</div>

> [@Charles\_Nutter](#):
>
> gamma=quadgk(z-\> (t^(z-1)\*exp(z-t)),0,1,rtol=1e-4)  
> (0.17730992497288517, 2.7755575615628914e-17)  
> If ‘t’ is known, then this works every time. Gamma in Julia seems to be a problem. What am I doing wrong?

Apart from the issues already highlighted by others, to me it seems you want `gamma` to be a function, so that you could write e.g. `gamma(1)`. But then you’d need

```julia
gamma = t -> first(quadgk(z -> t^(z - 1) * ..., ...))

```

(well actually, `gamma = z -> first(quadgk(t -> ..., ...))` as pointed out by Rudi79).  
In your current definition `gamma` is just a float which can only be evaluated if your `t` was defined prior, as you point out.

---

<div class="post-metadata">

**Author:** ![Charles\_Nutter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charles_nutter/32/24365_2.png) [@Charles\_Nutter](https://discourse.julialang.org/u/Charles_Nutter)\
**Post date:** [August 8, 2024, 2:38pm UTC](https://discourse.julialang.org/t/problem-with-gamma-function/117941/6 "2024-08-08T14:38:33Z")

</div>

Thank you for the answers. You are correct about choosing z, but it did not work for some reason. I will try it again.  
gamma=quadgk(t-\> ((t^(z-1))\*exp(-t)),0,10,rtol=1e-4)  
(0.9999546000702376, 6.425888090522136e-7)

This time, I found another error in the integral. Using the limits of integration as t = 0 to 100 for z=1.

gamma=quadgk(t-\> ((t^(z-1))\*exp(-t)),0,100,rtol=1e-4)  
(1.000000000000002, 6.472503355493802e-6)  
I will try using special functions again. Thank you! Beware! I might return.
