small error using FACT()

104 views
Skip to first unread message

CV

unread,
May 27, 2020, 12:08:35 PM5/27/20
to Harbour Users
Hi

I tried to use fact() - factorial of a number and see that this function is limited to 21 max (from CT contrib - ctmath2.c).

But if you try fact(21), the result is:
51.090.942.171.709.430.000 harbour / xharbour - WRONG
51.090.942.171.709.400.000 EXCEL 2013! - WRONG
51.090.942.171.709.440.000 any calculator (the one in W10) - RIGHT

Anyway, as it is limited to 21, does anyone know a way to calculate factorial() without any limit or at least use a greater limit like 30 (without rounding issues)?

I know: the problem relies on the double precision math that harbour/C uses.

I tried myself with my own function (with a for-loop or recursively) and obviously I get the same wrong result.
If I try with greater numbers than 21 I get rounding problems (the proper upper limit should be 20 for the CT source file, if someone can fix it).

A must will be to have the gamma function, but this is a totally different story.

Regards
Claudio Voskian

Klas Engwall

unread,
May 27, 2020, 1:42:42 PM5/27/20
to harbou...@googlegroups.com
Hi Claudio,

> If I try with greater numbers than 21 I get rounding problems (the
> proper upper limit should be 20 for the CT source file, if someone can
> fix it).

Probably Nantucket did not check if there were any rounding errors, or I
suppose they would have restricted the input to 20. The problem with the
kind of "fix" you suggested is the backwards Clipper compatibility.
Those who might rely on Harbour returning the same result as Clipper
will be more frustrated with your new restriction than with the rounding
error. So I think this is just a "known limitation" of the function.

Regards,
Klas
Message has been deleted

Pete

unread,
May 27, 2020, 2:38:26 PM5/27/20
to Harbour Users
Hi,


On Wednesday, 27 May 2020 19:08:35 UTC+3, CV wrote:
Hi

I tried to use fact() - factorial of a number and see that this function is limited to 21 max (from CT contrib - ctmath2.c).

But if you try fact(21), the result is:
51.090.942.171.709.430.000 harbour / xharbour - WRONG

At least for harbour that's not correct.

in Harbour 3.2.0dev (r1904111533) (Compiler: MinGW GNU C 9.1.1 (32-bit))
the result is: 51.090.942.171.709.440.000,00

in Harbour 3.2.0dev (r2002240732) (Compiler: MinGW GNU C 9.2.1 (32-bit))
the result is: 51.090.942.171.709.440.000,00

in Harbour 3.4.0dev () (2017-12-20 13:40) (Compiler: MinGW GNU C 8.1 (32-bit))
the result is: 51.090.942.171.709.440.000,00

which version of Harbour (and C compiler) did you use that gave this wrong result?

regards,
Pete

CV

unread,
May 28, 2020, 8:44:00 AM5/28/20
to Harbour Users
Thank you Klas and Pete

It was a mistake from a friend, who ask me to send him a function to calculate some statistics functions.
I developed the source, he compiled... but he used xharbour (I don't know which C compiler, as he doesn't know and is irrelevant); then send me the info I presented here.

Anyway it was a good exercise to remember probabilistics!
Still I wish to know if there is an alternative to calculate huge numbers without rounding effect, as in fact(N) with N >= 22.

I will send the message to the xharbour forum.

Best regards
Claudio Voskian
Reply all
Reply to author
Forward
0 new messages