LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Gamma function in LabVIEW

Why does gamma(0) in LabVIEW give me (NaN)?

While in Matlab, it gives me (Inf).

I don't thing (NaN) is ever generated by a gamma function, it should be (Inf).

This is very significant when I would like to do division, as any number divided by (Inf) gives me zero.

As whith NaN, any operation with NaN gives me another NaN.

 

Please let me know, if  I am wrong, or this is a bug in LabVIEW.

 

Thank you in advance.

Download All
0 Kudos
Message 1 of 6
(3,613 Views)

I know one can fix this easily, but porting my codes from MATLAB to native LabVIEW diagram took 2 days because of this issue.In my opinion, this is very crucial to be fixed internally. 

0 Kudos
Message 2 of 6
(3,587 Views)

I'm not a math major, but the way I see it is as follows:

 

I don't thing (NaN) is ever generated by a gamma function, it should be (Inf).

LIMIT of gamma (x) as x+  -> 0 is +INF
LIMIT of gamma (x) as x-  -> 0 is -INF
gamma (0) is undefined

So is it Positive INF, Negative INF, or Undefined?

 

I would like to do division, as any number divided by (Inf) gives me zero.

any number divided by INF <> zero
same as any number divided by zero <>  INF

 

this is a bug in LabVIEW.

All these singularities are "Undefined". If you feel that these singularities should be handled in a certain way (simialr to Matlab), then you should include those special cases in your code. IMHO the NaN works for me and is NOT a bug.

 

 Here is an interesting proof  that 1=0 !!

inf.PNG

 

0 Kudos
Message 3 of 6
(3,557 Views)

I definitly agree with you. Analitically, we can't tell what is gamma(0) as it is depending on from which side we are approaching.

However, what if the question is: 1/gamma(0)? The answer is 0.

If gamma(0) gives NaN, 1/gamma(0) is also NaN, which is wrong.

 

So, numerically (numerical approximation), gamma(0) = inf.

This is explained in  Cody, J., An Overview of Software Development for Special Functions, Lecture Notes in Mathematics, 506, Numerical Analysis Dundee, G. A. Watson (ed.), Springer Verlag, Berlin, 1976.

And Matlab followed Cody's implementation.

 

0 Kudos
Message 4 of 6
(3,549 Views)

This is another explanation:

gamma(x+1) =x*gama(x)

gamma(2) = 1*gamma(1)

gamma(3) = 2*gamma(2)

and so on..

 

Now,

gamma(1) = 1 =  0*gamma(0)

thus, gamma(0) = 1/0 = Inf.

 

Surprisingly, in LabVIEW 1/0 is already defined as Inf, not a NaN.

0 Kudos
Message 5 of 6
(3,538 Views)

I found a C++ function in math.h:

 

#include<iostream>
#include<sstream>
#include<math.h> // or <cmath>

int main(int argc, char **argv)
{

    std::cout << tgamma(0) << std::endl;
    std::cout  << 1/tgamma(0) << std::endl;
    return 0;
}

The result is that gamma(0) = inf and 1/gamma(0) = 0. It is never a NaN. So this is a bug in LabVIEW. Only LabVIEW is different compared to MATLAB and C++.

0 Kudos
Message 6 of 6
(3,512 Views)