xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/bignum.pm (revision 0:68f95e015346)
1*0Sstevel@tonic-gatepackage bignum;
2*0Sstevel@tonic-gaterequire 5.005;
3*0Sstevel@tonic-gate
4*0Sstevel@tonic-gate$VERSION = '0.15';
5*0Sstevel@tonic-gateuse Exporter;
6*0Sstevel@tonic-gate@EXPORT_OK 	= qw( );
7*0Sstevel@tonic-gate@EXPORT 	= qw( inf NaN );
8*0Sstevel@tonic-gate@ISA 		= qw( Exporter );
9*0Sstevel@tonic-gate
10*0Sstevel@tonic-gateuse strict;
11*0Sstevel@tonic-gate
12*0Sstevel@tonic-gate##############################################################################
13*0Sstevel@tonic-gate
14*0Sstevel@tonic-gate# These are all alike, and thus faked by AUTOLOAD
15*0Sstevel@tonic-gate
16*0Sstevel@tonic-gatemy @faked = qw/round_mode accuracy precision div_scale/;
17*0Sstevel@tonic-gateuse vars qw/$VERSION $AUTOLOAD $_lite/;		# _lite for testsuite
18*0Sstevel@tonic-gate
19*0Sstevel@tonic-gatesub AUTOLOAD
20*0Sstevel@tonic-gate  {
21*0Sstevel@tonic-gate  my $name = $AUTOLOAD;
22*0Sstevel@tonic-gate
23*0Sstevel@tonic-gate  $name =~ s/.*:://;    # split package
24*0Sstevel@tonic-gate  no strict 'refs';
25*0Sstevel@tonic-gate  foreach my $n (@faked)
26*0Sstevel@tonic-gate    {
27*0Sstevel@tonic-gate    if ($n eq $name)
28*0Sstevel@tonic-gate      {
29*0Sstevel@tonic-gate      *{"bignum::$name"} = sub
30*0Sstevel@tonic-gate        {
31*0Sstevel@tonic-gate        my $self = shift;
32*0Sstevel@tonic-gate        no strict 'refs';
33*0Sstevel@tonic-gate        if (defined $_[0])
34*0Sstevel@tonic-gate          {
35*0Sstevel@tonic-gate          Math::BigInt->$name($_[0]);
36*0Sstevel@tonic-gate          return Math::BigFloat->$name($_[0]);
37*0Sstevel@tonic-gate          }
38*0Sstevel@tonic-gate        return Math::BigInt->$name();
39*0Sstevel@tonic-gate        };
40*0Sstevel@tonic-gate      return &$name;
41*0Sstevel@tonic-gate      }
42*0Sstevel@tonic-gate    }
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate  # delayed load of Carp and avoid recursion
45*0Sstevel@tonic-gate  require Carp;
46*0Sstevel@tonic-gate  Carp::croak ("Can't call bignum\-\>$name, not a valid method");
47*0Sstevel@tonic-gate  }
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gatesub upgrade
50*0Sstevel@tonic-gate  {
51*0Sstevel@tonic-gate  my $self = shift;
52*0Sstevel@tonic-gate  no strict 'refs';
53*0Sstevel@tonic-gate#  if (defined $_[0])
54*0Sstevel@tonic-gate#    {
55*0Sstevel@tonic-gate#    $Math::BigInt::upgrade = $_[0];
56*0Sstevel@tonic-gate#    $Math::BigFloat::upgrade = $_[0];
57*0Sstevel@tonic-gate#    }
58*0Sstevel@tonic-gate  return $Math::BigInt::upgrade;
59*0Sstevel@tonic-gate  }
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gatesub import
62*0Sstevel@tonic-gate  {
63*0Sstevel@tonic-gate  my $self = shift;
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate  # some defaults
66*0Sstevel@tonic-gate  my $lib = 'Calc';
67*0Sstevel@tonic-gate  my $upgrade = 'Math::BigFloat';
68*0Sstevel@tonic-gate  my $downgrade = 'Math::BigInt';
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gate  my @import = ( ':constant' );				# drive it w/ constant
71*0Sstevel@tonic-gate  my @a = @_; my $l = scalar @_; my $j = 0;
72*0Sstevel@tonic-gate  my ($ver,$trace);					# version? trace?
73*0Sstevel@tonic-gate  my ($a,$p);						# accuracy, precision
74*0Sstevel@tonic-gate  for ( my $i = 0; $i < $l ; $i++,$j++ )
75*0Sstevel@tonic-gate    {
76*0Sstevel@tonic-gate    if ($_[$i] eq 'upgrade')
77*0Sstevel@tonic-gate      {
78*0Sstevel@tonic-gate      # this causes upgrading
79*0Sstevel@tonic-gate      $upgrade = $_[$i+1];		# or undef to disable
80*0Sstevel@tonic-gate      my $s = 2; $s = 1 if @a-$j < 2;	# avoid "can not modify non-existant..."
81*0Sstevel@tonic-gate      splice @a, $j, $s; $j -= $s; $i++;
82*0Sstevel@tonic-gate      }
83*0Sstevel@tonic-gate    elsif ($_[$i] eq 'downgrade')
84*0Sstevel@tonic-gate      {
85*0Sstevel@tonic-gate      # this causes downgrading
86*0Sstevel@tonic-gate      $downgrade = $_[$i+1];		# or undef to disable
87*0Sstevel@tonic-gate      my $s = 2; $s = 1 if @a-$j < 2;	# avoid "can not modify non-existant..."
88*0Sstevel@tonic-gate      splice @a, $j, $s; $j -= $s; $i++;
89*0Sstevel@tonic-gate      }
90*0Sstevel@tonic-gate    elsif ($_[$i] =~ /^(l|lib)$/)
91*0Sstevel@tonic-gate      {
92*0Sstevel@tonic-gate      # this causes a different low lib to take care...
93*0Sstevel@tonic-gate      $lib = $_[$i+1] || '';
94*0Sstevel@tonic-gate      my $s = 2; $s = 1 if @a-$j < 2;	# avoid "can not modify non-existant..."
95*0Sstevel@tonic-gate      splice @a, $j, $s; $j -= $s; $i++;
96*0Sstevel@tonic-gate      }
97*0Sstevel@tonic-gate    elsif ($_[$i] =~ /^(a|accuracy)$/)
98*0Sstevel@tonic-gate      {
99*0Sstevel@tonic-gate      $a = $_[$i+1];
100*0Sstevel@tonic-gate      my $s = 2; $s = 1 if @a-$j < 2;	# avoid "can not modify non-existant..."
101*0Sstevel@tonic-gate      splice @a, $j, $s; $j -= $s; $i++;
102*0Sstevel@tonic-gate      }
103*0Sstevel@tonic-gate    elsif ($_[$i] =~ /^(p|precision)$/)
104*0Sstevel@tonic-gate      {
105*0Sstevel@tonic-gate      $p = $_[$i+1];
106*0Sstevel@tonic-gate      my $s = 2; $s = 1 if @a-$j < 2;	# avoid "can not modify non-existant..."
107*0Sstevel@tonic-gate      splice @a, $j, $s; $j -= $s; $i++;
108*0Sstevel@tonic-gate      }
109*0Sstevel@tonic-gate    elsif ($_[$i] =~ /^(v|version)$/)
110*0Sstevel@tonic-gate      {
111*0Sstevel@tonic-gate      $ver = 1;
112*0Sstevel@tonic-gate      splice @a, $j, 1; $j --;
113*0Sstevel@tonic-gate      }
114*0Sstevel@tonic-gate    elsif ($_[$i] =~ /^(t|trace)$/)
115*0Sstevel@tonic-gate      {
116*0Sstevel@tonic-gate      $trace = 1;
117*0Sstevel@tonic-gate      splice @a, $j, 1; $j --;
118*0Sstevel@tonic-gate      }
119*0Sstevel@tonic-gate    else { die "unknown option $_[$i]"; }
120*0Sstevel@tonic-gate    }
121*0Sstevel@tonic-gate  my $class;
122*0Sstevel@tonic-gate  $_lite = 0;					# using M::BI::L ?
123*0Sstevel@tonic-gate  if ($trace)
124*0Sstevel@tonic-gate    {
125*0Sstevel@tonic-gate    require Math::BigInt::Trace; $class = 'Math::BigInt::Trace';
126*0Sstevel@tonic-gate    $upgrade = 'Math::BigFloat::Trace';
127*0Sstevel@tonic-gate    }
128*0Sstevel@tonic-gate  else
129*0Sstevel@tonic-gate    {
130*0Sstevel@tonic-gate    # see if we can find Math::BigInt::Lite
131*0Sstevel@tonic-gate    if (!defined $a && !defined $p)		# rounding won't work to well
132*0Sstevel@tonic-gate      {
133*0Sstevel@tonic-gate      eval 'require Math::BigInt::Lite;';
134*0Sstevel@tonic-gate      if ($@ eq '')
135*0Sstevel@tonic-gate        {
136*0Sstevel@tonic-gate        @import = ( );				# :constant in Lite, not MBI
137*0Sstevel@tonic-gate        Math::BigInt::Lite->import( ':constant' );
138*0Sstevel@tonic-gate        $_lite= 1;				# signal okay
139*0Sstevel@tonic-gate        }
140*0Sstevel@tonic-gate      }
141*0Sstevel@tonic-gate    require Math::BigInt if $_lite == 0;	# not already loaded?
142*0Sstevel@tonic-gate    $class = 'Math::BigInt';			# regardless of MBIL or not
143*0Sstevel@tonic-gate    }
144*0Sstevel@tonic-gate  # Math::BigInt::Trace or plain Math::BigInt
145*0Sstevel@tonic-gate  $class->import(@import, upgrade => $upgrade, lib => $lib);
146*0Sstevel@tonic-gate
147*0Sstevel@tonic-gate  if ($trace)
148*0Sstevel@tonic-gate    {
149*0Sstevel@tonic-gate    require Math::BigFloat::Trace; $class = 'Math::BigFloat::Trace';
150*0Sstevel@tonic-gate    $downgrade = 'Math::BigInt::Trace';
151*0Sstevel@tonic-gate    }
152*0Sstevel@tonic-gate  else
153*0Sstevel@tonic-gate    {
154*0Sstevel@tonic-gate    require Math::BigFloat; $class = 'Math::BigFloat';
155*0Sstevel@tonic-gate    }
156*0Sstevel@tonic-gate  $class->import(':constant','downgrade',$downgrade);
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate  bignum->accuracy($a) if defined $a;
159*0Sstevel@tonic-gate  bignum->precision($p) if defined $p;
160*0Sstevel@tonic-gate  if ($ver)
161*0Sstevel@tonic-gate    {
162*0Sstevel@tonic-gate    print "bignum\t\t\t v$VERSION\n";
163*0Sstevel@tonic-gate    print "Math::BigInt::Lite\t v$Math::BigInt::Lite::VERSION\n" if $_lite;
164*0Sstevel@tonic-gate    print "Math::BigInt\t\t v$Math::BigInt::VERSION";
165*0Sstevel@tonic-gate    my $config = Math::BigInt->config();
166*0Sstevel@tonic-gate    print " lib => $config->{lib} v$config->{lib_version}\n";
167*0Sstevel@tonic-gate    print "Math::BigFloat\t\t v$Math::BigFloat::VERSION\n";
168*0Sstevel@tonic-gate    exit;
169*0Sstevel@tonic-gate    }
170*0Sstevel@tonic-gate  $self->export_to_level(1,$self,@a);		# export inf and NaN
171*0Sstevel@tonic-gate  }
172*0Sstevel@tonic-gate
173*0Sstevel@tonic-gatesub inf () { Math::BigInt->binf(); }
174*0Sstevel@tonic-gatesub NaN () { Math::BigInt->bnan(); }
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate1;
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate__END__
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate=head1 NAME
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gatebignum - Transparent BigNumber support for Perl
183*0Sstevel@tonic-gate
184*0Sstevel@tonic-gate=head1 SYNOPSIS
185*0Sstevel@tonic-gate
186*0Sstevel@tonic-gate  use bignum;
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gate  $x = 2 + 4.5,"\n";			# BigFloat 6.5
189*0Sstevel@tonic-gate  print 2 ** 512 * 0.1,"\n";		# really is what you think it is
190*0Sstevel@tonic-gate  print inf * inf,"\n";			# prints inf
191*0Sstevel@tonic-gate  print NaN * 3,"\n";			# prints NaN
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate=head1 DESCRIPTION
194*0Sstevel@tonic-gate
195*0Sstevel@tonic-gateAll operators (including basic math operations) are overloaded. Integer and
196*0Sstevel@tonic-gatefloating-point constants are created as proper BigInts or BigFloats,
197*0Sstevel@tonic-gaterespectively.
198*0Sstevel@tonic-gate
199*0Sstevel@tonic-gateIf you do
200*0Sstevel@tonic-gate
201*0Sstevel@tonic-gate        use bignum;
202*0Sstevel@tonic-gate
203*0Sstevel@tonic-gateat the top of your script, Math::BigFloat and Math::BigInt will be loaded
204*0Sstevel@tonic-gateand any constant number will be converted to an object (Math::BigFloat for
205*0Sstevel@tonic-gatefloats like 3.1415 and Math::BigInt for integers like 1234).
206*0Sstevel@tonic-gate
207*0Sstevel@tonic-gateSo, the following line:
208*0Sstevel@tonic-gate
209*0Sstevel@tonic-gate        $x = 1234;
210*0Sstevel@tonic-gate
211*0Sstevel@tonic-gatecreates actually a Math::BigInt and stores a reference to in $x.
212*0Sstevel@tonic-gateThis happens transparently and behind your back, so to speak.
213*0Sstevel@tonic-gate
214*0Sstevel@tonic-gateYou can see this with the following:
215*0Sstevel@tonic-gate
216*0Sstevel@tonic-gate        perl -Mbignum -le 'print ref(1234)'
217*0Sstevel@tonic-gate
218*0Sstevel@tonic-gateDon't worry if it says Math::BigInt::Lite, bignum and friends will use Lite
219*0Sstevel@tonic-gateif it is installed since it is faster for some operations. It will be
220*0Sstevel@tonic-gateautomatically upgraded to BigInt whenever neccessary:
221*0Sstevel@tonic-gate
222*0Sstevel@tonic-gate        perl -Mbignum -le 'print ref(2**255)'
223*0Sstevel@tonic-gate
224*0Sstevel@tonic-gateThis also means it is a bad idea to check for some specific package, since
225*0Sstevel@tonic-gatethe actual contents of $x might be something unexpected. Due to the
226*0Sstevel@tonic-gatetransparent way of bignum C<ref()> should not be neccessary, anyway.
227*0Sstevel@tonic-gate
228*0Sstevel@tonic-gateSince Math::BigInt and BigFloat also overload the normal math operations,
229*0Sstevel@tonic-gatethe following line will still work:
230*0Sstevel@tonic-gate
231*0Sstevel@tonic-gate        perl -Mbignum -le 'print ref(1234+1234)'
232*0Sstevel@tonic-gate
233*0Sstevel@tonic-gateSince numbers are actually objects, you can call all the usual methods from
234*0Sstevel@tonic-gateBigInt/BigFloat on them. This even works to some extent on expressions:
235*0Sstevel@tonic-gate
236*0Sstevel@tonic-gate        perl -Mbignum -le '$x = 1234; print $x->bdec()'
237*0Sstevel@tonic-gate        perl -Mbignum -le 'print 1234->binc();'
238*0Sstevel@tonic-gate        perl -Mbignum -le 'print 1234->binc->badd(6);'
239*0Sstevel@tonic-gate        perl -Mbignum -le 'print +(1234)->binc()'
240*0Sstevel@tonic-gate
241*0Sstevel@tonic-gate(Note that print doesn't do what you expect if the expression starts with
242*0Sstevel@tonic-gate'(' hence the C<+>)
243*0Sstevel@tonic-gate
244*0Sstevel@tonic-gateYou can even chain the operations together as usual:
245*0Sstevel@tonic-gate
246*0Sstevel@tonic-gate        perl -Mbignum -le 'print 1234->binc->badd(6);'
247*0Sstevel@tonic-gate        1241
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gateUnder bignum (or bigint or bigrat), Perl will "upgrade" the numbers
250*0Sstevel@tonic-gateappropriately. This means that:
251*0Sstevel@tonic-gate
252*0Sstevel@tonic-gate        perl -Mbignum -le 'print 1234+4.5'
253*0Sstevel@tonic-gate        1238.5
254*0Sstevel@tonic-gate
255*0Sstevel@tonic-gatewill work correctly. These mixed cases don't do always work when using
256*0Sstevel@tonic-gateMath::BigInt or Math::BigFloat alone, or at least not in the way normal Perl
257*0Sstevel@tonic-gatescalars work.
258*0Sstevel@tonic-gate
259*0Sstevel@tonic-gateIf you do want to work with large integers like under C<use integer;>, try
260*0Sstevel@tonic-gateC<use bigint;>:
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate        perl -Mbigint -le 'print 1234.5+4.5'
263*0Sstevel@tonic-gate        1238
264*0Sstevel@tonic-gate
265*0Sstevel@tonic-gateThere is also C<use bigrat;> which gives you big rationals:
266*0Sstevel@tonic-gate
267*0Sstevel@tonic-gate        perl -Mbigrat -le 'print 1234+4.1'
268*0Sstevel@tonic-gate        12381/10
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gateThe entire upgrading/downgrading is still experimental and might not work
271*0Sstevel@tonic-gateas you expect or may even have bugs.
272*0Sstevel@tonic-gate
273*0Sstevel@tonic-gateYou might get errors like this:
274*0Sstevel@tonic-gate
275*0Sstevel@tonic-gate        Can't use an undefined value as an ARRAY reference at
276*0Sstevel@tonic-gate        /usr/local/lib/perl5/5.8.0/Math/BigInt/Calc.pm line 864
277*0Sstevel@tonic-gate
278*0Sstevel@tonic-gateThis means somewhere a routine got a BigFloat/Lite but expected a BigInt (or
279*0Sstevel@tonic-gatevice versa) and the upgrade/downgrad path was missing. This is a bug, please
280*0Sstevel@tonic-gatereport it so that we can fix it.
281*0Sstevel@tonic-gate
282*0Sstevel@tonic-gateYou might consider using just Math::BigInt or Math::BigFloat, since they
283*0Sstevel@tonic-gateallow you finer control over what get's done in which module/space. For
284*0Sstevel@tonic-gateinstance, simple loop counters will be Math::BigInts under C<use bignum;> and
285*0Sstevel@tonic-gatethis is slower than keeping them as Perl scalars:
286*0Sstevel@tonic-gate
287*0Sstevel@tonic-gate        perl -Mbignum -le 'for ($i = 0; $i < 10; $i++) { print ref($i); }'
288*0Sstevel@tonic-gate
289*0Sstevel@tonic-gatePlease note the following does not work as expected (prints nothing), since
290*0Sstevel@tonic-gateoverloading of '..' is not yet possible in Perl (as of v5.8.0):
291*0Sstevel@tonic-gate
292*0Sstevel@tonic-gate        perl -Mbignum -le 'for (1..2) { print ref($_); }'
293*0Sstevel@tonic-gate
294*0Sstevel@tonic-gate=head2 OPTIONS
295*0Sstevel@tonic-gate
296*0Sstevel@tonic-gatebignum recognizes some options that can be passed while loading it via use.
297*0Sstevel@tonic-gateThe options can (currently) be either a single letter form, or the long form.
298*0Sstevel@tonic-gateThe following options exist:
299*0Sstevel@tonic-gate
300*0Sstevel@tonic-gate=over 2
301*0Sstevel@tonic-gate
302*0Sstevel@tonic-gate=item a or accuracy
303*0Sstevel@tonic-gate
304*0Sstevel@tonic-gateThis sets the accuracy for all math operations. The argument must be greater
305*0Sstevel@tonic-gatethan or equal to zero. See Math::BigInt's bround() function for details.
306*0Sstevel@tonic-gate
307*0Sstevel@tonic-gate	perl -Mbignum=a,50 -le 'print sqrt(20)'
308*0Sstevel@tonic-gate
309*0Sstevel@tonic-gate=item p or precision
310*0Sstevel@tonic-gate
311*0Sstevel@tonic-gateThis sets the precision for all math operations. The argument can be any
312*0Sstevel@tonic-gateinteger. Negative values mean a fixed number of digits after the dot, while
313*0Sstevel@tonic-gatea positive value rounds to this digit left from the dot. 0 or 1 mean round to
314*0Sstevel@tonic-gateinteger. See Math::BigInt's bfround() function for details.
315*0Sstevel@tonic-gate
316*0Sstevel@tonic-gate	perl -Mbignum=p,-50 -le 'print sqrt(20)'
317*0Sstevel@tonic-gate
318*0Sstevel@tonic-gate=item t or trace
319*0Sstevel@tonic-gate
320*0Sstevel@tonic-gateThis enables a trace mode and is primarily for debugging bignum or
321*0Sstevel@tonic-gateMath::BigInt/Math::BigFloat.
322*0Sstevel@tonic-gate
323*0Sstevel@tonic-gate=item l or lib
324*0Sstevel@tonic-gate
325*0Sstevel@tonic-gateLoad a different math lib, see L<MATH LIBRARY>.
326*0Sstevel@tonic-gate
327*0Sstevel@tonic-gate	perl -Mbignum=l,GMP -e 'print 2 ** 512'
328*0Sstevel@tonic-gate
329*0Sstevel@tonic-gateCurrently there is no way to specify more than one library on the command
330*0Sstevel@tonic-gateline. This will be hopefully fixed soon ;)
331*0Sstevel@tonic-gate
332*0Sstevel@tonic-gate=item v or version
333*0Sstevel@tonic-gate
334*0Sstevel@tonic-gateThis prints out the name and version of all modules used and then exits.
335*0Sstevel@tonic-gate
336*0Sstevel@tonic-gate	perl -Mbignum=v -e ''
337*0Sstevel@tonic-gate
338*0Sstevel@tonic-gate=head2 METHODS
339*0Sstevel@tonic-gate
340*0Sstevel@tonic-gateBeside import() and AUTOLOAD() there are only a few other methods.
341*0Sstevel@tonic-gate
342*0Sstevel@tonic-gateSince all numbers are now objects, you can use all functions that are part of
343*0Sstevel@tonic-gatethe BigInt or BigFloat API. It is wise to use only the bxxx() notation, and not
344*0Sstevel@tonic-gatethe fxxx() notation, though. This makes it possible that the underlying object
345*0Sstevel@tonic-gatemight morph into a different class than BigFloat.
346*0Sstevel@tonic-gate
347*0Sstevel@tonic-gate=head2 CAVEAT
348*0Sstevel@tonic-gate
349*0Sstevel@tonic-gateBut a warning is in order. When using the following to make a copy of a number,
350*0Sstevel@tonic-gateonly a shallow copy will be made.
351*0Sstevel@tonic-gate
352*0Sstevel@tonic-gate        $x = 9; $y = $x;
353*0Sstevel@tonic-gate        $x = $y = 7;
354*0Sstevel@tonic-gate
355*0Sstevel@tonic-gateUsing the copy or the original with overloaded math is okay, e.g. the
356*0Sstevel@tonic-gatefollowing work:
357*0Sstevel@tonic-gate
358*0Sstevel@tonic-gate        $x = 9; $y = $x;
359*0Sstevel@tonic-gate        print $x + 1, " ", $y,"\n";     # prints 10 9
360*0Sstevel@tonic-gate
361*0Sstevel@tonic-gatebut calling any method that modifies the number directly will result in
362*0Sstevel@tonic-gateB<both> the original and the copy beeing destroyed:
363*0Sstevel@tonic-gate
364*0Sstevel@tonic-gate        $x = 9; $y = $x;
365*0Sstevel@tonic-gate        print $x->badd(1), " ", $y,"\n";        # prints 10 10
366*0Sstevel@tonic-gate
367*0Sstevel@tonic-gate        $x = 9; $y = $x;
368*0Sstevel@tonic-gate        print $x->binc(1), " ", $y,"\n";        # prints 10 10
369*0Sstevel@tonic-gate
370*0Sstevel@tonic-gate        $x = 9; $y = $x;
371*0Sstevel@tonic-gate        print $x->bmul(2), " ", $y,"\n";        # prints 18 18
372*0Sstevel@tonic-gate
373*0Sstevel@tonic-gateUsing methods that do not modify, but testthe contents works:
374*0Sstevel@tonic-gate
375*0Sstevel@tonic-gate        $x = 9; $y = $x;
376*0Sstevel@tonic-gate        $z = 9 if $x->is_zero();                # works fine
377*0Sstevel@tonic-gate
378*0Sstevel@tonic-gateSee the documentation about the copy constructor and C<=> in overload, as
379*0Sstevel@tonic-gatewell as the documentation in BigInt for further details.
380*0Sstevel@tonic-gate
381*0Sstevel@tonic-gate=over 2
382*0Sstevel@tonic-gate
383*0Sstevel@tonic-gate=item inf()
384*0Sstevel@tonic-gate
385*0Sstevel@tonic-gateA shortcut to return Math::BigInt->binf(). Usefull because Perl does not always
386*0Sstevel@tonic-gatehandle bareword C<inf> properly.
387*0Sstevel@tonic-gate
388*0Sstevel@tonic-gate=item NaN()
389*0Sstevel@tonic-gate
390*0Sstevel@tonic-gateA shortcut to return Math::BigInt->bnan(). Usefull because Perl does not always
391*0Sstevel@tonic-gatehandle bareword C<NaN> properly.
392*0Sstevel@tonic-gate
393*0Sstevel@tonic-gate=item upgrade()
394*0Sstevel@tonic-gate
395*0Sstevel@tonic-gateReturn the class that numbers are upgraded to, is in fact returning
396*0Sstevel@tonic-gateC<$Math::BigInt::upgrade>.
397*0Sstevel@tonic-gate
398*0Sstevel@tonic-gate=back
399*0Sstevel@tonic-gate
400*0Sstevel@tonic-gate=head2 MATH LIBRARY
401*0Sstevel@tonic-gate
402*0Sstevel@tonic-gateMath with the numbers is done (by default) by a module called
403*0Sstevel@tonic-gateMath::BigInt::Calc. This is equivalent to saying:
404*0Sstevel@tonic-gate
405*0Sstevel@tonic-gate	use bignum lib => 'Calc';
406*0Sstevel@tonic-gate
407*0Sstevel@tonic-gateYou can change this by using:
408*0Sstevel@tonic-gate
409*0Sstevel@tonic-gate	use bignum lib => 'BitVect';
410*0Sstevel@tonic-gate
411*0Sstevel@tonic-gateThe following would first try to find Math::BigInt::Foo, then
412*0Sstevel@tonic-gateMath::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:
413*0Sstevel@tonic-gate
414*0Sstevel@tonic-gate	use bignum lib => 'Foo,Math::BigInt::Bar';
415*0Sstevel@tonic-gate
416*0Sstevel@tonic-gatePlease see respective module documentation for further details.
417*0Sstevel@tonic-gate
418*0Sstevel@tonic-gate=head2 INTERNAL FORMAT
419*0Sstevel@tonic-gate
420*0Sstevel@tonic-gateThe numbers are stored as objects, and their internals might change at anytime,
421*0Sstevel@tonic-gateespecially between math operations. The objects also might belong to different
422*0Sstevel@tonic-gateclasses, like Math::BigInt, or Math::BigFLoat. Mixing them together, even
423*0Sstevel@tonic-gatewith normal scalars is not extraordinary, but normal and expected.
424*0Sstevel@tonic-gate
425*0Sstevel@tonic-gateYou should not depend on the internal format, all accesses must go through
426*0Sstevel@tonic-gateaccessor methods. E.g. looking at $x->{sign} is not a bright idea since there
427*0Sstevel@tonic-gateis no guaranty that the object in question has such a hashkey, nor is a hash
428*0Sstevel@tonic-gateunderneath at all.
429*0Sstevel@tonic-gate
430*0Sstevel@tonic-gate=head2 SIGN
431*0Sstevel@tonic-gate
432*0Sstevel@tonic-gateThe sign is either '+', '-', 'NaN', '+inf' or '-inf' and stored seperately.
433*0Sstevel@tonic-gateYou can access it with the sign() method.
434*0Sstevel@tonic-gate
435*0Sstevel@tonic-gateA sign of 'NaN' is used to represent the result when input arguments are not
436*0Sstevel@tonic-gatenumbers or as a result of 0/0. '+inf' and '-inf' represent plus respectively
437*0Sstevel@tonic-gateminus infinity. You will get '+inf' when dividing a positive number by 0, and
438*0Sstevel@tonic-gate'-inf' when dividing any negative number by 0.
439*0Sstevel@tonic-gate
440*0Sstevel@tonic-gate=head1 MODULES USED
441*0Sstevel@tonic-gate
442*0Sstevel@tonic-gateC<bignum> is just a thin wrapper around various modules of the Math::BigInt
443*0Sstevel@tonic-gatefamily. Think of it as the head of the family, who runs the shop, and orders
444*0Sstevel@tonic-gatethe others to do the work.
445*0Sstevel@tonic-gate
446*0Sstevel@tonic-gateThe following modules are currently used by bignum:
447*0Sstevel@tonic-gate
448*0Sstevel@tonic-gate	Math::BigInt::Lite	(for speed, and only if it is loadable)
449*0Sstevel@tonic-gate	Math::BigInt
450*0Sstevel@tonic-gate	Math::BigFloat
451*0Sstevel@tonic-gate
452*0Sstevel@tonic-gate=head1 EXAMPLES
453*0Sstevel@tonic-gate
454*0Sstevel@tonic-gateSome cool command line examples to impress the Python crowd ;)
455*0Sstevel@tonic-gate
456*0Sstevel@tonic-gate	perl -Mbignum -le 'print sqrt(33)'
457*0Sstevel@tonic-gate	perl -Mbignum -le 'print 2*255'
458*0Sstevel@tonic-gate	perl -Mbignum -le 'print 4.5+2*255'
459*0Sstevel@tonic-gate	perl -Mbignum -le 'print 3/7 + 5/7 + 8/3'
460*0Sstevel@tonic-gate	perl -Mbignum -le 'print 123->is_odd()'
461*0Sstevel@tonic-gate	perl -Mbignum -le 'print log(2)'
462*0Sstevel@tonic-gate	perl -Mbignum -le 'print 2 ** 0.5'
463*0Sstevel@tonic-gate	perl -Mbignum=a,65 -le 'print 2 ** 0.2'
464*0Sstevel@tonic-gate
465*0Sstevel@tonic-gate=head1 LICENSE
466*0Sstevel@tonic-gate
467*0Sstevel@tonic-gateThis program is free software; you may redistribute it and/or modify it under
468*0Sstevel@tonic-gatethe same terms as Perl itself.
469*0Sstevel@tonic-gate
470*0Sstevel@tonic-gate=head1 SEE ALSO
471*0Sstevel@tonic-gate
472*0Sstevel@tonic-gateEspecially L<bigrat> as in C<perl -Mbigrat -le 'print 1/3+1/4'>.
473*0Sstevel@tonic-gate
474*0Sstevel@tonic-gateL<Math::BigFloat>, L<Math::BigInt>, L<Math::BigRat> and L<Math::Big> as well
475*0Sstevel@tonic-gateas L<Math::BigInt::BitVect>, L<Math::BigInt::Pari> and  L<Math::BigInt::GMP>.
476*0Sstevel@tonic-gate
477*0Sstevel@tonic-gate=head1 AUTHORS
478*0Sstevel@tonic-gate
479*0Sstevel@tonic-gate(C) by Tels L<http://bloodgate.com/> in early 2002, 2003.
480*0Sstevel@tonic-gate
481*0Sstevel@tonic-gate=cut
482