HP Forums
%NFACT bug - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: General Forum (/forum-4.html)
+--- Thread: %NFACT bug (/thread-360.html)



%NFACT bug - David Hayden - 01-08-2014 03:01 AM

It appears to me that the System RPL entry %NFACT on my 50g doesn't work properly. This computes the factorial of a real number. Note that it's different from %FACT which computes gamma(x+1) and works correctly.

%NFACT appears to return IP(ABS(x))! There are 2 bugs
  1. It doesn't honor system flag 22 when the result overflows. It returns 9.99999999999E499, even if the flag is clear
  2. If x is between -1 and 1 (exclusive of the endpoints), it return 9.99999999999E499 instead of 1.


Hoping that someone can confirm or explain to me why I'm wrong.

Thanks,
Dave


RE: %NFACT bug - Han - 01-08-2014 03:14 AM

(01-08-2014 03:01 AM)David Hayden Wrote:  It appears to me that the System RPL entry %NFACT on my 50g doesn't work properly. This computes the factorial of a real number. Note that it's different from %FACT which computes gamma(x+1) and works correctly.

%NFACT appears to return IP(ABS(x))! There are 2 bugs
  1. It doesn't honor system flag 22 when the result overflows. It returns 9.99999999999E499, even if the flag is clear
  2. If x is between -1 and 1 (exclusive of the endpoints), it return 9.99999999999E499 instead of 1.


Hoping that someone can confirm or explain to me why I'm wrong.

Thanks,
Dave

Sounds like this bug I reported a while back: http://bugs.hpcalc.org/show_bug.cgi?id=255

This bug affects pretty much ANY command that uses argument checking and auto-converts DOZINT types to DOREAL -- which is to say a lot of commands are affected by it. I found the bug while porting Jazz for the HP48 series to the HP50G.


RE: %NFACT bug - David Hayden - 01-08-2014 01:32 PM

(01-08-2014 03:14 AM)Han Wrote:  Sounds like this bug I reported a while back: http://bugs.hpcalc.org/show_bug.cgi?id=255
I don't think so. %NFACT is a primitive code object and operates on real numbers only. It doesn't call CK&DISPATCH1


RE: %NFACT bug - Han - 01-08-2014 06:12 PM

If I am not mistaken, is flag 22 is for cases when you truly have infinity (such as division by 0), whereas flag 21 is for overflow. %NFACT does respect flag 21.

As for the second issue, it has to do with the fact at %NFACT (and pretty much any other SysRPL command) does not do any argument checking. That it works for almost any decimal value is a mere bonus due to the algorithm implemented. Basically the algorithm checks a few special cases and then proceeds with the general algorithm, which fails for the case when the input \( x \) satisfies \( x\in (-1,1) \). Perhaps you are expecting it to return the factorial of any real number, whereas the program is designed to return the factorial of an DOREAL integer.


RE: %NFACT bug - David Hayden - 01-09-2014 03:02 AM

(01-08-2014 06:12 PM)Han Wrote:  If I am not mistaken, is flag 22 is for cases when you truly have infinity (such as division by 0), whereas flag 21 is for overflow. %NFACT does respect flag 21.

Ah! That's it. Thanks so much.

(01-08-2014 06:12 PM)Han Wrote:  Perhaps you are expecting it to return the factorial of any real number, whereas the program is designed to return the factorial of an DOREAL integer.
Yes I think that's fair. A good way to look at it is that %NFACT expects a DOREAL integer argument greater than zero. The fact that it returns something sort-of meaningful for DOREAL integers less than -1 is just coincidence.

Thanks for your help!
Dave