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

ACCURACY and PRECISION

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

Infinity and Not a Number

1605       While Math::BigInt has extensive handling of inf and NaN, certain
1606       quirks remain.
1607
1608       oct()/hex()
1609           These perl routines currently (as of Perl v.5.8.6) cannot handle
1610           passed inf.
1611
1612               te@linux:~> perl -wle 'print 2 ** 3333'
1613               Inf
1614               te@linux:~> perl -wle 'print 2 ** 3333 == 2 ** 3333'
1615               1
1616               te@linux:~> perl -wle 'print oct(2 ** 3333)'
1617               0
1618               te@linux:~> perl -wle 'print hex(2 ** 3333)'
1619               Illegal hexadecimal digit 'I' ignored at -e line 1.
1620               0
1621
1622           The same problems occur if you pass them Math::BigInt->binf()
1623           objects. Since overloading these routines is not possible, this
1624           cannot be fixed from Math::BigInt.
1625

INTERNALS

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

EXAMPLES

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

Autocreating constants

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

PERFORMANCE

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

SUBCLASSING

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

UPGRADING

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

EXPORTS

1908       "Math::BigInt" exports nothing by default, but can export the following
1909       methods:
1910
1911           bgcd
1912           blcm
1913

CAVEATS

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

BUGS

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

SUPPORT

2094       You can find documentation for this module with the perldoc command.
2095
2096           perldoc Math::BigInt
2097
2098       You can also look for information at:
2099
2100       ·   RT: CPAN's request tracker
2101
2102           <https://rt.cpan.org/Public/Dist/Display.html?Name=Math-BigInt>
2103
2104       ·   AnnoCPAN: Annotated CPAN documentation
2105
2106           <http://annocpan.org/dist/Math-BigInt>
2107
2108       ·   CPAN Ratings
2109
2110           <http://cpanratings.perl.org/dist/Math-BigInt>
2111
2112       ·   Search CPAN
2113
2114           <http://search.cpan.org/dist/Math-BigInt/>
2115
2116       ·   CPAN Testers Matrix
2117
2118           <http://matrix.cpantesters.org/?dist=Math-BigInt>
2119
2120       ·   The Bignum mailing list
2121
2122           ·   Post to mailing list
2123
2124               "bignum at lists.scsys.co.uk"
2125
2126           ·   View mailing list
2127
2128               <http://lists.scsys.co.uk/pipermail/bignum/>
2129
2130           ·   Subscribe/Unsubscribe
2131
2132               <http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/bignum>
2133

LICENSE

2135       This program is free software; you may redistribute it and/or modify it
2136       under the same terms as Perl itself.
2137

SEE ALSO

2139       Math::BigFloat and Math::BigRat as well as the backends
2140       Math::BigInt::FastCalc, Math::BigInt::GMP, and Math::BigInt::Pari.
2141
2142       The pragmas bignum, bigint and bigrat also might be of interest because
2143       they solve the autoupgrading/downgrading issue, at least partly.
2144

AUTHORS

2146       ·   Mark Biggar, overloaded interface by Ilya Zakharevich, 1996-2001.
2147
2148       ·   Completely rewritten by Tels <http://bloodgate.com>, 2001-2008.
2149
2150       ·   Florian Ragwitz <flora@cpan.org>, 2010.
2151
2152       ·   Peter John Acklam <pjacklam@online.no>, 2011-.
2153
2154       Many people contributed in one or more ways to the final beast, see the
2155       file CREDITS for an (incomplete) list. If you miss your name, please
2156       drop me a mail. Thank you!
2157
2158
2159
2160perl v5.28.1                      2018-04-18                   Math::BigInt(3)
Impressum