start page | rating of books | rating of authors | reviews | copyrights

Book HomeJava and XSLTSearch this book

8.27. bignum

Supports whatever big numbers you give it, including large integers and floats. In addition, bignum lets you specify options for the accuracy and precision of numbers that you'll calculate with it.

For example:

#!/usr/local/bin/perl -w
use bignum;

my $num1 = 4;
my $num2 = 5;
my $mult = $num1 * $num2;
 
print sqrt($mult),"\n"; # Yup

For a number that would be large, you can use bignum's accuracy option, a, to set the number of places after the decimal:

perl -Mbignum=a,50 -le 'print sqrt(20), "\n";'

If you wish to use rounding with bignum, you can use the precision option, p, with a 0 or 1 value to round up a value:

perl -Mbignum=p,0 -le 'print sqrt(24), "\n";' # Gives 5

To enable tracing for bignum, use the t or trace options:

/usr/local/perl5.8-prerc1/bin/perl5.7.3 -Mbignum=p,0,t -le 'print sqrt(24)'
MBI import Math::BigInt::Trace :constant upgrade Math::BigFloat::Trace lib Calc
MBF import Math::BigFloat::Trace :constant downgrade Math::BigInt::Trace
MBI new '24' => '24' (Math::BigInt::Trace)
MBF new '24' => '24' (Math::BigFloat)
MBF new '4' => '4' (Math::BigFloat)
MBF new '1E-4' => '0' (Math::BigFloat)
MBI new '2' => '2' (Math::BigInt::Trace)
MBF new '2' => '2' (Math::BigInt::Trace)
MBI new '38' => '38' (Math::BigInt::Trace)
MBI new '38' => '38' (Math::BigInt::Trace)

The l, or lib, option lets you specify a different math library:

perl -Mbignum=l,new_math_lib -e 'print $this ** $that'

The v, or version, option shows you the version of all modules used by bignum and then exits.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.