# bignum 0.22

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

Hello

I found a problem related with calculating Catalan's numbers

#!/usr/local/bin/perl -w

use bignum;

while (1){
\$n++;
#    \$Cat = (fact(2*\$n))/((n+1)!n!)
\$Cat = (fact(2*\$n))/((\$n+1)*fact(\$n)**2);
print "\$Cat\n";
last if length \$Cat > 78;
}

sub fact {
my \$i = shift;
my \$f = 1;
while (\$i>1){
\$f *= \$i;
\$i--;
}
return \$f;
}

__END__

TIA

--
http://www.telecable.es/personales/gamo /
perl -e 'print 111_111_111**2,"\n";'

## Re: bignum 0.22

What is the problem ? (I don't see it.)

Cheers,
Rob

## Re: bignum 0.22

On Thu, 5 Jul 2007, Sisyphus wrote:

The problem is that this output lacks precision aparently

216489185503133990863274261791925599831200000000000000000000
853702637172735926234421145556838686126800000000000000000000
3366939372774715522158184331074634818182000000000000000000000
13280705303722489004068393750349948449500000000000000000000000
52391773216519910749994580850004383791590000000000000000000000
206709359781542193322705891717290023323200000000000000000000000
815663960219058384462569194343901173113100000000000000000000000
3218959557293069695825496284821467129607000000000000000000000000
12704920022590345879098861442746675573490000000000000000000000000
50151000089172417943811295168736877263790000000000000000000000000
197987426438993719534698504405274280676200000000000000000000000000
781708976802233823680102715669100177152500000000000000000000000000
3086748267372923303762456877257472494397000000000000000000000000000
12190039767760866606383939871203238833810000000000000000000000000000
48145535217206784075634048230802707999060000000000000000000000000000
190174864107966797098754490511670696596300000000000000000000000000000
751269297881058917464501210451062751843200000000000000000000000000000
2968129521136642608343357241290264314659000000000000000000000000000000
11727731278637465915893265197293239487190000000000000000000000000000000
46343454246228695957965322150594252812290000000000000000000000000000000
183149331181095806425878953139148487114200000000000000000000000000000000
723875928001473901587997767169015449070200000000000000000000000000000000
2861304849265668492891140780463352404986000000000000000000000000000000000
11311095732253345760960290897769189975960000000000000000000000000000000000
44718285453094622775889522153971216184030000000000000000000000000000000000
176809220945312585436978572208778500912300000000000000000000000000000000000
699138751524213124094312216825551629561400000000000000000000000000000000000
2764775971936660990736598311991954171447000000000000000000000000000000000000
10934377152170553993439479038404269881060000000000000000000000000000000000000
43247909631719355347185999181748231619130000000000000000000000000000000000000
171069509209912116706646841207804116182300000000000000000000000000000000000000
676730852609799402854235298307342753721300000000000000000000000000000000000000

Cheers,

--
http://www.telecable.es/personales/gamo /
perl -e 'print 111_111_111**2,"\n";'

## Re: bignum 0.22

On Thu, 5 Jul 2007, gamo wrote:

The real Catalan numbers are of this form

50151000089172417943811295168736877263790538026356457136124707000
197987426438993719534698504405274280676181776208398535128701017200
781708976802233823680102715669100177152510806064194216284009188600
3086748267372923303762456877257472494397093952150920751480446539600
12190039767760866606383939871203238833805472726290924323643119385200
48145535217206784075634048230802707999063631776107012034556858076000
190174864107966797098754490511670696596301345515622697536499589400200
751269297881058917464501210451062751843240026086509499359064493663600
2968129521136642608343357241290264314659358135849980153205484311031600
11727731278637465915893265197293239487190634585553580117543620936271200
46343454246228695957965322150594252812285572152590760141906244022362000
183149331181095806425878953139148487114152581147038684080813476376374624
723875928001473901587997767169015449070222106438295751367024692344718752
2861304849265668492891140780463352404986232263244287143198790516197234752
11311095732253345760960290897769189975961199415637572612957718759342193629
44718285453094622775889522153971216184032648852520635911693306722980765510
176809220945312585436978572208778500912252165463043129681618151197016257478
699138751524213124094312216825551629561424593205010237977696200916445964684
2764775971936660990736598311991954171447451800401631395639071339987763587614
10934377152170553993439479038404269881062854488806451985760537780703486068308
43247909631719355347185999181748231619129200590055369794426007640095877732860
171069509209912116706646841207804116182333282333996796075729541331934805254424
676730852609799402854235298307342753721289013938899090358400685563095038432942
2677285562879644352897777603522480091364515660984549685943453807118230006063172

--
http://www.telecable.es/personales/gamo /
perl -e 'print 111_111_111**2,"\n";'

## Re: bignum 0.22

On 07/05/2007 04:36 AM, gamo wrote:

You can set the "accuracy" of the bignum module. Read "perldoc bignum"

BTW, if you make fact() recursive and use the Memoize module, you can
get a speed improvement--a /substantial/ speed improvement :-)

## Re: bignum 0.22

On Thu, 5 Jul 2007, Mumia W. wrote:

Thanks all of you.
This trick really really improve speed.

But if there is no previous knowledge that all numbers will be
integers I can not fix the precision or accuracy.

Cheers

## Re: bignum 0.22

Aren't Catalan numbers only defined on integers?

Choosing to leave the defaults in place is still a choice, and you should
understand what the consequences of that choice are.  It is not for the
accuracy and precision to be infinite.  They are still limited through
some confusing fall-back method.

Xho

--
Usenet Newsgroup Service                        \$9.95/Month 30GB

## Re: bignum 0.22

Oh, yes ... I should've realised.

Is that a bug in bignum ? I didn't see it documented anywhere that there was
a default "accuracy" set.
The same issue does not arise if you explicitly 'use Math::BigInt;'.

Cheers,
Rob

## Re: bignum 0.22

On 07/05/2007 10:08 PM, Sisyphus wrote:

Yes, I also noticed that Math::BigInt (or "bigint") works right out of
the box.

Evidently "bignum" has more to do and is more complicated, and
consequently, has more bugs. In this case, I don't know if the bug is in
the documentation or code; however, I think that the default accuracy
should be given in the POD.

Nonetheless, it can be queried by calling Math::BigFloat->accuracy().

Gamo, why did you choose bignum rather than bigint?

## Re: bignum 0.22

On Fri, 6 Jul 2007, Mumia W. wrote:

Because it's simple to use. It overloads any operation.
Fire and forget. No OOP nor especial commands.
If I need to use only BigInt, I'll check GMP::Mpz or Math::PARI, too.
PARI is another kind of animal, because it does functions.
Best regards,

--
http://www.telecable.es/personales/gamo /
perl -e 'print 111_111_111**2,"\n";'

## Re: bignum 0.22

On 07/06/2007 03:53 AM, gamo wrote:

"Bigint" is the same way, and I'm talking about the "bigint" pragma--not
the Math::BigInt module.

Want your program to run like a bat out of hades? Install
Math::BigInt::GMP and the binary GMP library. Then put this at the top

use bigint 'lib', 'GMP';

This gets you (nearly) the simplicity of normal Perl numbers with the
accuracy of Math::BigInt with the breathtaking speed of GMP.

Yes, I tested it.
:-)~~~~

## Re: bignum 0.22

news:NXkji.4428
.
.

Aaah ... the division in gamo's code means that the bignums are
Math::BigFloat objects rather than Math::BigInt objects ... and, according
to the Math::BigFloat documentation, Math::BigFloat sets the default value
of "div_scale" to 40.

So it's really just the price one pays for relying on bignum to DWIM (which
it fails to do wrt the original code that was posted), instead of using
Math::BigInt.

Cheers,
Rob

## Re: bignum 0.22

Hmmm.  Why is that?  It seems like BigInt overloads division to be bdiv,
not fdiv.  And bignum doesn't seem to overload anything, it relies on

Xho

--
Usenet Newsgroup Service                        \$9.95/Month 30GB

## Re: bignum 0.22

.
.

In Math::Big Int, the 'bdiv' operation (which is the overload function for
the division operator) contains the following code:
---------------------------------
---------------------------------

Presumably bignum sets \$Math::BigInt::upgrade to 'Math::bigFloat'.

The test script:
----------------------------------
use warnings;
use Math::BigInt;
use Math::BigFloat;

\$Math::BigInt::upgrade = 'Math::BigFloat'; # line 5

\$x = Math::BigInt->new(2);
\$y = Math::BigInt->new(7);
\$z = \$y / \$x;
print ref(\$z), "\n";
----------------------------------

That prints out 'Math::BigFloat', but if line 5 is commented out it prints
out 'Math::BigInt'.

Cheers,
Rob

## Re: bignum 0.22

^^
use bingnum qw(p 0); #instead see perldoc bignum;

To use this algoritm the precison must be 0 because all the numbers are
integers.

133 =
43247909631719355347185999181748231619129200590055369794426007640095877
732860
134 =
17106950920991211670664684120780411618233328233399679607572954133193480
5254424
135 =
67673085260979940285423529830734275372128901393889909035840068556309503
8432942
136 =
26772855628796443528977776035224800913645156609845496859434538071182300
06063172