12 ##############################################################################
14 # These are all alike, and thus faked by AUTOLOAD
16 my @faked = qw/round_mode accuracy precision div_scale/;
17 use vars qw/$VERSION $AUTOLOAD $_lite/; # _lite for testsuite
23 $name =~ s/.*:://; # split package
25 foreach my $n (@faked)
29 *{"bigint::$name"} = sub
35 Math::BigInt->$name($_[0]);
37 return Math::BigInt->$name();
43 # delayed load of Carp and avoid recursion
45 Carp::croak ("Can't call bigint\-\>$name, not a valid method");
54 # $Math::BigInt::upgrade = $_[0];
56 return $Math::BigInt::upgrade;
61 # this takes a floating point constant string and returns it truncated to
62 # integer. For instance, '4.5' => '4', '1.234e2' => '123' etc
65 # some simple cases first
66 return $float if ($float =~ /^[+-]?[0-9]+$/); # '+123','-1','0' etc
68 if ($float =~ /^[+-]?[0-9]+\.?[eE]\+?[0-9]+$/); # 123e2, 123.e+2
69 return '0' if ($float =~ /^[+-]?[0]*\.[0-9]+$/); # .2, 0.2, -.1
70 if ($float =~ /^[+-]?[0-9]+\.[0-9]*$/) # 1., 1.23, -1.2 etc
75 my ($mis,$miv,$mfv,$es,$ev) = Math::BigInt::_split(\$float);
76 return $float if !defined $mis; # doesn't look like a number to me
78 my $sign = $$mis; $sign = '' if $sign eq '+';
81 # ignore fraction part entirely
82 if ($ec >= length($$miv)) # 123.23E-4
86 return $sign . substr ($$miv,0,length($$miv)-$ec); # 1234.45E-2 = 12
89 if ($ec >= length($$mfv))
92 return $sign.$$miv.$$mfv if $ec == 0; # 123.45E+2 => 12345
93 return $sign.$$miv.$$mfv.'E'.$ec; # 123.45e+3 => 12345e1
95 $mfv = substr($$mfv,0,$ec);
96 return $sign.$$miv.$mfv; # 123.45e+1 => 1234
106 my @import = ( ':constant' ); # drive it w/ constant
107 my @a = @_; my $l = scalar @_; my $j = 0;
108 my ($ver,$trace); # version? trace?
109 my ($a,$p); # accuracy, precision
110 for ( my $i = 0; $i < $l ; $i++,$j++ )
112 if ($_[$i] =~ /^(l|lib)$/)
114 # this causes a different low lib to take care...
115 $lib = $_[$i+1] || '';
116 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
117 splice @a, $j, $s; $j -= $s; $i++;
119 elsif ($_[$i] =~ /^(a|accuracy)$/)
122 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
123 splice @a, $j, $s; $j -= $s; $i++;
125 elsif ($_[$i] =~ /^(p|precision)$/)
128 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
129 splice @a, $j, $s; $j -= $s; $i++;
131 elsif ($_[$i] =~ /^(v|version)$/)
134 splice @a, $j, 1; $j --;
136 elsif ($_[$i] =~ /^(t|trace)$/)
139 splice @a, $j, 1; $j --;
141 else { die "unknown option $_[$i]"; }
144 $_lite = 0; # using M::BI::L ?
147 require Math::BigInt::Trace; $class = 'Math::BigInt::Trace';
151 # see if we can find Math::BigInt::Lite
152 if (!defined $a && !defined $p) # rounding won't work to well
154 eval 'require Math::BigInt::Lite;';
157 @import = ( ); # :constant in Lite, not MBI
158 Math::BigInt::Lite->import( ':constant' );
159 $_lite= 1; # signal okay
162 require Math::BigInt if $_lite == 0; # not already loaded?
163 $class = 'Math::BigInt'; # regardless of MBIL or not
165 # Math::BigInt::Trace or plain Math::BigInt
166 $class->import(@import, lib => $lib);
168 bigint->accuracy($a) if defined $a;
169 bigint->precision($p) if defined $p;
172 print "bigint\t\t\t v$VERSION\n";
173 print "Math::BigInt::Lite\t v$Math::BigInt::Lite::VERSION\n" if $_lite;
174 print "Math::BigInt\t\t v$Math::BigInt::VERSION";
175 my $config = Math::BigInt->config();
176 print " lib => $config->{lib} v$config->{lib_version}\n";
179 # we take care of floating point constants, since BigFloat isn't available
180 # and BigInt doesn't like them:
181 overload::constant float => sub { Math::BigInt->new( _constant(shift) ); };
190 bigint - Transparent big integer support for Perl
196 $x = 2 + 4.5,"\n"; # BigInt 6
197 print 2 ** 512; # really is what you think it is
201 All operators (including basic math operations) are overloaded. Integer
202 constants are created as proper BigInts.
204 Floating point constants are truncated to integer. All results are also
209 bigint recognizes some options that can be passed while loading it via use.
210 The options can (currently) be either a single letter form, or the long form.
211 The following options exist:
217 This sets the accuracy for all math operations. The argument must be greater
218 than or equal to zero. See Math::BigInt's bround() function for details.
220 perl -Mbigint=a,2 -le 'print 12345+1'
224 This sets the precision for all math operations. The argument can be any
225 integer. Negative values mean a fixed number of digits after the dot, and
226 are <B>ignored</B> since all operations happen in integer space.
227 A positive value rounds to this digit left from the dot. 0 or 1 mean round to
228 integer and are ignore like negative values.
230 See Math::BigInt's bfround() function for details.
232 perl -Mbignum=p,5 -le 'print 123456789+123'
236 This enables a trace mode and is primarily for debugging bigint or
241 Load a different math lib, see L<MATH LIBRARY>.
243 perl -Mbigint=l,GMP -e 'print 2 ** 512'
245 Currently there is no way to specify more than one library on the command
246 line. This will be hopefully fixed soon ;)
250 This prints out the name and version of all modules used and then exits.
252 perl -Mbigint=v -e ''
256 Math with the numbers is done (by default) by a module called
257 Math::BigInt::Calc. This is equivalent to saying:
259 use bigint lib => 'Calc';
261 You can change this by using:
263 use bigint lib => 'BitVect';
265 The following would first try to find Math::BigInt::Foo, then
266 Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:
268 use bigint lib => 'Foo,Math::BigInt::Bar';
270 Please see respective module documentation for further details.
272 =head2 INTERNAL FORMAT
274 The numbers are stored as objects, and their internals might change at anytime,
275 especially between math operations. The objects also might belong to different
276 classes, like Math::BigInt, or Math::BigInt::Lite. Mixing them together, even
277 with normal scalars is not extraordinary, but normal and expected.
279 You should not depend on the internal format, all accesses must go through
280 accessor methods. E.g. looking at $x->{sign} is not a bright idea since there
281 is no guaranty that the object in question has such a hash key, nor is a hash
286 The sign is either '+', '-', 'NaN', '+inf' or '-inf' and stored seperately.
287 You can access it with the sign() method.
289 A sign of 'NaN' is used to represent the result when input arguments are not
290 numbers or as a result of 0/0. '+inf' and '-inf' represent plus respectively
291 minus infinity. You will get '+inf' when dividing a positive number by 0, and
292 '-inf' when dividing any negative number by 0.
296 Since all numbers are now objects, you can use all functions that are part of
297 the BigInt API. You can only use the bxxx() notation, and not the fxxx()
302 C<bigint> is just a thin wrapper around various modules of the Math::BigInt
303 family. Think of it as the head of the family, who runs the shop, and orders
304 the others to do the work.
306 The following modules are currently used by bigint:
308 Math::BigInt::Lite (for speed, and only if it is loadable)
313 Some cool command line examples to impress the Python crowd ;) You might want
314 to compare them to the results under -Mbignum or -Mbigrat:
316 perl -Mbigint -le 'print sqrt(33)'
317 perl -Mbigint -le 'print 2*255'
318 perl -Mbigint -le 'print 4.5+2*255'
319 perl -Mbigint -le 'print 3/7 + 5/7 + 8/3'
320 perl -Mbigint -le 'print 123->is_odd()'
321 perl -Mbigint -le 'print log(2)'
322 perl -Mbigint -le 'print 2 ** 0.5'
323 perl -Mbigint=a,65 -le 'print 2 ** 0.2'
327 This program is free software; you may redistribute it and/or modify it under
328 the same terms as Perl itself.
332 Especially L<bigrat> as in C<perl -Mbigrat -le 'print 1/3+1/4'> and
333 L<bignum> as in C<perl -Mbignum -le 'print sqrt(2)'>.
335 L<Math::BigInt>, L<Math::BigRat> and L<Math::Big> as well
336 as L<Math::BigInt::BitVect>, L<Math::BigInt::Pari> and L<Math::BigInt::GMP>.
340 (C) by Tels L<http://bloodgate.com/> in early 2002.