1Math::BigInt(3)       User Contributed Perl Documentation      Math::BigInt(3)
2
3
4

NAME

6       Math::BigInt - Arbitrary size integer/float math package
7

SYNOPSIS

9         use Math::BigInt;
10
11         # or make it faster with huge numbers: install (optional)
12         # Math::BigInt::GMP and always use (it falls back to
13         # pure Perl if the GMP library is not installed):
14         # (See also the L<MATH LIBRARY> section!)
15
16         # warns if Math::BigInt::GMP cannot be found
17         use Math::BigInt lib => 'GMP';
18
19         # to suppress the warning use this:
20         # use Math::BigInt try => 'GMP';
21
22         # dies if GMP cannot be loaded:
23         # use Math::BigInt only => 'GMP';
24
25         my $str = '1234567890';
26         my @values = (64, 74, 18);
27         my $n = 1; my $sign = '-';
28
29         # Configuration methods (may be used as class methods and instance methods)
30
31         Math::BigInt->accuracy();     # get class accuracy
32         Math::BigInt->accuracy($n);   # set class accuracy
33         Math::BigInt->precision();    # get class precision
34         Math::BigInt->precision($n);  # set class precision
35         Math::BigInt->round_mode();   # get class rounding mode
36         Math::BigInt->round_mode($m); # set global round mode, must be one of
37                                       # 'even', 'odd', '+inf', '-inf', 'zero',
38                                       # 'trunc', or 'common'
39         Math::BigInt->config();       # return hash with configuration
40
41         # Constructor methods (when the class methods below are used as instance
42         # methods, the value is assigned the invocand)
43
44         $x = Math::BigInt->new($str);         # defaults to 0
45         $x = Math::BigInt->new('0x123');      # from hexadecimal
46         $x = Math::BigInt->new('0b101');      # from binary
47         $x = Math::BigInt->from_hex('cafe');  # from hexadecimal
48         $x = Math::BigInt->from_oct('377');   # from octal
49         $x = Math::BigInt->from_bin('1101');  # from binary
50         $x = Math::BigInt->bzero();           # create a +0
51         $x = Math::BigInt->bone();            # create a +1
52         $x = Math::BigInt->bone('-');         # create a -1
53         $x = Math::BigInt->binf();            # create a +inf
54         $x = Math::BigInt->binf('-');         # create a -inf
55         $x = Math::BigInt->bnan();            # create a Not-A-Number
56         $x = Math::BigInt->bpi();             # returns pi
57
58         $y = $x->copy();         # make a copy (unlike $y = $x)
59         $y = $x->as_int();       # return as a Math::BigInt
60
61         # Boolean methods (these don't modify the invocand)
62
63         $x->is_zero();          # if $x is 0
64         $x->is_one();           # if $x is +1
65         $x->is_one("+");        # ditto
66         $x->is_one("-");        # if $x is -1
67         $x->is_inf();           # if $x is +inf or -inf
68         $x->is_inf("+");        # if $x is +inf
69         $x->is_inf("-");        # if $x is -inf
70         $x->is_nan();           # if $x is NaN
71
72         $x->is_positive();      # if $x > 0
73         $x->is_pos();           # ditto
74         $x->is_negative();      # if $x < 0
75         $x->is_neg();           # ditto
76
77         $x->is_odd();           # if $x is odd
78         $x->is_even();          # if $x is even
79         $x->is_int();           # if $x is an integer
80
81         # Comparison methods
82
83         $x->bcmp($y);           # compare numbers (undef, < 0, == 0, > 0)
84         $x->bacmp($y);          # compare absolutely (undef, < 0, == 0, > 0)
85         $x->beq($y);            # true if and only if $x == $y
86         $x->bne($y);            # true if and only if $x != $y
87         $x->blt($y);            # true if and only if $x < $y
88         $x->ble($y);            # true if and only if $x <= $y
89         $x->bgt($y);            # true if and only if $x > $y
90         $x->bge($y);            # true if and only if $x >= $y
91
92         # Arithmetic methods
93
94         $x->bneg();             # negation
95         $x->babs();             # absolute value
96         $x->bsgn();             # sign function (-1, 0, 1, or NaN)
97         $x->bnorm();            # normalize (no-op)
98         $x->binc();             # increment $x by 1
99         $x->bdec();             # decrement $x by 1
100         $x->badd($y);           # addition (add $y to $x)
101         $x->bsub($y);           # subtraction (subtract $y from $x)
102         $x->bmul($y);           # multiplication (multiply $x by $y)
103         $x->bmuladd($y,$z);     # $x = $x * $y + $z
104         $x->bdiv($y);           # division (floored), set $x to quotient
105                                 # return (quo,rem) or quo if scalar
106         $x->btdiv($y);          # division (truncated), set $x to quotient
107                                 # return (quo,rem) or quo if scalar
108         $x->bmod($y);           # modulus (x % y)
109         $x->btmod($y);          # modulus (truncated)
110         $x->bmodinv($mod);      # modular multiplicative inverse
111         $x->bmodpow($y,$mod);   # modular exponentiation (($x ** $y) % $mod)
112         $x->bpow($y);           # power of arguments (x ** y)
113         $x->blog();             # logarithm of $x to base e (Euler's number)
114         $x->blog($base);        # logarithm of $x to base $base (e.g., base 2)
115         $x->bexp();             # calculate e ** $x where e is Euler's number
116         $x->bnok($y);           # x over y (binomial coefficient n over k)
117         $x->bsin();             # sine
118         $x->bcos();             # cosine
119         $x->batan();            # inverse tangent
120         $x->batan2($y);         # two-argument inverse tangent
121         $x->bsqrt();            # calculate square-root
122         $x->broot($y);          # $y'th root of $x (e.g. $y == 3 => cubic root)
123         $x->bfac();             # factorial of $x (1*2*3*4*..$x)
124
125         $x->blsft($n);          # left shift $n places in base 2
126         $x->blsft($n,$b);       # left shift $n places in base $b
127                                 # returns (quo,rem) or quo (scalar context)
128         $x->brsft($n);          # right shift $n places in base 2
129         $x->brsft($n,$b);       # right shift $n places in base $b
130                                 # returns (quo,rem) or quo (scalar context)
131
132         # Bitwise methods
133
134         $x->band($y);           # bitwise and
135         $x->bior($y);           # bitwise inclusive or
136         $x->bxor($y);           # bitwise exclusive or
137         $x->bnot();             # bitwise not (two's complement)
138
139         # Rounding methods
140         $x->round($A,$P,$mode); # round to accuracy or precision using
141                                 # rounding mode $mode
142         $x->bround($n);         # accuracy: preserve $n digits
143         $x->bfround($n);        # $n > 0: round to $nth digit left of dec. point
144                                 # $n < 0: round to $nth digit right of dec. point
145         $x->bfloor();           # round towards minus infinity
146         $x->bceil();            # round towards plus infinity
147         $x->bint();             # round towards zero
148
149         # Other mathematical methods
150
151         $x->bgcd($y);            # greatest common divisor
152         $x->blcm($y);            # least common multiple
153
154         # Object property methods (do not modify the invocand)
155
156         $x->sign();              # the sign, either +, - or NaN
157         $x->digit($n);           # the nth digit, counting from the right
158         $x->digit(-$n);          # the nth digit, counting from the left
159         $x->length();            # return number of digits in number
160         ($xl,$f) = $x->length(); # length of number and length of fraction
161                                  # part, latter is always 0 digits long
162                                  # for Math::BigInt objects
163         $x->mantissa();          # return (signed) mantissa as a Math::BigInt
164         $x->exponent();          # return exponent as a Math::BigInt
165         $x->parts();             # return (mantissa,exponent) as a Math::BigInt
166         $x->sparts();            # mantissa and exponent (as integers)
167         $x->nparts();            # mantissa and exponent (normalised)
168         $x->eparts();            # mantissa and exponent (engineering notation)
169         $x->dparts();            # integer and fraction part
170
171         # Conversion methods (do not modify the invocand)
172
173         $x->bstr();         # decimal notation, possibly zero padded
174         $x->bsstr();        # string in scientific notation with integers
175         $x->bnstr();        # string in normalized notation
176         $x->bestr();        # string in engineering notation
177         $x->bdstr();        # string in decimal notation
178
179         $x->to_hex();       # as signed hexadecimal string
180         $x->to_bin();       # as signed binary string
181         $x->to_oct();       # as signed octal string
182         $x->to_bytes();     # as byte string
183
184         $x->as_hex();       # as signed hexadecimal string with prefixed 0x
185         $x->as_bin();       # as signed binary string with prefixed 0b
186         $x->as_oct();       # as signed octal string with prefixed 0
187
188         # Other conversion methods
189
190         $x->numify();           # return as scalar (might overflow or underflow)
191

DESCRIPTION

193       Math::BigInt provides support for arbitrary precision integers.
194       Overloading is also provided for Perl operators.
195
196   Input
197       Input values to these routines may be any scalar number or string that
198       looks like a number and represents an integer.
199
200       ·   Leading and trailing whitespace is ignored.
201
202       ·   Leading and trailing zeros are ignored.
203
204       ·   If the string has a "0x" prefix, it is interpreted as a hexadecimal
205           number.
206
207       ·   If the string has a "0b" prefix, it is interpreted as a binary
208           number.
209
210       ·   One underline is allowed between any two digits.
211
212       ·   If the string can not be interpreted, NaN is returned.
213
214       Octal numbers are typically prefixed by "0", but since leading zeros
215       are stripped, these methods can not automatically recognize octal
216       numbers, so use the constructor from_oct() to interpret octal strings.
217
218       Some examples of valid string input
219
220           Input string                Resulting value
221           123                         123
222           1.23e2                      123
223           12300e-2                    123
224           0xcafe                      51966
225           0b1101                      13
226           67_538_754                  67538754
227           -4_5_6.7_8_9e+0_1_0         -4567890000000
228
229       Input given as scalar numbers might lose precision. Quote your input to
230       ensure that no digits are lost:
231
232           $x = Math::BigInt->new( 56789012345678901234 );   # bad
233           $x = Math::BigInt->new('56789012345678901234');   # good
234
235       Currently, Math::BigInt->new() defaults to 0, while
236       Math::BigInt->new('') results in 'NaN'. This might change in the
237       future, so use always the following explicit forms to get a zero or
238       NaN:
239
240           $zero = Math::BigInt->bzero();
241           $nan  = Math::BigInt->bnan();
242
243   Output
244       Output values are usually Math::BigInt objects.
245
246       Boolean operators "is_zero()", "is_one()", "is_inf()", etc. return true
247       or false.
248
249       Comparison operators "bcmp()" and "bacmp()") return -1, 0, 1, or undef.
250

METHODS

252   Configuration methods
253       Each of the methods below (except config(), accuracy() and precision())
254       accepts three additional parameters. These arguments $A, $P and $R are
255       "accuracy", "precision" and "round_mode". Please see the section about
256       "ACCURACY and PRECISION" for more information.
257
258       Setting a class variable effects all object instance that are created
259       afterwards.
260
261       accuracy()
262               Math::BigInt->accuracy(5);      # set class accuracy
263               $x->accuracy(5);                # set instance accuracy
264
265               $A = Math::BigInt->accuracy();  # get class accuracy
266               $A = $x->accuracy();            # get instance accuracy
267
268           Set or get the accuracy, i.e., the number of significant digits.
269           The accuracy must be an integer. If the accuracy is set to "undef",
270           no rounding is done.
271
272           Alternatively, one can round the results explicitly using one of
273           "round()", "bround()" or "bfround()" or by passing the desired
274           accuracy to the method as an additional parameter:
275
276               my $x = Math::BigInt->new(30000);
277               my $y = Math::BigInt->new(7);
278               print scalar $x->copy()->bdiv($y, 2);               # prints 4300
279               print scalar $x->copy()->bdiv($y)->bround(2);       # prints 4300
280
281           Please see the section about "ACCURACY and PRECISION" for further
282           details.
283
284               $y = Math::BigInt->new(1234567);    # $y is not rounded
285               Math::BigInt->accuracy(4);          # set class accuracy to 4
286               $x = Math::BigInt->new(1234567);    # $x is rounded automatically
287               print "$x $y";                      # prints "1235000 1234567"
288
289               print $x->accuracy();       # prints "4"
290               print $y->accuracy();       # also prints "4", since
291                                           #   class accuracy is 4
292
293               Math::BigInt->accuracy(5);  # set class accuracy to 5
294               print $x->accuracy();       # prints "4", since instance
295                                           #   accuracy is 4
296               print $y->accuracy();       # prints "5", since no instance
297                                           #   accuracy, and class accuracy is 5
298
299           Note: Each class has it's own globals separated from Math::BigInt,
300           but it is possible to subclass Math::BigInt and make the globals of
301           the subclass aliases to the ones from Math::BigInt.
302
303       precision()
304               Math::BigInt->precision(-2);     # set class precision
305               $x->precision(-2);               # set instance precision
306
307               $P = Math::BigInt->precision();  # get class precision
308               $P = $x->precision();            # get instance precision
309
310           Set or get the precision, i.e., the place to round relative to the
311           decimal point. The precision must be a integer. Setting the
312           precision to $P means that each number is rounded up or down,
313           depending on the rounding mode, to the nearest multiple of 10**$P.
314           If the precision is set to "undef", no rounding is done.
315
316           You might want to use "accuracy()" instead. With "accuracy()" you
317           set the number of digits each result should have, with
318           "precision()" you set the place where to round.
319
320           Please see the section about "ACCURACY and PRECISION" for further
321           details.
322
323               $y = Math::BigInt->new(1234567);    # $y is not rounded
324               Math::BigInt->precision(4);         # set class precision to 4
325               $x = Math::BigInt->new(1234567);    # $x is rounded automatically
326               print $x;                           # prints "1230000"
327
328           Note: Each class has its own globals separated from Math::BigInt,
329           but it is possible to subclass Math::BigInt and make the globals of
330           the subclass aliases to the ones from Math::BigInt.
331
332       div_scale()
333           Set/get the fallback accuracy. This is the accuracy used when
334           neither accuracy nor precision is set explicitly. It is used when a
335           computation might otherwise attempt to return an infinite number of
336           digits.
337
338       round_mode()
339           Set/get the rounding mode.
340
341       upgrade()
342           Set/get the class for upgrading. When a computation might result in
343           a non-integer, the operands are upgraded to this class. This is
344           used for instance by bignum. The default is "undef", thus the
345           following operation creates a Math::BigInt, not a Math::BigFloat:
346
347               my $i = Math::BigInt->new(123);
348               my $f = Math::BigFloat->new('123.1');
349
350               print $i + $f, "\n";                # prints 246
351
352       downgrade()
353           Set/get the class for downgrading. The default is "undef".
354           Downgrading is not done by Math::BigInt.
355
356       modify()
357               $x->modify('bpowd');
358
359           This method returns 0 if the object can be modified with the given
360           operation, or 1 if not.
361
362           This is used for instance by Math::BigInt::Constant.
363
364       config()
365               use Data::Dumper;
366
367               print Dumper ( Math::BigInt->config() );
368               print Math::BigInt->config()->{lib},"\n";
369               print Math::BigInt->config('lib')},"\n";
370
371           Returns a hash containing the configuration, e.g. the version
372           number, lib loaded etc. The following hash keys are currently
373           filled in with the appropriate information.
374
375               key           Description
376                             Example
377               ============================================================
378               lib           Name of the low-level math library
379                             Math::BigInt::Calc
380               lib_version   Version of low-level math library (see 'lib')
381                             0.30
382               class         The class name of config() you just called
383                             Math::BigInt
384               upgrade       To which class math operations might be
385                             upgraded Math::BigFloat
386               downgrade     To which class math operations might be
387                             downgraded undef
388               precision     Global precision
389                             undef
390               accuracy      Global accuracy
391                             undef
392               round_mode    Global round mode
393                             even
394               version       version number of the class you used
395                             1.61
396               div_scale     Fallback accuracy for div
397                             40
398               trap_nan      If true, traps creation of NaN via croak()
399                             1
400               trap_inf      If true, traps creation of +inf/-inf via croak()
401                             1
402
403           The following values can be set by passing "config()" a reference
404           to a hash:
405
406                   accuracy precision round_mode div_scale
407                   upgrade downgrade trap_inf trap_nan
408
409           Example:
410
411               $new_cfg = Math::BigInt->config(
412                   { trap_inf => 1, precision => 5 }
413               );
414
415   Constructor methods
416       new()
417               $x = Math::BigInt->new($str,$A,$P,$R);
418
419           Creates a new Math::BigInt object from a scalar or another
420           Math::BigInt object.  The input is accepted as decimal, hexadecimal
421           (with leading '0x') or binary (with leading '0b').
422
423           See "Input" for more info on accepted input formats.
424
425       from_hex()
426               $x = Math::BigInt->from_hex("0xcafe");    # input is hexadecimal
427
428           Interpret input as a hexadecimal string. A "0x" or "x" prefix is
429           optional. A single underscore character may be placed right after
430           the prefix, if present, or between any two digits. If the input is
431           invalid, a NaN is returned.
432
433       from_oct()
434               $x = Math::BigInt->from_oct("0775");      # input is octal
435
436           Interpret the input as an octal string and return the corresponding
437           value. A "0" (zero) prefix is optional. A single underscore
438           character may be placed right after the prefix, if present, or
439           between any two digits. If the input is invalid, a NaN is returned.
440
441       from_bin()
442               $x = Math::BigInt->from_bin("0b10011");   # input is binary
443
444           Interpret the input as a binary string. A "0b" or "b" prefix is
445           optional. A single underscore character may be placed right after
446           the prefix, if present, or between any two digits. If the input is
447           invalid, a NaN is returned.
448
449       from_bytes()
450               $x = Math::BigInt->from_bytes("\xf3\x6b");  # $x = 62315
451
452           Interpret the input as a byte string, assuming big endian byte
453           order. The output is always a non-negative, finite integer.
454
455           In some special cases, from_bytes() matches the conversion done by
456           unpack():
457
458               $b = "\x4e";                             # one char byte string
459               $x = Math::BigInt->from_bytes($b);       # = 78
460               $y = unpack "C", $b;                     # ditto, but scalar
461
462               $b = "\xf3\x6b";                         # two char byte string
463               $x = Math::BigInt->from_bytes($b);       # = 62315
464               $y = unpack "S>", $b;                    # ditto, but scalar
465
466               $b = "\x2d\xe0\x49\xad";                 # four char byte string
467               $x = Math::BigInt->from_bytes($b);       # = 769673645
468               $y = unpack "L>", $b;                    # ditto, but scalar
469
470               $b = "\x2d\xe0\x49\xad\x2d\xe0\x49\xad"; # eight char byte string
471               $x = Math::BigInt->from_bytes($b);       # = 3305723134637787565
472               $y = unpack "Q>", $b;                    # ditto, but scalar
473
474       bzero()
475               $x = Math::BigInt->bzero();
476               $x->bzero();
477
478           Returns a new Math::BigInt object representing zero. If used as an
479           instance method, assigns the value to the invocand.
480
481       bone()
482               $x = Math::BigInt->bone();          # +1
483               $x = Math::BigInt->bone("+");       # +1
484               $x = Math::BigInt->bone("-");       # -1
485               $x->bone();                         # +1
486               $x->bone("+");                      # +1
487               $x->bone('-');                      # -1
488
489           Creates a new Math::BigInt object representing one. The optional
490           argument is either '-' or '+', indicating whether you want plus one
491           or minus one. If used as an instance method, assigns the value to
492           the invocand.
493
494       binf()
495               $x = Math::BigInt->binf($sign);
496
497           Creates a new Math::BigInt object representing infinity. The
498           optional argument is either '-' or '+', indicating whether you want
499           infinity or minus infinity.  If used as an instance method, assigns
500           the value to the invocand.
501
502               $x->binf();
503               $x->binf('-');
504
505       bnan()
506               $x = Math::BigInt->bnan();
507
508           Creates a new Math::BigInt object representing NaN (Not A Number).
509           If used as an instance method, assigns the value to the invocand.
510
511               $x->bnan();
512
513       bpi()
514               $x = Math::BigInt->bpi(100);        # 3
515               $x->bpi(100);                       # 3
516
517           Creates a new Math::BigInt object representing PI. If used as an
518           instance method, assigns the value to the invocand. With
519           Math::BigInt this always returns 3.
520
521           If upgrading is in effect, returns PI, rounded to N digits with the
522           current rounding mode:
523
524               use Math::BigFloat;
525               use Math::BigInt upgrade => "Math::BigFloat";
526               print Math::BigInt->bpi(3), "\n";           # 3.14
527               print Math::BigInt->bpi(100), "\n";         # 3.1415....
528
529       copy()
530               $x->copy();         # make a true copy of $x (unlike $y = $x)
531
532       as_int()
533       as_number()
534           These methods are called when Math::BigInt encounters an object it
535           doesn't know how to handle. For instance, assume $x is a
536           Math::BigInt, or subclass thereof, and $y is defined, but not a
537           Math::BigInt, or subclass thereof. If you do
538
539               $x -> badd($y);
540
541           $y needs to be converted into an object that $x can deal with. This
542           is done by first checking if $y is something that $x might be
543           upgraded to. If that is the case, no further attempts are made. The
544           next is to see if $y supports the method "as_int()". If it does,
545           "as_int()" is called, but if it doesn't, the next thing is to see
546           if $y supports the method "as_number()". If it does, "as_number()"
547           is called. The method "as_int()" (and "as_number()") is expected to
548           return either an object that has the same class as $x, a subclass
549           thereof, or a string that "ref($x)->new()" can parse to create an
550           object.
551
552           "as_number()" is an alias to "as_int()". "as_number" was introduced
553           in v1.22, while "as_int()" was introduced in v1.68.
554
555           In Math::BigInt, "as_int()" has the same effect as "copy()".
556
557   Boolean methods
558       None of these methods modify the invocand object.
559
560       is_zero()
561               $x->is_zero();              # true if $x is 0
562
563           Returns true if the invocand is zero and false otherwise.
564
565       is_one( [ SIGN ])
566               $x->is_one();               # true if $x is +1
567               $x->is_one("+");            # ditto
568               $x->is_one("-");            # true if $x is -1
569
570           Returns true if the invocand is one and false otherwise.
571
572       is_finite()
573               $x->is_finite();    # true if $x is not +inf, -inf or NaN
574
575           Returns true if the invocand is a finite number, i.e., it is
576           neither +inf, -inf, nor NaN.
577
578       is_inf( [ SIGN ] )
579               $x->is_inf();               # true if $x is +inf
580               $x->is_inf("+");            # ditto
581               $x->is_inf("-");            # true if $x is -inf
582
583           Returns true if the invocand is infinite and false otherwise.
584
585       is_nan()
586               $x->is_nan();               # true if $x is NaN
587
588       is_positive()
589       is_pos()
590               $x->is_positive();          # true if > 0
591               $x->is_pos();               # ditto
592
593           Returns true if the invocand is positive and false otherwise. A
594           "NaN" is neither positive nor negative.
595
596       is_negative()
597       is_neg()
598               $x->is_negative();          # true if < 0
599               $x->is_neg();               # ditto
600
601           Returns true if the invocand is negative and false otherwise. A
602           "NaN" is neither positive nor negative.
603
604       is_odd()
605               $x->is_odd();               # true if odd, false for even
606
607           Returns true if the invocand is odd and false otherwise. "NaN",
608           "+inf", and "-inf" are neither odd nor even.
609
610       is_even()
611               $x->is_even();              # true if $x is even
612
613           Returns true if the invocand is even and false otherwise. "NaN",
614           "+inf", "-inf" are not integers and are neither odd nor even.
615
616       is_int()
617               $x->is_int();               # true if $x is an integer
618
619           Returns true if the invocand is an integer and false otherwise.
620           "NaN", "+inf", "-inf" are not integers.
621
622   Comparison methods
623       None of these methods modify the invocand object. Note that a "NaN" is
624       neither less than, greater than, or equal to anything else, even a
625       "NaN".
626
627       bcmp()
628               $x->bcmp($y);
629
630           Returns -1, 0, 1 depending on whether $x is less than, equal to, or
631           grater than $y. Returns undef if any operand is a NaN.
632
633       bacmp()
634               $x->bacmp($y);
635
636           Returns -1, 0, 1 depending on whether the absolute value of $x is
637           less than, equal to, or grater than the absolute value of $y.
638           Returns undef if any operand is a NaN.
639
640       beq()
641               $x -> beq($y);
642
643           Returns true if and only if $x is equal to $y, and false otherwise.
644
645       bne()
646               $x -> bne($y);
647
648           Returns true if and only if $x is not equal to $y, and false
649           otherwise.
650
651       blt()
652               $x -> blt($y);
653
654           Returns true if and only if $x is equal to $y, and false otherwise.
655
656       ble()
657               $x -> ble($y);
658
659           Returns true if and only if $x is less than or equal to $y, and
660           false otherwise.
661
662       bgt()
663               $x -> bgt($y);
664
665           Returns true if and only if $x is greater than $y, and false
666           otherwise.
667
668       bge()
669               $x -> bge($y);
670
671           Returns true if and only if $x is greater than or equal to $y, and
672           false otherwise.
673
674   Arithmetic methods
675       These methods modify the invocand object and returns it.
676
677       bneg()
678               $x->bneg();
679
680           Negate the number, e.g. change the sign between '+' and '-', or
681           between '+inf' and '-inf', respectively. Does nothing for NaN or
682           zero.
683
684       babs()
685               $x->babs();
686
687           Set the number to its absolute value, e.g. change the sign from '-'
688           to '+' and from '-inf' to '+inf', respectively. Does nothing for
689           NaN or positive numbers.
690
691       bsgn()
692               $x->bsgn();
693
694           Signum function. Set the number to -1, 0, or 1, depending on
695           whether the number is negative, zero, or positive, respectively.
696           Does not modify NaNs.
697
698       bnorm()
699               $x->bnorm();                        # normalize (no-op)
700
701           Normalize the number. This is a no-op and is provided only for
702           backwards compatibility.
703
704       binc()
705               $x->binc();                 # increment x by 1
706
707       bdec()
708               $x->bdec();                 # decrement x by 1
709
710       badd()
711               $x->badd($y);               # addition (add $y to $x)
712
713       bsub()
714               $x->bsub($y);               # subtraction (subtract $y from $x)
715
716       bmul()
717               $x->bmul($y);               # multiplication (multiply $x by $y)
718
719       bmuladd()
720               $x->bmuladd($y,$z);
721
722           Multiply $x by $y, and then add $z to the result,
723
724           This method was added in v1.87 of Math::BigInt (June 2007).
725
726       bdiv()
727               $x->bdiv($y);               # divide, set $x to quotient
728
729           Divides $x by $y by doing floored division (F-division), where the
730           quotient is the floored (rounded towards negative infinity)
731           quotient of the two operands.  In list context, returns the
732           quotient and the remainder. The remainder is either zero or has the
733           same sign as the second operand. In scalar context, only the
734           quotient is returned.
735
736           The quotient is always the greatest integer less than or equal to
737           the real-valued quotient of the two operands, and the remainder
738           (when it is non-zero) always has the same sign as the second
739           operand; so, for example,
740
741                 1 /  4  => ( 0,  1)
742                 1 / -4  => (-1, -3)
743                -3 /  4  => (-1,  1)
744                -3 / -4  => ( 0, -3)
745               -11 /  2  => (-5,  1)
746                11 / -2  => (-5, -1)
747
748           The behavior of the overloaded operator % agrees with the behavior
749           of Perl's built-in % operator (as documented in the perlop
750           manpage), and the equation
751
752               $x == ($x / $y) * $y + ($x % $y)
753
754           holds true for any finite $x and finite, non-zero $y.
755
756           Perl's "use integer" might change the behaviour of % and / for
757           scalars. This is because under 'use integer' Perl does what the
758           underlying C library thinks is right, and this varies. However,
759           "use integer" does not change the way things are done with
760           Math::BigInt objects.
761
762       btdiv()
763               $x->btdiv($y);              # divide, set $x to quotient
764
765           Divides $x by $y by doing truncated division (T-division), where
766           quotient is the truncated (rouneded towards zero) quotient of the
767           two operands. In list context, returns the quotient and the
768           remainder. The remainder is either zero or has the same sign as the
769           first operand. In scalar context, only the quotient is returned.
770
771       bmod()
772               $x->bmod($y);               # modulus (x % y)
773
774           Returns $x modulo $y, i.e., the remainder after floored division
775           (F-division).  This method is like Perl's % operator. See "bdiv()".
776
777       btmod()
778               $x->btmod($y);              # modulus
779
780           Returns the remainer after truncated division (T-division). See
781           "btdiv()".
782
783       bmodinv()
784               $x->bmodinv($mod);          # modular multiplicative inverse
785
786           Returns the multiplicative inverse of $x modulo $mod. If
787
788               $y = $x -> copy() -> bmodinv($mod)
789
790           then $y is the number closest to zero, and with the same sign as
791           $mod, satisfying
792
793               ($x * $y) % $mod = 1 % $mod
794
795           If $x and $y are non-zero, they must be relative primes, i.e.,
796           "bgcd($y, $mod)==1". '"NaN"' is returned when no modular
797           multiplicative inverse exists.
798
799       bmodpow()
800               $num->bmodpow($exp,$mod);           # modular exponentiation
801                                                   # ($num**$exp % $mod)
802
803           Returns the value of $num taken to the power $exp in the modulus
804           $mod using binary exponentiation.  "bmodpow" is far superior to
805           writing
806
807               $num ** $exp % $mod
808
809           because it is much faster - it reduces internal variables into the
810           modulus whenever possible, so it operates on smaller numbers.
811
812           "bmodpow" also supports negative exponents.
813
814               bmodpow($num, -1, $mod)
815
816           is exactly equivalent to
817
818               bmodinv($num, $mod)
819
820       bpow()
821               $x->bpow($y);               # power of arguments (x ** y)
822
823           "bpow()" (and the rounding functions) now modifies the first
824           argument and returns it, unlike the old code which left it alone
825           and only returned the result. This is to be consistent with
826           "badd()" etc. The first three modifies $x, the last one won't:
827
828               print bpow($x,$i),"\n";         # modify $x
829               print $x->bpow($i),"\n";        # ditto
830               print $x **= $i,"\n";           # the same
831               print $x ** $i,"\n";            # leave $x alone
832
833           The form "$x **= $y" is faster than "$x = $x ** $y;", though.
834
835       blog()
836               $x->blog($base, $accuracy);         # logarithm of x to the base $base
837
838           If $base is not defined, Euler's number (e) is used:
839
840               print $x->blog(undef, 100);         # log(x) to 100 digits
841
842       bexp()
843               $x->bexp($accuracy);                # calculate e ** X
844
845           Calculates the expression "e ** $x" where "e" is Euler's number.
846
847           This method was added in v1.82 of Math::BigInt (April 2007).
848
849           See also "blog()".
850
851       bnok()
852               $x->bnok($y);               # x over y (binomial coefficient n over k)
853
854           Calculates the binomial coefficient n over k, also called the
855           "choose" function. The result is equivalent to:
856
857               ( n )      n!
858               | - |  = -------
859               ( k )    k!(n-k)!
860
861           This method was added in v1.84 of Math::BigInt (April 2007).
862
863       bsin()
864               my $x = Math::BigInt->new(1);
865               print $x->bsin(100), "\n";
866
867           Calculate the sine of $x, modifying $x in place.
868
869           In Math::BigInt, unless upgrading is in effect, the result is
870           truncated to an integer.
871
872           This method was added in v1.87 of Math::BigInt (June 2007).
873
874       bcos()
875               my $x = Math::BigInt->new(1);
876               print $x->bcos(100), "\n";
877
878           Calculate the cosine of $x, modifying $x in place.
879
880           In Math::BigInt, unless upgrading is in effect, the result is
881           truncated to an integer.
882
883           This method was added in v1.87 of Math::BigInt (June 2007).
884
885       batan()
886               my $x = Math::BigFloat->new(0.5);
887               print $x->batan(100), "\n";
888
889           Calculate the arcus tangens of $x, modifying $x in place.
890
891           In Math::BigInt, unless upgrading is in effect, the result is
892           truncated to an integer.
893
894           This method was added in v1.87 of Math::BigInt (June 2007).
895
896       batan2()
897               my $x = Math::BigInt->new(1);
898               my $y = Math::BigInt->new(1);
899               print $y->batan2($x), "\n";
900
901           Calculate the arcus tangens of $y divided by $x, modifying $y in
902           place.
903
904           In Math::BigInt, unless upgrading is in effect, the result is
905           truncated to an integer.
906
907           This method was added in v1.87 of Math::BigInt (June 2007).
908
909       bsqrt()
910               $x->bsqrt();                # calculate square-root
911
912           "bsqrt()" returns the square root truncated to an integer.
913
914           If you want a better approximation of the square root, then use:
915
916               $x = Math::BigFloat->new(12);
917               Math::BigFloat->precision(0);
918               Math::BigFloat->round_mode('even');
919               print $x->copy->bsqrt(),"\n";           # 4
920
921               Math::BigFloat->precision(2);
922               print $x->bsqrt(),"\n";                 # 3.46
923               print $x->bsqrt(3),"\n";                # 3.464
924
925       broot()
926               $x->broot($N);
927
928           Calculates the N'th root of $x.
929
930       bfac()
931               $x->bfac();                 # factorial of $x (1*2*3*4*..*$x)
932
933           Returns the factorial of $x, i.e., the product of all positive
934           integers up to and including $x.
935
936       bdfac()
937               $x->bdfac();                # double factorial of $x (1*2*3*4*..*$x)
938
939           Returns the double factorial of $x. If $x is an even integer,
940           returns the product of all positive, even integers up to and
941           including $x, i.e., 2*4*6*...*$x. If $x is an odd integer, returns
942           the product of all positive, odd integers, i.e., 1*3*5*...*$x.
943
944       bfib()
945               $F = $n->bfib();            # a single Fibonacci number
946               @F = $n->bfib();            # a list of Fibonacci numbers
947
948           In scalar context, returns a single Fibonacci number. In list
949           context, returns a list of Fibonacci numbers. The invocand is the
950           last element in the output.
951
952           The Fibonacci sequence is defined by
953
954               F(0) = 0
955               F(1) = 1
956               F(n) = F(n-1) + F(n-2)
957
958           In list context, F(0) and F(n) is the first and last number in the
959           output, respectively. For example, if $n is 12, then "@F =
960           $n->bfib()" returns the following values, F(0) to F(12):
961
962               0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144
963
964           The sequence can also be extended to negative index n using the re-
965           arranged recurrence relation
966
967               F(n-2) = F(n) - F(n-1)
968
969           giving the bidirectional sequence
970
971                  n  -7  -6  -5  -4  -3  -2  -1   0   1   2   3   4   5   6   7
972               F(n)  13  -8   5  -3   2  -1   1   0   1   1   2   3   5   8  13
973
974           If $n is -12, the following values, F(0) to F(12), are returned:
975
976               0, 1, -1, 2, -3, 5, -8, 13, -21, 34, -55, 89, -144
977
978       blucas()
979               $F = $n->blucas();          # a single Lucas number
980               @F = $n->blucas();          # a list of Lucas numbers
981
982           In scalar context, returns a single Lucas number. In list context,
983           returns a list of Lucas numbers. The invocand is the last element
984           in the output.
985
986           The Lucas sequence is defined by
987
988               L(0) = 2
989               L(1) = 1
990               L(n) = L(n-1) + L(n-2)
991
992           In list context, L(0) and L(n) is the first and last number in the
993           output, respectively. For example, if $n is 12, then "@L =
994           $n->blucas()" returns the following values, L(0) to L(12):
995
996               2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, 199, 322
997
998           The sequence can also be extended to negative index n using the re-
999           arranged recurrence relation
1000
1001               L(n-2) = L(n) - L(n-1)
1002
1003           giving the bidirectional sequence
1004
1005                  n  -7  -6  -5  -4  -3  -2  -1   0   1   2   3   4   5   6   7
1006               L(n)  29 -18  11  -7   4  -3   1   2   1   3   4   7  11  18  29
1007
1008           If $n is -12, the following values, L(0) to L(-12), are returned:
1009
1010               2, 1, -3, 4, -7, 11, -18, 29, -47, 76, -123, 199, -322
1011
1012       brsft()
1013               $x->brsft($n);              # right shift $n places in base 2
1014               $x->brsft($n, $b);          # right shift $n places in base $b
1015
1016           The latter is equivalent to
1017
1018               $x -> bdiv($b -> copy() -> bpow($n))
1019
1020       blsft()
1021               $x->blsft($n);              # left shift $n places in base 2
1022               $x->blsft($n, $b);          # left shift $n places in base $b
1023
1024           The latter is equivalent to
1025
1026               $x -> bmul($b -> copy() -> bpow($n))
1027
1028   Bitwise methods
1029       band()
1030               $x->band($y);               # bitwise and
1031
1032       bior()
1033               $x->bior($y);               # bitwise inclusive or
1034
1035       bxor()
1036               $x->bxor($y);               # bitwise exclusive or
1037
1038       bnot()
1039               $x->bnot();                 # bitwise not (two's complement)
1040
1041           Two's complement (bitwise not). This is equivalent to, but faster
1042           than,
1043
1044               $x->binc()->bneg();
1045
1046   Rounding methods
1047       round()
1048               $x->round($A,$P,$round_mode);
1049
1050           Round $x to accuracy $A or precision $P using the round mode
1051           $round_mode.
1052
1053       bround()
1054               $x->bround($N);               # accuracy: preserve $N digits
1055
1056           Rounds $x to an accuracy of $N digits.
1057
1058       bfround()
1059               $x->bfround($N);
1060
1061           Rounds to a multiple of 10**$N. Examples:
1062
1063               Input            N          Result
1064
1065               123456.123456    3          123500
1066               123456.123456    2          123450
1067               123456.123456   -2          123456.12
1068               123456.123456   -3          123456.123
1069
1070       bfloor()
1071               $x->bfloor();
1072
1073           Round $x towards minus infinity, i.e., set $x to the largest
1074           integer less than or equal to $x.
1075
1076       bceil()
1077               $x->bceil();
1078
1079           Round $x towards plus infinity, i.e., set $x to the smallest
1080           integer greater than or equal to $x).
1081
1082       bint()
1083               $x->bint();
1084
1085           Round $x towards zero.
1086
1087   Other mathematical methods
1088       bgcd()
1089               $x -> bgcd($y);             # GCD of $x and $y
1090               $x -> bgcd($y, $z, ...);    # GCD of $x, $y, $z, ...
1091
1092           Returns the greatest common divisor (GCD).
1093
1094       blcm()
1095               $x -> blcm($y);             # LCM of $x and $y
1096               $x -> blcm($y, $z, ...);    # LCM of $x, $y, $z, ...
1097
1098           Returns the least common multiple (LCM).
1099
1100   Object property methods
1101       sign()
1102               $x->sign();
1103
1104           Return the sign, of $x, meaning either "+", "-", "-inf", "+inf" or
1105           NaN.
1106
1107           If you want $x to have a certain sign, use one of the following
1108           methods:
1109
1110               $x->babs();                 # '+'
1111               $x->babs()->bneg();         # '-'
1112               $x->bnan();                 # 'NaN'
1113               $x->binf();                 # '+inf'
1114               $x->binf('-');              # '-inf'
1115
1116       digit()
1117               $x->digit($n);       # return the nth digit, counting from right
1118
1119           If $n is negative, returns the digit counting from left.
1120
1121       length()
1122               $x->length();
1123               ($xl, $fl) = $x->length();
1124
1125           Returns the number of digits in the decimal representation of the
1126           number. In list context, returns the length of the integer and
1127           fraction part. For Math::BigInt objects, the length of the fraction
1128           part is always 0.
1129
1130           The following probably doesn't do what you expect:
1131
1132               $c = Math::BigInt->new(123);
1133               print $c->length(),"\n";                # prints 30
1134
1135           It prints both the number of digits in the number and in the
1136           fraction part since print calls "length()" in list context. Use
1137           something like:
1138
1139               print scalar $c->length(),"\n";         # prints 3
1140
1141       mantissa()
1142               $x->mantissa();
1143
1144           Return the signed mantissa of $x as a Math::BigInt.
1145
1146       exponent()
1147               $x->exponent();
1148
1149           Return the exponent of $x as a Math::BigInt.
1150
1151       parts()
1152               $x->parts();
1153
1154           Returns the significand (mantissa) and the exponent as integers. In
1155           Math::BigFloat, both are returned as Math::BigInt objects.
1156
1157       sparts()
1158           Returns the significand (mantissa) and the exponent as integers. In
1159           scalar context, only the significand is returned. The significand
1160           is the integer with the smallest absolute value. The output of
1161           "sparts()" corresponds to the output from "bsstr()".
1162
1163           In Math::BigInt, this method is identical to "parts()".
1164
1165       nparts()
1166           Returns the significand (mantissa) and exponent corresponding to
1167           normalized notation. In scalar context, only the significand is
1168           returned. For finite non-zero numbers, the significand's absolute
1169           value is greater than or equal to 1 and less than 10. The output of
1170           "nparts()" corresponds to the output from "bnstr()". In
1171           Math::BigInt, if the significand can not be represented as an
1172           integer, upgrading is performed or NaN is returned.
1173
1174       eparts()
1175           Returns the significand (mantissa) and exponent corresponding to
1176           engineering notation. In scalar context, only the significand is
1177           returned. For finite non-zero numbers, the significand's absolute
1178           value is greater than or equal to 1 and less than 1000, and the
1179           exponent is a multiple of 3. The output of "eparts()" corresponds
1180           to the output from "bestr()". In Math::BigInt, if the significand
1181           can not be represented as an integer, upgrading is performed or NaN
1182           is returned.
1183
1184       dparts()
1185           Returns the integer part and the fraction part. If the fraction
1186           part can not be represented as an integer, upgrading is performed
1187           or NaN is returned. The output of "dparts()" corresponds to the
1188           output from "bdstr()".
1189
1190   String conversion methods
1191       bstr()
1192           Returns a string representing the number using decimal notation. In
1193           Math::BigFloat, the output is zero padded according to the current
1194           accuracy or precision, if any of those are defined.
1195
1196       bsstr()
1197           Returns a string representing the number using scientific notation
1198           where both the significand (mantissa) and the exponent are
1199           integers. The output corresponds to the output from "sparts()".
1200
1201                 123 is returned as "123e+0"
1202                1230 is returned as "123e+1"
1203               12300 is returned as "123e+2"
1204               12000 is returned as "12e+3"
1205               10000 is returned as "1e+4"
1206
1207       bnstr()
1208           Returns a string representing the number using normalized notation,
1209           the most common variant of scientific notation. For finite non-zero
1210           numbers, the absolute value of the significand is less than or
1211           equal to 1 and less than 10.  The output corresponds to the output
1212           from "nparts()".
1213
1214                 123 is returned as "1.23e+2"
1215                1230 is returned as "1.23e+3"
1216               12300 is returned as "1.23e+4"
1217               12000 is returned as "1.2e+4"
1218               10000 is returned as "1e+4"
1219
1220       bestr()
1221           Returns a string representing the number using engineering
1222           notation. For finite non-zero numbers, the absolute value of the
1223           significand is less than or equal to 1 and less than 1000, and the
1224           exponent is a multiple of 3. The output corresponds to the output
1225           from "eparts()".
1226
1227                 123 is returned as "123e+0"
1228                1230 is returned as "1.23e+3"
1229               12300 is returned as "12.3e+3"
1230               12000 is returned as "12e+3"
1231               10000 is returned as "10e+3"
1232
1233       bdstr()
1234           Returns a string representing the number using decimal notation.
1235           The output corresponds to the output from "dparts()".
1236
1237                 123 is returned as "123"
1238                1230 is returned as "1230"
1239               12300 is returned as "12300"
1240               12000 is returned as "12000"
1241               10000 is returned as "10000"
1242
1243       to_hex()
1244               $x->to_hex();
1245
1246           Returns a hexadecimal string representation of the number.
1247
1248       to_bin()
1249               $x->to_bin();
1250
1251           Returns a binary string representation of the number.
1252
1253       to_oct()
1254               $x->to_oct();
1255
1256           Returns an octal string representation of the number.
1257
1258       to_bytes()
1259               $x = Math::BigInt->new("1667327589");
1260               $s = $x->to_bytes();                    # $s = "cafe"
1261
1262           Returns a byte string representation of the number using big endian
1263           byte order. The invocand must be a non-negative, finite integer.
1264
1265       as_hex()
1266               $x->as_hex();
1267
1268           As, "to_hex()", but with a "0x" prefix.
1269
1270       as_bin()
1271               $x->as_bin();
1272
1273           As, "to_bin()", but with a "0b" prefix.
1274
1275       as_oct()
1276               $x->as_oct();
1277
1278           As, "to_oct()", but with a "0" prefix.
1279
1280       as_bytes()
1281           This is just an alias for "to_bytes()".
1282
1283   Other conversion methods
1284       numify()
1285               print $x->numify();
1286
1287           Returns a Perl scalar from $x. It is used automatically whenever a
1288           scalar is needed, for instance in array index operations.
1289

ACCURACY and PRECISION

1291       Math::BigInt and Math::BigFloat have full support for accuracy and
1292       precision based rounding, both automatically after every operation, as
1293       well as manually.
1294
1295       This section describes the accuracy/precision handling in Math::BigInt
1296       and Math::BigFloat as it used to be and as it is now, complete with an
1297       explanation of all terms and abbreviations.
1298
1299       Not yet implemented things (but with correct description) are marked
1300       with '!', things that need to be answered are marked with '?'.
1301
1302       In the next paragraph follows a short description of terms used here
1303       (because these may differ from terms used by others people or
1304       documentation).
1305
1306       During the rest of this document, the shortcuts A (for accuracy), P
1307       (for precision), F (fallback) and R (rounding mode) are be used.
1308
1309   Precision P
1310       Precision is a fixed number of digits before (positive) or after
1311       (negative) the decimal point. For example, 123.45 has a precision of
1312       -2. 0 means an integer like 123 (or 120). A precision of 2 means at
1313       least two digits to the left of the decimal point are zero, so 123 with
1314       P = 1 becomes 120. Note that numbers with zeros before the decimal
1315       point may have different precisions, because 1200 can have P = 0, 1 or
1316       2 (depending on what the initial value was). It could also have p < 0,
1317       when the digits after the decimal point are zero.
1318
1319       The string output (of floating point numbers) is padded with zeros:
1320
1321           Initial value    P      A       Result          String
1322           ------------------------------------------------------------
1323           1234.01         -3              1000            1000
1324           1234            -2              1200            1200
1325           1234.5          -1              1230            1230
1326           1234.001         1              1234            1234.0
1327           1234.01          0              1234            1234
1328           1234.01          2              1234.01         1234.01
1329           1234.01          5              1234.01         1234.01000
1330
1331       For Math::BigInt objects, no padding occurs.
1332
1333   Accuracy A
1334       Number of significant digits. Leading zeros are not counted. A number
1335       may have an accuracy greater than the non-zero digits when there are
1336       zeros in it or trailing zeros. For example, 123.456 has A of 6, 10203
1337       has 5, 123.0506 has 7, 123.45000 has 8 and 0.000123 has 3.
1338
1339       The string output (of floating point numbers) is padded with zeros:
1340
1341           Initial value    P      A       Result          String
1342           ------------------------------------------------------------
1343           1234.01                 3       1230            1230
1344           1234.01                 6       1234.01         1234.01
1345           1234.1                  8       1234.1          1234.1000
1346
1347       For Math::BigInt objects, no padding occurs.
1348
1349   Fallback F
1350       When both A and P are undefined, this is used as a fallback accuracy
1351       when dividing numbers.
1352
1353   Rounding mode R
1354       When rounding a number, different 'styles' or 'kinds' of rounding are
1355       possible.  (Note that random rounding, as in Math::Round, is not
1356       implemented.)
1357
1358       'trunc'
1359           truncation invariably removes all digits following the rounding
1360           place, replacing them with zeros. Thus, 987.65 rounded to tens (P =
1361           1) becomes 980, and rounded to the fourth sigdig becomes 987.6 (A =
1362           4). 123.456 rounded to the second place after the decimal point (P
1363           = -2) becomes 123.46.
1364
1365           All other implemented styles of rounding attempt to round to the
1366           "nearest digit." If the digit D immediately to the right of the
1367           rounding place (skipping the decimal point) is greater than 5, the
1368           number is incremented at the rounding place (possibly causing a
1369           cascade of incrementation): e.g. when rounding to units, 0.9 rounds
1370           to 1, and -19.9 rounds to -20. If D < 5, the number is similarly
1371           truncated at the rounding place: e.g. when rounding to units, 0.4
1372           rounds to 0, and -19.4 rounds to -19.
1373
1374           However the results of other styles of rounding differ if the digit
1375           immediately to the right of the rounding place (skipping the
1376           decimal point) is 5 and if there are no digits, or no digits other
1377           than 0, after that 5. In such cases:
1378
1379       'even'
1380           rounds the digit at the rounding place to 0, 2, 4, 6, or 8 if it is
1381           not already. E.g., when rounding to the first sigdig, 0.45 becomes
1382           0.4, -0.55 becomes -0.6, but 0.4501 becomes 0.5.
1383
1384       'odd'
1385           rounds the digit at the rounding place to 1, 3, 5, 7, or 9 if it is
1386           not already. E.g., when rounding to the first sigdig, 0.45 becomes
1387           0.5, -0.55 becomes -0.5, but 0.5501 becomes 0.6.
1388
1389       '+inf'
1390           round to plus infinity, i.e. always round up. E.g., when rounding
1391           to the first sigdig, 0.45 becomes 0.5, -0.55 becomes -0.5, and
1392           0.4501 also becomes 0.5.
1393
1394       '-inf'
1395           round to minus infinity, i.e. always round down. E.g., when
1396           rounding to the first sigdig, 0.45 becomes 0.4, -0.55 becomes -0.6,
1397           but 0.4501 becomes 0.5.
1398
1399       'zero'
1400           round to zero, i.e. positive numbers down, negative ones up. E.g.,
1401           when rounding to the first sigdig, 0.45 becomes 0.4, -0.55 becomes
1402           -0.5, but 0.4501 becomes 0.5.
1403
1404       'common'
1405           round up if the digit immediately to the right of the rounding
1406           place is 5 or greater, otherwise round down. E.g., 0.15 becomes 0.2
1407           and 0.149 becomes 0.1.
1408
1409       The handling of A & P in MBI/MBF (the old core code shipped with Perl
1410       versions <= 5.7.2) is like this:
1411
1412       Precision
1413             * bfround($p) is able to round to $p number of digits after the decimal
1414               point
1415             * otherwise P is unused
1416
1417       Accuracy (significant digits)
1418             * bround($a) rounds to $a significant digits
1419             * only bdiv() and bsqrt() take A as (optional) parameter
1420               + other operations simply create the same number (bneg etc), or
1421                 more (bmul) of digits
1422               + rounding/truncating is only done when explicitly calling one
1423                 of bround or bfround, and never for Math::BigInt (not implemented)
1424             * bsqrt() simply hands its accuracy argument over to bdiv.
1425             * the documentation and the comment in the code indicate two
1426               different ways on how bdiv() determines the maximum number
1427               of digits it should calculate, and the actual code does yet
1428               another thing
1429               POD:
1430                 max($Math::BigFloat::div_scale,length(dividend)+length(divisor))
1431               Comment:
1432                 result has at most max(scale, length(dividend), length(divisor)) digits
1433               Actual code:
1434                 scale = max(scale, length(dividend)-1,length(divisor)-1);
1435                 scale += length(divisor) - length(dividend);
1436               So for lx = 3, ly = 9, scale = 10, scale will actually be 16 (10
1437               So for lx = 3, ly = 9, scale = 10, scale will actually be 16
1438               (10+9-3). Actually, the 'difference' added to the scale is cal-
1439               culated from the number of "significant digits" in dividend and
1440               divisor, which is derived by looking at the length of the man-
1441               tissa. Which is wrong, since it includes the + sign (oops) and
1442               actually gets 2 for '+100' and 4 for '+101'. Oops again. Thus
1443               124/3 with div_scale=1 will get you '41.3' based on the strange
1444               assumption that 124 has 3 significant digits, while 120/7 will
1445               get you '17', not '17.1' since 120 is thought to have 2 signif-
1446               icant digits. The rounding after the division then uses the
1447               remainder and $y to determine whether it must round up or down.
1448            ?  I have no idea which is the right way. That's why I used a slightly more
1449            ?  simple scheme and tweaked the few failing testcases to match it.
1450
1451       This is how it works now:
1452
1453       Setting/Accessing
1454             * You can set the A global via Math::BigInt->accuracy() or
1455               Math::BigFloat->accuracy() or whatever class you are using.
1456             * You can also set P globally by using Math::SomeClass->precision()
1457               likewise.
1458             * Globals are classwide, and not inherited by subclasses.
1459             * to undefine A, use Math::SomeCLass->accuracy(undef);
1460             * to undefine P, use Math::SomeClass->precision(undef);
1461             * Setting Math::SomeClass->accuracy() clears automatically
1462               Math::SomeClass->precision(), and vice versa.
1463             * To be valid, A must be > 0, P can have any value.
1464             * If P is negative, this means round to the P'th place to the right of the
1465               decimal point; positive values mean to the left of the decimal point.
1466               P of 0 means round to integer.
1467             * to find out the current global A, use Math::SomeClass->accuracy()
1468             * to find out the current global P, use Math::SomeClass->precision()
1469             * use $x->accuracy() respective $x->precision() for the local
1470               setting of $x.
1471             * Please note that $x->accuracy() respective $x->precision()
1472               return eventually defined global A or P, when $x's A or P is not
1473               set.
1474
1475       Creating numbers
1476             * When you create a number, you can give the desired A or P via:
1477               $x = Math::BigInt->new($number,$A,$P);
1478             * Only one of A or P can be defined, otherwise the result is NaN
1479             * If no A or P is give ($x = Math::BigInt->new($number) form), then the
1480               globals (if set) will be used. Thus changing the global defaults later on
1481               will not change the A or P of previously created numbers (i.e., A and P of
1482               $x will be what was in effect when $x was created)
1483             * If given undef for A and P, NO rounding will occur, and the globals will
1484               NOT be used. This is used by subclasses to create numbers without
1485               suffering rounding in the parent. Thus a subclass is able to have its own
1486               globals enforced upon creation of a number by using
1487               $x = Math::BigInt->new($number,undef,undef):
1488
1489                   use Math::BigInt::SomeSubclass;
1490                   use Math::BigInt;
1491
1492                   Math::BigInt->accuracy(2);
1493                   Math::BigInt::SomeSubClass->accuracy(3);
1494                   $x = Math::BigInt::SomeSubClass->new(1234);
1495
1496               $x is now 1230, and not 1200. A subclass might choose to implement
1497               this otherwise, e.g. falling back to the parent's A and P.
1498
1499       Usage
1500             * If A or P are enabled/defined, they are used to round the result of each
1501               operation according to the rules below
1502             * Negative P is ignored in Math::BigInt, since Math::BigInt objects never
1503               have digits after the decimal point
1504             * Math::BigFloat uses Math::BigInt internally, but setting A or P inside
1505               Math::BigInt as globals does not tamper with the parts of a Math::BigFloat.
1506               A flag is used to mark all Math::BigFloat numbers as 'never round'.
1507
1508       Precedence
1509             * It only makes sense that a number has only one of A or P at a time.
1510               If you set either A or P on one object, or globally, the other one will
1511               be automatically cleared.
1512             * If two objects are involved in an operation, and one of them has A in
1513               effect, and the other P, this results in an error (NaN).
1514             * A takes precedence over P (Hint: A comes before P).
1515               If neither of them is defined, nothing is used, i.e. the result will have
1516               as many digits as it can (with an exception for bdiv/bsqrt) and will not
1517               be rounded.
1518             * There is another setting for bdiv() (and thus for bsqrt()). If neither of
1519               A or P is defined, bdiv() will use a fallback (F) of $div_scale digits.
1520               If either the dividend's or the divisor's mantissa has more digits than
1521               the value of F, the higher value will be used instead of F.
1522               This is to limit the digits (A) of the result (just consider what would
1523               happen with unlimited A and P in the case of 1/3 :-)
1524             * bdiv will calculate (at least) 4 more digits than required (determined by
1525               A, P or F), and, if F is not used, round the result
1526               (this will still fail in the case of a result like 0.12345000000001 with A
1527               or P of 5, but this can not be helped - or can it?)
1528             * Thus you can have the math done by on Math::Big* class in two modi:
1529               + never round (this is the default):
1530                 This is done by setting A and P to undef. No math operation
1531                 will round the result, with bdiv() and bsqrt() as exceptions to guard
1532                 against overflows. You must explicitly call bround(), bfround() or
1533                 round() (the latter with parameters).
1534                 Note: Once you have rounded a number, the settings will 'stick' on it
1535                 and 'infect' all other numbers engaged in math operations with it, since
1536                 local settings have the highest precedence. So, to get SaferRound[tm],
1537                 use a copy() before rounding like this:
1538
1539                   $x = Math::BigFloat->new(12.34);
1540                   $y = Math::BigFloat->new(98.76);
1541                   $z = $x * $y;                           # 1218.6984
1542                   print $x->copy()->bround(3);            # 12.3 (but A is now 3!)
1543                   $z = $x * $y;                           # still 1218.6984, without
1544                                                           # copy would have been 1210!
1545
1546               + round after each op:
1547                 After each single operation (except for testing like is_zero()), the
1548                 method round() is called and the result is rounded appropriately. By
1549                 setting proper values for A and P, you can have all-the-same-A or
1550                 all-the-same-P modes. For example, Math::Currency might set A to undef,
1551                 and P to -2, globally.
1552
1553            ?Maybe an extra option that forbids local A & P settings would be in order,
1554            ?so that intermediate rounding does not 'poison' further math?
1555
1556       Overriding globals
1557             * you will be able to give A, P and R as an argument to all the calculation
1558               routines; the second parameter is A, the third one is P, and the fourth is
1559               R (shift right by one for binary operations like badd). P is used only if
1560               the first parameter (A) is undefined. These three parameters override the
1561               globals in the order detailed as follows, i.e. the first defined value
1562               wins:
1563               (local: per object, global: global default, parameter: argument to sub)
1564                 + parameter A
1565                 + parameter P
1566                 + local A (if defined on both of the operands: smaller one is taken)
1567                 + local P (if defined on both of the operands: bigger one is taken)
1568                 + global A
1569                 + global P
1570                 + global F
1571             * bsqrt() will hand its arguments to bdiv(), as it used to, only now for two
1572               arguments (A and P) instead of one
1573
1574       Local settings
1575             * You can set A or P locally by using $x->accuracy() or
1576               $x->precision()
1577               and thus force different A and P for different objects/numbers.
1578             * Setting A or P this way immediately rounds $x to the new value.
1579             * $x->accuracy() clears $x->precision(), and vice versa.
1580
1581       Rounding
1582             * the rounding routines will use the respective global or local settings.
1583               bround() is for accuracy rounding, while bfround() is for precision
1584             * the two rounding functions take as the second parameter one of the
1585               following rounding modes (R):
1586               'even', 'odd', '+inf', '-inf', 'zero', 'trunc', 'common'
1587             * you can set/get the global R by using Math::SomeClass->round_mode()
1588               or by setting $Math::SomeClass::round_mode
1589             * after each operation, $result->round() is called, and the result may
1590               eventually be rounded (that is, if A or P were set either locally,
1591               globally or as parameter to the operation)
1592             * to manually round a number, call $x->round($A,$P,$round_mode);
1593               this will round the number by using the appropriate rounding function
1594               and then normalize it.
1595             * rounding modifies the local settings of the number:
1596
1597                   $x = Math::BigFloat->new(123.456);
1598                   $x->accuracy(5);
1599                   $x->bround(4);
1600
1601               Here 4 takes precedence over 5, so 123.5 is the result and $x->accuracy()
1602               will be 4 from now on.
1603
1604       Default values
1605             * R: 'even'
1606             * F: 40
1607             * A: undef
1608             * P: undef
1609
1610       Remarks
1611             * The defaults are set up so that the new code gives the same results as
1612               the old code (except in a few cases on bdiv):
1613               + Both A and P are undefined and thus will not be used for rounding
1614                 after each operation.
1615               + round() is thus a no-op, unless given extra parameters A and P
1616

Infinity and Not a Number

1618       While Math::BigInt has extensive handling of inf and NaN, certain
1619       quirks remain.
1620
1621       oct()/hex()
1622           These perl routines currently (as of Perl v.5.8.6) cannot handle
1623           passed inf.
1624
1625               te@linux:~> perl -wle 'print 2 ** 3333'
1626               Inf
1627               te@linux:~> perl -wle 'print 2 ** 3333 == 2 ** 3333'
1628               1
1629               te@linux:~> perl -wle 'print oct(2 ** 3333)'
1630               0
1631               te@linux:~> perl -wle 'print hex(2 ** 3333)'
1632               Illegal hexadecimal digit 'I' ignored at -e line 1.
1633               0
1634
1635           The same problems occur if you pass them Math::BigInt->binf()
1636           objects. Since overloading these routines is not possible, this
1637           cannot be fixed from Math::BigInt.
1638

INTERNALS

1640       You should neither care about nor depend on the internal
1641       representation; it might change without notice. Use ONLY method calls
1642       like "$x->sign();" instead relying on the internal representation.
1643
1644   MATH LIBRARY
1645       Math with the numbers is done (by default) by a module called
1646       "Math::BigInt::Calc". This is equivalent to saying:
1647
1648           use Math::BigInt try => 'Calc';
1649
1650       You can change this backend library by using:
1651
1652           use Math::BigInt try => 'GMP';
1653
1654       Note: General purpose packages should not be explicit about the library
1655       to use; let the script author decide which is best.
1656
1657       If your script works with huge numbers and Calc is too slow for them,
1658       you can also for the loading of one of these libraries and if none of
1659       them can be used, the code dies:
1660
1661           use Math::BigInt only => 'GMP,Pari';
1662
1663       The following would first try to find Math::BigInt::Foo, then
1664       Math::BigInt::Bar, and when this also fails, revert to
1665       Math::BigInt::Calc:
1666
1667           use Math::BigInt try => 'Foo,Math::BigInt::Bar';
1668
1669       The library that is loaded last is used. Note that this can be
1670       overwritten at any time by loading a different library, and numbers
1671       constructed with different libraries cannot be used in math operations
1672       together.
1673
1674       What library to use?
1675
1676       Note: General purpose packages should not be explicit about the library
1677       to use; let the script author decide which is best.
1678
1679       Math::BigInt::GMP and Math::BigInt::Pari are in cases involving big
1680       numbers much faster than Calc, however it is slower when dealing with
1681       very small numbers (less than about 20 digits) and when converting very
1682       large numbers to decimal (for instance for printing, rounding,
1683       calculating their length in decimal etc).
1684
1685       So please select carefully what library you want to use.
1686
1687       Different low-level libraries use different formats to store the
1688       numbers.  However, you should NOT depend on the number having a
1689       specific format internally.
1690
1691       See the respective math library module documentation for further
1692       details.
1693
1694   SIGN
1695       The sign is either '+', '-', 'NaN', '+inf' or '-inf'.
1696
1697       A sign of 'NaN' is used to represent the result when input arguments
1698       are not numbers or as a result of 0/0. '+inf' and '-inf' represent plus
1699       respectively minus infinity. You get '+inf' when dividing a positive
1700       number by 0, and '-inf' when dividing any negative number by 0.
1701

EXAMPLES

1703         use Math::BigInt;
1704
1705         sub bigint { Math::BigInt->new(shift); }
1706
1707         $x = Math::BigInt->bstr("1234")       # string "1234"
1708         $x = "$x";                            # same as bstr()
1709         $x = Math::BigInt->bneg("1234");      # Math::BigInt "-1234"
1710         $x = Math::BigInt->babs("-12345");    # Math::BigInt "12345"
1711         $x = Math::BigInt->bnorm("-0.00");    # Math::BigInt "0"
1712         $x = bigint(1) + bigint(2);           # Math::BigInt "3"
1713         $x = bigint(1) + "2";                 # ditto (auto-Math::BigIntify of "2")
1714         $x = bigint(1);                       # Math::BigInt "1"
1715         $x = $x + 5 / 2;                      # Math::BigInt "3"
1716         $x = $x ** 3;                         # Math::BigInt "27"
1717         $x *= 2;                              # Math::BigInt "54"
1718         $x = Math::BigInt->new(0);            # Math::BigInt "0"
1719         $x--;                                 # Math::BigInt "-1"
1720         $x = Math::BigInt->badd(4,5)          # Math::BigInt "9"
1721         print $x->bsstr();                    # 9e+0
1722
1723       Examples for rounding:
1724
1725         use Math::BigFloat;
1726         use Test::More;
1727
1728         $x = Math::BigFloat->new(123.4567);
1729         $y = Math::BigFloat->new(123.456789);
1730         Math::BigFloat->accuracy(4);          # no more A than 4
1731
1732         is ($x->copy()->bround(),123.4);      # even rounding
1733         print $x->copy()->bround(),"\n";      # 123.4
1734         Math::BigFloat->round_mode('odd');    # round to odd
1735         print $x->copy()->bround(),"\n";      # 123.5
1736         Math::BigFloat->accuracy(5);          # no more A than 5
1737         Math::BigFloat->round_mode('odd');    # round to odd
1738         print $x->copy()->bround(),"\n";      # 123.46
1739         $y = $x->copy()->bround(4),"\n";      # A = 4: 123.4
1740         print "$y, ",$y->accuracy(),"\n";     # 123.4, 4
1741
1742         Math::BigFloat->accuracy(undef);      # A not important now
1743         Math::BigFloat->precision(2);         # P important
1744         print $x->copy()->bnorm(),"\n";       # 123.46
1745         print $x->copy()->bround(),"\n";      # 123.46
1746
1747       Examples for converting:
1748
1749         my $x = Math::BigInt->new('0b1'.'01' x 123);
1750         print "bin: ",$x->as_bin()," hex:",$x->as_hex()," dec: ",$x,"\n";
1751

Autocreating constants

1753       After "use Math::BigInt ':constant'" all the integer decimal,
1754       hexadecimal and binary constants in the given scope are converted to
1755       "Math::BigInt". This conversion happens at compile time.
1756
1757       In particular,
1758
1759         perl -MMath::BigInt=:constant -e 'print 2**100,"\n"'
1760
1761       prints the integer value of "2**100". Note that without conversion of
1762       constants the expression 2**100 is calculated using Perl scalars.
1763
1764       Please note that strings and floating point constants are not affected,
1765       so that
1766
1767           use Math::BigInt qw/:constant/;
1768
1769           $x = 1234567890123456789012345678901234567890
1770                   + 123456789123456789;
1771           $y = '1234567890123456789012345678901234567890'
1772                   + '123456789123456789';
1773
1774       does not give you what you expect. You need an explicit
1775       Math::BigInt->new() around one of the operands. You should also quote
1776       large constants to protect loss of precision:
1777
1778           use Math::BigInt;
1779
1780           $x = Math::BigInt->new('1234567889123456789123456789123456789');
1781
1782       Without the quotes Perl would convert the large number to a floating
1783       point constant at compile time and then hand the result to
1784       Math::BigInt, which results in an truncated result or a NaN.
1785
1786       This also applies to integers that look like floating point constants:
1787
1788           use Math::BigInt ':constant';
1789
1790           print ref(123e2),"\n";
1791           print ref(123.2e2),"\n";
1792
1793       prints nothing but newlines. Use either bignum or Math::BigFloat to get
1794       this to work.
1795

PERFORMANCE

1797       Using the form $x += $y; etc over $x = $x + $y is faster, since a copy
1798       of $x must be made in the second case. For long numbers, the copy can
1799       eat up to 20% of the work (in the case of addition/subtraction, less
1800       for multiplication/division). If $y is very small compared to $x, the
1801       form $x += $y is MUCH faster than $x = $x + $y since making the copy of
1802       $x takes more time then the actual addition.
1803
1804       With a technique called copy-on-write, the cost of copying with
1805       overload could be minimized or even completely avoided. A test
1806       implementation of COW did show performance gains for overloaded math,
1807       but introduced a performance loss due to a constant overhead for all
1808       other operations. So Math::BigInt does currently not COW.
1809
1810       The rewritten version of this module (vs. v0.01) is slower on certain
1811       operations, like "new()", "bstr()" and "numify()". The reason are that
1812       it does now more work and handles much more cases. The time spent in
1813       these operations is usually gained in the other math operations so that
1814       code on the average should get (much) faster. If they don't, please
1815       contact the author.
1816
1817       Some operations may be slower for small numbers, but are significantly
1818       faster for big numbers. Other operations are now constant (O(1), like
1819       "bneg()", "babs()" etc), instead of O(N) and thus nearly always take
1820       much less time.  These optimizations were done on purpose.
1821
1822       If you find the Calc module to slow, try to install any of the
1823       replacement modules and see if they help you.
1824
1825   Alternative math libraries
1826       You can use an alternative library to drive Math::BigInt. See the
1827       section "MATH LIBRARY" for more information.
1828
1829       For more benchmark results see
1830       <http://bloodgate.com/perl/benchmarks.html>.
1831

SUBCLASSING

1833   Subclassing Math::BigInt
1834       The basic design of Math::BigInt allows simple subclasses with very
1835       little work, as long as a few simple rules are followed:
1836
1837       ·   The public API must remain consistent, i.e. if a sub-class is
1838           overloading addition, the sub-class must use the same name, in this
1839           case badd(). The reason for this is that Math::BigInt is optimized
1840           to call the object methods directly.
1841
1842       ·   The private object hash keys like "$x->{sign}" may not be changed,
1843           but additional keys can be added, like "$x->{_custom}".
1844
1845       ·   Accessor functions are available for all existing object hash keys
1846           and should be used instead of directly accessing the internal hash
1847           keys. The reason for this is that Math::BigInt itself has a
1848           pluggable interface which permits it to support different storage
1849           methods.
1850
1851       More complex sub-classes may have to replicate more of the logic
1852       internal of Math::BigInt if they need to change more basic behaviors. A
1853       subclass that needs to merely change the output only needs to overload
1854       "bstr()".
1855
1856       All other object methods and overloaded functions can be directly
1857       inherited from the parent class.
1858
1859       At the very minimum, any subclass needs to provide its own "new()" and
1860       can store additional hash keys in the object. There are also some
1861       package globals that must be defined, e.g.:
1862
1863           # Globals
1864           $accuracy = undef;
1865           $precision = -2;       # round to 2 decimal places
1866           $round_mode = 'even';
1867           $div_scale = 40;
1868
1869       Additionally, you might want to provide the following two globals to
1870       allow auto-upgrading and auto-downgrading to work correctly:
1871
1872           $upgrade = undef;
1873           $downgrade = undef;
1874
1875       This allows Math::BigInt to correctly retrieve package globals from the
1876       subclass, like $SubClass::precision. See t/Math/BigInt/Subclass.pm or
1877       t/Math/BigFloat/SubClass.pm completely functional subclass examples.
1878
1879       Don't forget to
1880
1881           use overload;
1882
1883       in your subclass to automatically inherit the overloading from the
1884       parent. If you like, you can change part of the overloading, look at
1885       Math::String for an example.
1886

UPGRADING

1888       When used like this:
1889
1890           use Math::BigInt upgrade => 'Foo::Bar';
1891
1892       certain operations 'upgrade' their calculation and thus the result to
1893       the class Foo::Bar. Usually this is used in conjunction with
1894       Math::BigFloat:
1895
1896           use Math::BigInt upgrade => 'Math::BigFloat';
1897
1898       As a shortcut, you can use the module bignum:
1899
1900           use bignum;
1901
1902       Also good for one-liners:
1903
1904           perl -Mbignum -le 'print 2 ** 255'
1905
1906       This makes it possible to mix arguments of different classes (as in 2.5
1907       + 2) as well es preserve accuracy (as in sqrt(3)).
1908
1909       Beware: This feature is not fully implemented yet.
1910
1911   Auto-upgrade
1912       The following methods upgrade themselves unconditionally; that is if
1913       upgrade is in effect, they always hands up their work:
1914
1915           div bsqrt blog bexp bpi bsin bcos batan batan2
1916
1917       All other methods upgrade themselves only when one (or all) of their
1918       arguments are of the class mentioned in $upgrade.
1919

EXPORTS

1921       "Math::BigInt" exports nothing by default, but can export the following
1922       methods:
1923
1924           bgcd
1925           blcm
1926

CAVEATS

1928       Some things might not work as you expect them. Below is documented what
1929       is known to be troublesome:
1930
1931       Comparing numbers as strings
1932           Both "bstr()" and "bsstr()" as well as stringify via overload drop
1933           the leading '+'. This is to be consistent with Perl and to make
1934           "cmp" (especially with overloading) to work as you expect. It also
1935           solves problems with "Test.pm" and Test::More, which stringify
1936           arguments before comparing them.
1937
1938           Mark Biggar said, when asked about to drop the '+' altogether, or
1939           make only "cmp" work:
1940
1941               I agree (with the first alternative), don't add the '+' on positive
1942               numbers.  It's not as important anymore with the new internal form
1943               for numbers.  It made doing things like abs and neg easier, but
1944               those have to be done differently now anyway.
1945
1946           So, the following examples now works as expected:
1947
1948               use Test::More tests => 1;
1949               use Math::BigInt;
1950
1951               my $x = Math::BigInt -> new(3*3);
1952               my $y = Math::BigInt -> new(3*3);
1953
1954               is($x,3*3, 'multiplication');
1955               print "$x eq 9" if $x eq $y;
1956               print "$x eq 9" if $x eq '9';
1957               print "$x eq 9" if $x eq 3*3;
1958
1959           Additionally, the following still works:
1960
1961               print "$x == 9" if $x == $y;
1962               print "$x == 9" if $x == 9;
1963               print "$x == 9" if $x == 3*3;
1964
1965           There is now a "bsstr()" method to get the string in scientific
1966           notation aka 1e+2 instead of 100. Be advised that overloaded 'eq'
1967           always uses bstr() for comparison, but Perl represents some numbers
1968           as 100 and others as 1e+308.  If in doubt, convert both arguments
1969           to Math::BigInt before comparing them as strings:
1970
1971               use Test::More tests => 3;
1972               use Math::BigInt;
1973
1974               $x = Math::BigInt->new('1e56'); $y = 1e56;
1975               is($x,$y);                     # fails
1976               is($x->bsstr(),$y);            # okay
1977               $y = Math::BigInt->new($y);
1978               is($x,$y);                     # okay
1979
1980           Alternatively, simply use "<=>" for comparisons, this always gets
1981           it right. There is not yet a way to get a number automatically
1982           represented as a string that matches exactly the way Perl
1983           represents it.
1984
1985           See also the section about "Infinity and Not a Number" for problems
1986           in comparing NaNs.
1987
1988       int()
1989           "int()" returns (at least for Perl v5.7.1 and up) another
1990           Math::BigInt, not a Perl scalar:
1991
1992               $x = Math::BigInt->new(123);
1993               $y = int($x);                           # 123 as a Math::BigInt
1994               $x = Math::BigFloat->new(123.45);
1995               $y = int($x);                           # 123 as a Math::BigFloat
1996
1997           If you want a real Perl scalar, use "numify()":
1998
1999               $y = $x->numify();                      # 123 as a scalar
2000
2001           This is seldom necessary, though, because this is done
2002           automatically, like when you access an array:
2003
2004               $z = $array[$x];                        # does work automatically
2005
2006       Modifying and =
2007           Beware of:
2008
2009               $x = Math::BigFloat->new(5);
2010               $y = $x;
2011
2012           This makes a second reference to the same object and stores it in
2013           $y. Thus anything that modifies $x (except overloaded operators)
2014           also modifies $y, and vice versa. Or in other words, "=" is only
2015           safe if you modify your Math::BigInt objects only via overloaded
2016           math. As soon as you use a method call it breaks:
2017
2018               $x->bmul(2);
2019               print "$x, $y\n";       # prints '10, 10'
2020
2021           If you want a true copy of $x, use:
2022
2023               $y = $x->copy();
2024
2025           You can also chain the calls like this, this first makes a copy and
2026           then multiply it by 2:
2027
2028               $y = $x->copy()->bmul(2);
2029
2030           See also the documentation for overload.pm regarding "=".
2031
2032       Overloading -$x
2033           The following:
2034
2035               $x = -$x;
2036
2037           is slower than
2038
2039               $x->bneg();
2040
2041           since overload calls "sub($x,0,1);" instead of "neg($x)". The first
2042           variant needs to preserve $x since it does not know that it later
2043           gets overwritten.  This makes a copy of $x and takes O(N), but
2044           $x->bneg() is O(1).
2045
2046       Mixing different object types
2047           With overloaded operators, it is the first (dominating) operand
2048           that determines which method is called. Here are some examples
2049           showing what actually gets called in various cases.
2050
2051               use Math::BigInt;
2052               use Math::BigFloat;
2053
2054               $mbf  = Math::BigFloat->new(5);
2055               $mbi2 = Math::BigInt->new(5);
2056               $mbi  = Math::BigInt->new(2);
2057                                               # what actually gets called:
2058               $float = $mbf + $mbi;           # $mbf->badd($mbi)
2059               $float = $mbf / $mbi;           # $mbf->bdiv($mbi)
2060               $integer = $mbi + $mbf;         # $mbi->badd($mbf)
2061               $integer = $mbi2 / $mbi;        # $mbi2->bdiv($mbi)
2062               $integer = $mbi2 / $mbf;        # $mbi2->bdiv($mbf)
2063
2064           For instance, Math::BigInt->bdiv() always returns a Math::BigInt,
2065           regardless of whether the second operant is a Math::BigFloat. To
2066           get a Math::BigFloat you either need to call the operation
2067           manually, make sure each operand already is a Math::BigFloat, or
2068           cast to that type via Math::BigFloat->new():
2069
2070               $float = Math::BigFloat->new($mbi2) / $mbi;     # = 2.5
2071
2072           Beware of casting the entire expression, as this would cast the
2073           result, at which point it is too late:
2074
2075               $float = Math::BigFloat->new($mbi2 / $mbi);     # = 2
2076
2077           Beware also of the order of more complicated expressions like:
2078
2079               $integer = ($mbi2 + $mbi) / $mbf;               # int / float => int
2080               $integer = $mbi2 / Math::BigFloat->new($mbi);   # ditto
2081
2082           If in doubt, break the expression into simpler terms, or cast all
2083           operands to the desired resulting type.
2084
2085           Scalar values are a bit different, since:
2086
2087               $float = 2 + $mbf;
2088               $float = $mbf + 2;
2089
2090           both result in the proper type due to the way the overloaded math
2091           works.
2092
2093           This section also applies to other overloaded math packages, like
2094           Math::String.
2095
2096           One solution to you problem might be autoupgrading|upgrading. See
2097           the pragmas bignum, bigint and bigrat for an easy way to do this.
2098

BUGS

2100       Please report any bugs or feature requests to "bug-math-bigint at
2101       rt.cpan.org", or through the web interface at
2102       <https://rt.cpan.org/Ticket/Create.html?Queue=Math-BigInt> (requires
2103       login).  We will be notified, and then you'll automatically be notified
2104       of progress on your bug as I make changes.
2105

SUPPORT

2107       You can find documentation for this module with the perldoc command.
2108
2109           perldoc Math::BigInt
2110
2111       You can also look for information at:
2112
2113       ·   RT: CPAN's request tracker
2114
2115           <https://rt.cpan.org/Public/Dist/Display.html?Name=Math-BigInt>
2116
2117       ·   AnnoCPAN: Annotated CPAN documentation
2118
2119           <http://annocpan.org/dist/Math-BigInt>
2120
2121       ·   CPAN Ratings
2122
2123           <http://cpanratings.perl.org/dist/Math-BigInt>
2124
2125       ·   Search CPAN
2126
2127           <http://search.cpan.org/dist/Math-BigInt/>
2128
2129       ·   CPAN Testers Matrix
2130
2131           <http://matrix.cpantesters.org/?dist=Math-BigInt>
2132
2133       ·   The Bignum mailing list
2134
2135           ·   Post to mailing list
2136
2137               "bignum at lists.scsys.co.uk"
2138
2139           ·   View mailing list
2140
2141               <http://lists.scsys.co.uk/pipermail/bignum/>
2142
2143           ·   Subscribe/Unsubscribe
2144
2145               <http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/bignum>
2146

LICENSE

2148       This program is free software; you may redistribute it and/or modify it
2149       under the same terms as Perl itself.
2150

SEE ALSO

2152       Math::BigFloat and Math::BigRat as well as the backends
2153       Math::BigInt::FastCalc, Math::BigInt::GMP, and Math::BigInt::Pari.
2154
2155       The pragmas bignum, bigint and bigrat also might be of interest because
2156       they solve the autoupgrading/downgrading issue, at least partly.
2157

AUTHORS

2159       ·   Mark Biggar, overloaded interface by Ilya Zakharevich, 1996-2001.
2160
2161       ·   Completely rewritten by Tels <http://bloodgate.com>, 2001-2008.
2162
2163       ·   Florian Ragwitz <flora@cpan.org>, 2010.
2164
2165       ·   Peter John Acklam <pjacklam@online.no>, 2011-.
2166
2167       Many people contributed in one or more ways to the final beast, see the
2168       file CREDITS for an (incomplete) list. If you miss your name, please
2169       drop me a mail. Thank you!
2170
2171
2172
2173perl v5.26.3                      2017-03-15                   Math::BigInt(3)
Impressum