02-16-2017 04:27 AM - edited 02-16-2017 04:40 AM
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.
02-16-2017 04:45 AM - edited 02-16-2017 04:46 AM
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.
02-16-2017 06:44 AM
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 !!
02-16-2017 07:29 AM - edited 02-16-2017 07:35 AM
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.
02-16-2017 07:53 AM - edited 02-16-2017 07:55 AM
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.
02-16-2017 11:15 AM - edited 02-16-2017 11:16 AM
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++.