Debian Codegraph @

upstream/opensuse-tumbleweed/man3/Math::BigInt.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "Math::BigInt 3pm"
61 .TH Math::BigInt 3pm 2026-03-09 "perl v5.42.1" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 Math::BigInt \- arbitrary size integer math package
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 1
71 \& use Math::BigInt;
72 \&
73 \& # or make it faster with huge numbers: install (optional)
74 \& # Math::BigInt::GMP and always use (it falls back to
75 \& # pure Perl if the GMP library is not installed):
76 \& # (See also the L</Math Library> section!)
77 \&
78 \& # to warn if Math::BigInt::GMP cannot be found, use
79 \& use Math::BigInt lib => \*(AqGMP\*(Aq;
80 \&
81 \& # to suppress the warning if Math::BigInt::GMP cannot be found, use
82 \& # use Math::BigInt try => \*(AqGMP\*(Aq;
83 \&
84 \& # to die if Math::BigInt::GMP cannot be found, use
85 \& # use Math::BigInt only => \*(AqGMP\*(Aq;
86 \&
87 \& # Configuration methods (may be used as class methods and instance methods)
88 \&
89 \& Math::BigInt\->accuracy($n); # set accuracy
90 \& Math::BigInt\->accuracy(); # get accuracy
91 \& Math::BigInt\->precision($n); # set precision
92 \& Math::BigInt\->precision(); # get precision
93 \& Math::BigInt\->round_mode($m); # set rounding mode, must be
94 \& # \*(Aqeven\*(Aq, \*(Aqodd\*(Aq, \*(Aq+inf\*(Aq, \*(Aq\-inf\*(Aq,
95 \& # \*(Aqzero\*(Aq, \*(Aqtrunc\*(Aq, or \*(Aqcommon\*(Aq
96 \& Math::BigInt\->round_mode(); # get class rounding mode
97 \& Math::BigInt\->div_scale($n); # set fallback accuracy
98 \& Math::BigInt\->div_scale(); # get fallback accuracy
99 \& Math::BigInt\->trap_inf($b); # trap infinities or not
100 \& Math::BigInt\->trap_inf(); # get trap infinities status
101 \& Math::BigInt\->trap_nan($b); # trap NaNs or not
102 \& Math::BigInt\->trap_nan(); # get trap NaNs status
103 \& Math::BigInt\->config($par, $val); # set configuration parameter
104 \& Math::BigInt\->config($par); # get configuration parameter
105 \& Math::BigInt\->config(); # get hash with configuration
106 \& Math::BigFloat\->config("lib"); # get name of backend library
107 \&
108 \& # Generic constructor method (always returns a new object)
109 \&
110 \& $x = Math::BigInt\->new($str); # defaults to 0
111 \& $x = Math::BigInt\->new(\*(Aq256\*(Aq); # from decimal
112 \& $x = Math::BigInt\->new(\*(Aq0256\*(Aq); # from decimal
113 \& $x = Math::BigInt\->new(\*(Aq0xcafe\*(Aq); # from hexadecimal
114 \& $x = Math::BigInt\->new(\*(Aq0x1.fap+7\*(Aq); # from hexadecimal
115 \& $x = Math::BigInt\->new(\*(Aq0o377\*(Aq); # from octal
116 \& $x = Math::BigInt\->new(\*(Aq0o1.35p+6\*(Aq); # from octal
117 \& $x = Math::BigInt\->new(\*(Aq0b101\*(Aq); # from binary
118 \& $x = Math::BigInt\->new(\*(Aq0b1.101p+3\*(Aq); # from binary
119 \&
120 \& # Specific constructor methods (no prefix needed; when used as
121 \& # instance method, the value is assigned to the invocand)
122 \&
123 \& $x = Math::BigInt\->from_dec(\*(Aq234\*(Aq); # from decimal
124 \& $x = Math::BigInt\->from_hex(\*(Aqcafe\*(Aq); # from hexadecimal
125 \& $x = Math::BigInt\->from_hex(\*(Aq1.fap+7\*(Aq); # from hexadecimal
126 \& $x = Math::BigInt\->from_oct(\*(Aq377\*(Aq); # from octal
127 \& $x = Math::BigInt\->from_oct(\*(Aq1.35p+6\*(Aq); # from octal
128 \& $x = Math::BigInt\->from_bin(\*(Aq1101\*(Aq); # from binary
129 \& $x = Math::BigInt\->from_bin(\*(Aq1.101p+3\*(Aq); # from binary
130 \& $x = Math::BigInt\->from_bytes($bytes); # from byte string
131 \& $x = Math::BigInt\->from_base(\*(Aqwhy\*(Aq, 36); # from any base
132 \& $x = Math::BigInt\->from_base_num([1, 0], 2); # from any base
133 \& $x = Math::BigInt\->from_ieee754($b, $fmt); # from IEEE\-754 bytes
134 \& $x = Math::BigInt\->bzero(); # create a +0
135 \& $x = Math::BigInt\->bone(); # create a +1
136 \& $x = Math::BigInt\->bone(\*(Aq\-\*(Aq); # create a \-1
137 \& $x = Math::BigInt\->binf(); # create a +inf
138 \& $x = Math::BigInt\->binf(\*(Aq\-\*(Aq); # create a \-inf
139 \& $x = Math::BigInt\->bnan(); # create a Not\-A\-Number
140 \& $x = Math::BigInt\->bpi(); # returns pi
141 \&
142 \& $y = $x\->copy(); # make a copy (unlike $y = $x)
143 \& $y = $x\->as_int(); # return as a Math::BigInt
144 \& $y = $x\->as_float(); # return as a Math::BigFloat
145 \& $y = $x\->as_rat(); # return as a Math::BigRat
146 \&
147 \& # Boolean methods (these don\*(Aqt modify the invocand)
148 \&
149 \& $x\->is_zero(); # true if $x is 0
150 \& $x\->is_one(); # true if $x is +1
151 \& $x\->is_one("+"); # true if $x is +1
152 \& $x\->is_one("\-"); # true if $x is \-1
153 \& $x\->is_inf(); # true if $x is +inf or \-inf
154 \& $x\->is_inf("+"); # true if $x is +inf
155 \& $x\->is_inf("\-"); # true if $x is \-inf
156 \& $x\->is_nan(); # true if $x is NaN
157 \&
158 \& $x\->is_finite(); # true if \-inf < $x < inf
159 \& $x\->is_positive(); # true if $x > 0
160 \& $x\->is_pos(); # true if $x > 0
161 \& $x\->is_negative(); # true if $x < 0
162 \& $x\->is_neg(); # true if $x < 0
163 \& $x\->is_non_positive() # true if $x <= 0
164 \& $x\->is_non_negative() # true if $x >= 0
165 \&
166 \& $x\->is_odd(); # true if $x is odd
167 \& $x\->is_even(); # true if $x is even
168 \& $x\->is_int(); # true if $x is an integer
169 \&
170 \& # Comparison methods (these don\*(Aqt modify the invocand)
171 \&
172 \& $x\->bcmp($y); # compare numbers (undef, < 0, == 0, > 0)
173 \& $x\->bacmp($y); # compare abs values (undef, < 0, == 0, > 0)
174 \& $x\->beq($y); # true if $x == $y
175 \& $x\->bne($y); # true if $x != $y
176 \& $x\->blt($y); # true if $x < $y
177 \& $x\->ble($y); # true if $x <= $y
178 \& $x\->bgt($y); # true if $x > $y
179 \& $x\->bge($y); # true if $x >= $y
180 \&
181 \& # Arithmetic methods (these modify the invocand)
182 \&
183 \& $x\->bneg(); # negation
184 \& $x\->babs(); # absolute value
185 \& $x\->bsgn(); # sign function (\-1, 0, 1, or NaN)
186 \& $x\->bdigitsum(); # sum of decimal digits
187 \& $x\->binc(); # increment $x by 1
188 \& $x\->bdec(); # decrement $x by 1
189 \& $x\->badd($y); # addition (add $y to $x)
190 \& $x\->bsub($y); # subtraction (subtract $y from $x)
191 \& $x\->bmul($y); # multiplication (multiply $x by $y)
192 \& $x\->bmuladd($y, $z); # $x = $x * $y + $z
193 \& $x\->bdiv($y); # division (floored)
194 \& $x\->bmod($y); # modulus (x % y)
195 \& $x\->bmodinv($mod); # modular multiplicative inverse
196 \& $x\->bmodpow($y, $mod); # modular exponentiation (($x ** $y) % $mod)
197 \& $x\->btdiv($y); # division (truncated), set $x to quotient
198 \& $x\->btmod($y); # modulus (truncated)
199 \& $x\->binv() # inverse (1/$x)
200 \& $x\->bpow($y); # power of arguments (x ** y)
201 \& $x\->blog(); # logarithm of $x to base e (Euler\*(Aqs number)
202 \& $x\->blog($base); # logarithm of $x to base $base (e.g., base 2)
203 \& $x\->bexp(); # calculate e ** $x where e is Euler\*(Aqs number
204 \& $x\->bilog2(); # log2($x) rounded down to nearest int
205 \& $x\->bilog10(); # log10($x) rounded down to nearest int
206 \& $x\->bclog2(); # log2($x) rounded up to nearest int
207 \& $x\->bclog10(); # log10($x) rounded up to nearest int
208 \& $x\->bnok($y); # combinations (binomial coefficient n over k)
209 \& $x\->bperm($y); # permutations
210 \& $x\->buparrow($n, $y); # Knuth\*(Aqs up\-arrow notation
211 \& $x\->bhyperop($n, $y); # n\*(Aqth hyperoprator
212 \& $x\->backermann($y); # the Ackermann function
213 \& $x\->bsin(); # sine
214 \& $x\->bcos(); # cosine
215 \& $x\->batan(); # inverse tangent
216 \& $x\->batan2($y); # two\-argument inverse tangent
217 \& $x\->bsqrt(); # calculate square root
218 \& $x\->broot($y); # $y\*(Aqth root of $x (e.g. $y == 3 => cubic root)
219 \& $x\->bfac(); # factorial of $x (1*2*3*4*..$x)
220 \& $x\->bdfac(); # double factorial of $x ($x*($x\-2)*($x\-4)*...)
221 \& $x\->btfac(); # triple factorial of $x ($x*($x\-3)*($x\-6)*...)
222 \& $x\->bmfac($k); # $k\*(Aqth multi\-factorial of $x ($x*($x\-$k)*...)
223 \& $x\->bfib($k); # $k\*(Aqth Fibonacci number
224 \& $x\->blucas($k); # $k\*(Aqth Lucas number
225 \&
226 \& $x\->blsft($n); # left shift $n places in base 2
227 \& $x\->blsft($n, $b); # left shift $n places in base $b
228 \& $x\->brsft($n); # right shift $n places in base 2
229 \& $x\->brsft($n, $b); # right shift $n places in base $b
230 \&
231 \& # Bitwise methods (these modify the invocand)
232 \&
233 \& $x\->bblsft($y); # bitwise left shift
234 \& $x\->bbrsft($y); # bitwise right shift
235 \& $x\->band($y); # bitwise and
236 \& $x\->bior($y); # bitwise inclusive or
237 \& $x\->bxor($y); # bitwise exclusive or
238 \& $x\->bnot(); # bitwise not (two\*(Aqs complement)
239 \&
240 \& # Rounding methods (these modify the invocand)
241 \&
242 \& $x\->round($A, $P, $R); # round to accuracy or precision using
243 \& # rounding mode $R
244 \& $x\->bround($n); # accuracy: preserve $n digits
245 \& $x\->bfround($n); # $n > 0: round to $nth digit left of dec. point
246 \& # $n < 0: round to $nth digit right of dec. point
247 \& $x\->bfloor(); # round towards minus infinity
248 \& $x\->bceil(); # round towards plus infinity
249 \& $x\->bint(); # round towards zero
250 \&
251 \& # Other mathematical methods (these don\*(Aqt modify the invocand)
252 \&
253 \& $x\->bgcd($y); # greatest common divisor
254 \& $x\->blcm($y); # least common multiple
255 \&
256 \& # Object property methods (these don\*(Aqt modify the invocand)
257 \&
258 \& $x\->sign(); # the sign, either +, \- or NaN
259 \& $x\->digit($n); # the nth digit, counting from the right
260 \& $x\->digit(\-$n); # the nth digit, counting from the left
261 \& $x\->digitsum(); # sum of decimal digits
262 \& $x\->length(); # return number of digits in number
263 \& $x\->mantissa(); # return (signed) mantissa as a Math::BigInt
264 \& $x\->exponent(); # return exponent as a Math::BigInt
265 \& $x\->parts(); # return (mantissa,exponent) as a Math::BigInt
266 \& $x\->sparts(); # mantissa and exponent (as integers)
267 \& $x\->nparts(); # mantissa and exponent (normalised)
268 \& $x\->eparts(); # mantissa and exponent (engineering notation)
269 \& $x\->dparts(); # integer and fraction part
270 \& $x\->fparts(); # numerator and denominator
271 \& $x\->numerator(); # numerator
272 \& $x\->denominator(); # denominator
273 \&
274 \& # Conversion methods (these don\*(Aqt modify the invocand)
275 \&
276 \& $x\->bstr(); # decimal notation (possibly zero padded)
277 \& $x\->bsstr(); # string in scientific notation with integers
278 \& $x\->bnstr(); # string in normalized notation
279 \& $x\->bestr(); # string in engineering notation
280 \& $x\->bdstr(); # string in decimal notation (no padding)
281 \& $x\->bfstr(); # string in fractional notation
282 \&
283 \& $x\->to_hex(); # as signed hexadecimal string
284 \& $x\->to_bin(); # as signed binary string
285 \& $x\->to_oct(); # as signed octal string
286 \& $x\->to_bytes(); # as byte string
287 \& $x\->to_base($b); # as string in any base
288 \& $x\->to_base_num($b); # as array of integers in any base
289 \& $x\->to_ieee754($fmt); # to bytes encoded according to IEEE 754\-2008
290 \&
291 \& $x\->as_hex(); # as signed hexadecimal string with "0x" prefix
292 \& $x\->as_bin(); # as signed binary string with "0b" prefix
293 \& $x\->as_oct(); # as signed octal string with "0" prefix
294 \&
295 \& # Other conversion methods (these don\*(Aqt modify the invocand)
296 \&
297 \& $x\->numify(); # return as scalar (might overflow or underflow)
298 .Ve
299 .SH DESCRIPTION
300 .IX Header "DESCRIPTION"
301 Math::BigInt provides support for arbitrary precision integers. Overloading is
302 also provided for Perl operators.
303 .SS Input
304 .IX Subsection "Input"
305 Input values to these routines may be any scalar number or string that looks
306 like a number and represents an integer. Anything that is accepted by Perl as a
307 literal numeric constant should be accepted by this module, except that finite
308 non\-integers return NaN.
309 .IP \(bu 4
310 Leading and trailing whitespace is ignored.
311 .IP \(bu 4
312 Leading zeros are ignored, except for floating point numbers with a binary
313 exponent, in which case the number is interpreted as an octal floating point
314 number. For example, "01.4p+0" gives 1.5, "00.4p+0" gives 0.5, but "0.4p+0"
315 gives a NaN. And while "0377" gives 255, "0377p0" gives 255.
316 .IP \(bu 4
317 If the string has a "0x" or "0X" prefix, it is interpreted as a hexadecimal
318 number.
319 .IP \(bu 4
320 If the string has a "0o" or "0O" prefix, it is interpreted as an octal number.
321 A floating point literal with a "0" prefix is also interpreted as an octal
322 number.
323 .IP \(bu 4
324 If the string has a "0b" or "0B" prefix, it is interpreted as a binary number.
325 .IP \(bu 4
326 Underline characters are allowed in the same way as they are allowed in literal
327 numerical constants.
328 .IP \(bu 4
329 If the string can not be interpreted, or does not represent a finite integer,
330 NaN is returned.
331 .IP \(bu 4
332 For hexadecimal, octal, and binary floating point numbers, the exponent must be
333 separated from the significand (mantissa) by the letter "p" or "P", not "e" or
334 "E" as with decimal numbers.
335 .PP
336 Some examples of valid string input
337 .PP
338 .Vb 1
339 \& Input string Resulting value
340 \&
341 \& 123 123
342 \& 1.23e2 123
343 \& 12300e\-2 123
344 \&
345 \& 67_538_754 67538754
346 \& \-4_5_6.7_8_9e+0_1_0 \-4567890000000
347 \&
348 \& 0x13a 314
349 \& 0x13ap0 314
350 \& 0x1.3ap+8 314
351 \& 0x0.00013ap+24 314
352 \& 0x13a000p\-12 314
353 \&
354 \& 0o472 314
355 \& 0o1.164p+8 314
356 \& 0o0.0001164p+20 314
357 \& 0o1164000p\-10 314
358 \&
359 \& 0472 472 Note!
360 \& 01.164p+8 314
361 \& 00.0001164p+20 314
362 \& 01164000p\-10 314
363 \&
364 \& 0b100111010 314
365 \& 0b1.0011101p+8 314
366 \& 0b0.00010011101p+12 314
367 \& 0b100111010000p\-3 314
368 .Ve
369 .PP
370 Input given as scalar numbers might lose precision. Quote your input to ensure
371 that no digits are lost:
372 .PP
373 .Vb 2
374 \& $x = Math::BigInt\->new( 56789012345678901234 ); # bad
375 \& $x = Math::BigInt\->new(\*(Aq56789012345678901234\*(Aq); # good
376 .Ve
377 .PP
378 Currently, \f(CW\*(C`Math::BigInt\-\*(C'\fR\fBnew()\fR> (no input argument) and
379 \&\f(CW\*(C`Math::BigInt\-\*(C'\fRnew("")> return 0. This might change in the future, so always
380 use the following explicit forms to get a zero:
381 .PP
382 .Vb 1
383 \& $zero = Math::BigInt\->bzero();
384 .Ve
385 .SS Output
386 .IX Subsection "Output"
387 Output values are usually Math::BigInt objects.
388 .PP
389 Boolean operators "\fBis_zero()\fR", "\fBis_one()\fR", "\fBis_inf()\fR", etc. return true
390 or false.
391 .PP
392 Comparison operators "\fBbcmp()\fR" and "\fBbacmp()\fR") return \-1, 0, 1, or undef.
393 .SH METHODS
394 .IX Header "METHODS"
395 .SS "Configuration methods"
396 .IX Subsection "Configuration methods"
397 Each of the methods below (except "\fBconfig()\fR", "\fBaccuracy()\fR" and
398 "\fBprecision()\fR") accepts three additional parameters. These arguments \f(CW$A\fR,
399 \&\f(CW$P\fR and \f(CW$R\fR are \f(CW\*(C`accuracy\*(C'\fR, \f(CW\*(C`precision\*(C'\fR and \f(CW\*(C`round_mode\*(C'\fR. Please see the
400 section about "ACCURACY AND PRECISION" for more information.
401 .PP
402 Setting a class variable effects all object instance that are created
403 afterwards.
404 .IP \fBaccuracy()\fR 4
405 .IX Item "accuracy()"
406 .Vb 2
407 \& Math::BigInt\->accuracy(5); # set class accuracy
408 \& $x\->accuracy(5); # set instance accuracy
409 \&
410 \& $A = Math::BigInt\->accuracy(); # get class accuracy
411 \& $A = $x\->accuracy(); # get instance accuracy
412 .Ve
413 .Sp
414 Set or get the accuracy, i.e., the number of significant digits. The accuracy
415 must be an integer. If the accuracy is set to \f(CW\*(C`undef\*(C'\fR, no rounding is done.
416 .Sp
417 Alternatively, one can round the results explicitly using one of "\fBround()\fR",
418 "\fBbround()\fR" or "\fBbfround()\fR" or by passing the desired accuracy to the method
419 as an additional parameter:
420 .Sp
421 .Vb 4
422 \& my $x = Math::BigInt\->new(30000);
423 \& my $y = Math::BigInt\->new(7);
424 \& print scalar $x\->copy()\->bdiv($y, 2); # prints 4300
425 \& print scalar $x\->copy()\->bdiv($y)\->bround(2); # prints 4300
426 .Ve
427 .Sp
428 Please see the section about "ACCURACY AND PRECISION" for further details.
429 .Sp
430 .Vb 4
431 \& $y = Math::BigInt\->new(1234567); # $y is not rounded
432 \& Math::BigInt\->accuracy(4); # set class accuracy to 4
433 \& $x = Math::BigInt\->new(1234567); # $x is rounded automatically
434 \& print "$x $y"; # prints "1235000 1234567"
435 \&
436 \& print $x\->accuracy(); # prints "4"
437 \& print $y\->accuracy(); # also prints "4", since
438 \& # class accuracy is 4
439 \&
440 \& Math::BigInt\->accuracy(5); # set class accuracy to 5
441 \& print $x\->accuracy(); # prints "4", since instance
442 \& # accuracy is 4
443 \& print $y\->accuracy(); # prints "5", since no instance
444 \& # accuracy, and class accuracy is 5
445 .Ve
446 .Sp
447 Note: Each class has it\*(Aqs own globals separated from Math::BigInt, but it is
448 possible to subclass Math::BigInt and make the globals of the subclass aliases
449 to the ones from Math::BigInt.
450 .IP \fBprecision()\fR 4
451 .IX Item "precision()"
452 .Vb 2
453 \& Math::BigInt\->precision(\-2); # set class precision
454 \& $x\->precision(\-2); # set instance precision
455 \&
456 \& $P = Math::BigInt\->precision(); # get class precision
457 \& $P = $x\->precision(); # get instance precision
458 .Ve
459 .Sp
460 Set or get the precision, i.e., the place to round relative to the decimal
461 point. The precision must be a integer. Setting the precision to \f(CW$P\fR means that
462 each number is rounded up or down, depending on the rounding mode, to the
463 nearest multiple of 10**$P. If the precision is set to \f(CW\*(C`undef\*(C'\fR, no rounding is
464 done.
465 .Sp
466 You might want to use "\fBaccuracy()\fR" instead. With "\fBaccuracy()\fR" you set the
467 number of digits each result should have, with "\fBprecision()\fR" you set the
468 place where to round.
469 .Sp
470 Please see the section about "ACCURACY AND PRECISION" for further details.
471 .Sp
472 .Vb 4
473 \& $y = Math::BigInt\->new(1234567); # $y is not rounded
474 \& Math::BigInt\->precision(4); # set class precision to 4
475 \& $x = Math::BigInt\->new(1234567); # $x is rounded automatically
476 \& print $x; # prints "1230000"
477 .Ve
478 .Sp
479 Note: Each class has its own globals separated from Math::BigInt, but it is
480 possible to subclass Math::BigInt and make the globals of the subclass aliases
481 to the ones from Math::BigInt.
482 .IP \fBround_mode()\fR 4
483 .IX Item "round_mode()"
484 Set/get the rounding mode.
485 .IP \fBdiv_scale()\fR 4
486 .IX Item "div_scale()"
487 Set/get the fallback accuracy. This is the accuracy used when neither accuracy
488 nor precision is set explicitly. It is used when a computation might otherwise
489 attempt to return an infinite number of digits.
490 .IP \fBtrap_inf()\fR 4
491 .IX Item "trap_inf()"
492 Set/get the value determining whether infinities should cause a fatal error or
493 not.
494 .IP \fBtrap_nan()\fR 4
495 .IX Item "trap_nan()"
496 Set/get the value determining whether NaNs should cause a fatal error or not.
497 .IP \fBupgrade()\fR 4
498 .IX Item "upgrade()"
499 Set/get the class for upgrading. When a computation might result in a
500 non\-integer, the operands are upgraded to this class. This is used for instance
501 by bignum. The default is \f(CW\*(C`undef\*(C'\fR, i.e., no upgrading.
502 .Sp
503 .Vb 4
504 \& # with no upgrading
505 \& $x = Math::BigInt\->new(12);
506 \& $y = Math::BigInt\->new(5);
507 \& print $x / $y, "\en"; # 2 as a Math::BigInt
508 \&
509 \& # with upgrading to Math::BigFloat
510 \& Math::BigInt \-> upgrade("Math::BigFloat");
511 \& print $x / $y, "\en"; # 2.4 as a Math::BigFloat
512 \&
513 \& # with upgrading to Math::BigRat (after loading Math::BigRat)
514 \& Math::BigInt \-> upgrade("Math::BigRat");
515 \& print $x / $y, "\en"; # 12/5 as a Math::BigRat
516 .Ve
517 .IP \fBdowngrade()\fR 4
518 .IX Item "downgrade()"
519 Set/get the class for downgrading. The default is \f(CW\*(C`undef\*(C'\fR, i.e., no
520 downgrading. Downgrading is not done by Math::BigInt.
521 .IP \fBmodify()\fR 4
522 .IX Item "modify()"
523 .Vb 1
524 \& $x\->modify(\*(Aqbpowd\*(Aq);
525 .Ve
526 .Sp
527 This method returns 0 if the object can be modified with the given operation,
528 or 1 if not.
529 .Sp
530 This is used for instance by Math::BigInt::Constant.
531 .IP \fBconfig()\fR 4
532 .IX Item "config()"
533 .Vb 2
534 \& Math::BigInt\->config("trap_nan" => 1); # set
535 \& $accu = Math::BigInt\->config("accuracy"); # get
536 .Ve
537 .Sp
538 Set or get class variables. Read\-only parameters are marked as RO. Read\-write
539 parameters are marked as RW. The following parameters are supported.
540 .Sp
541 .Vb 10
542 \& Parameter RO/RW Description
543 \& Example
544 \& ============================================================
545 \& lib RO Name of the math backend library
546 \& Math::BigInt::Calc
547 \& lib_version RO Version of the math backend library
548 \& 0.30
549 \& class RO The class of config you just called
550 \& Math::BigRat
551 \& version RO version number of the class you used
552 \& 0.10
553 \& upgrade RW To which class numbers are upgraded
554 \& undef
555 \& downgrade RW To which class numbers are downgraded
556 \& undef
557 \& precision RW Global precision
558 \& undef
559 \& accuracy RW Global accuracy
560 \& undef
561 \& round_mode RW Global round mode
562 \& even
563 \& div_scale RW Fallback accuracy for division etc.
564 \& 40
565 \& trap_nan RW Trap NaNs
566 \& undef
567 \& trap_inf RW Trap +inf/\-inf
568 \& undef
569 .Ve
570 .SS "Constructor methods"
571 .IX Subsection "Constructor methods"
572 .IP \fBnew()\fR 4
573 .IX Item "new()"
574 .Vb 1
575 \& $x = Math::BigInt\->new($str,$A,$P,$R);
576 .Ve
577 .Sp
578 Creates a new Math::BigInt object from a scalar or another Math::BigInt object.
579 The input is accepted as decimal, hexadecimal (with leading \*(Aq0x\*(Aq), octal (with
580 leading (\*(Aq0o\*(Aq) or binary (with leading \*(Aq0b\*(Aq).
581 .Sp
582 See "Input" for more info on accepted input formats.
583 .IP \fBfrom_dec()\fR 4
584 .IX Item "from_dec()"
585 .Vb 1
586 \& $x = Math::BigInt\->from_dec("314159"); # input is decimal
587 .Ve
588 .Sp
589 Interpret input as a decimal. It is equivalent to "\fBnew()\fR", but does not
590 accept anything but strings representing finite, decimal numbers.
591 .IP \fBfrom_hex()\fR 4
592 .IX Item "from_hex()"
593 .Vb 1
594 \& $x = Math::BigInt\->from_hex("0xcafe"); # input is hexadecimal
595 .Ve
596 .Sp
597 Interpret input as a hexadecimal string. A "0x" or "x" prefix is optional. A
598 single underscore character may be placed right after the prefix, if present,
599 or between any two digits. If the input is invalid, a NaN is returned.
600 .IP \fBfrom_oct()\fR 4
601 .IX Item "from_oct()"
602 .Vb 1
603 \& $x = Math::BigInt\->from_oct("0775"); # input is octal
604 .Ve
605 .Sp
606 Interpret the input as an octal string and return the corresponding value. A
607 "0" (zero) prefix is optional. A single underscore character may be placed
608 right after the prefix, if present, or between any two digits. If the input is
609 invalid, a NaN is returned.
610 .IP \fBfrom_bin()\fR 4
611 .IX Item "from_bin()"
612 .Vb 1
613 \& $x = Math::BigInt\->from_bin("0b10011"); # input is binary
614 .Ve
615 .Sp
616 Interpret the input as a binary string. A "0b" or "b" prefix is optional. A
617 single underscore character may be placed right after the prefix, if present,
618 or between any two digits. If the input is invalid, a NaN is returned.
619 .IP \fBfrom_bytes()\fR 4
620 .IX Item "from_bytes()"
621 .Vb 1
622 \& $x = Math::BigInt\->from_bytes("\exf3\ex6b"); # $x = 62315
623 .Ve
624 .Sp
625 Interpret the input as a byte string, assuming big endian byte order. The
626 output is always a non\-negative, finite integer.
627 .Sp
628 In some special cases, "\fBfrom_bytes()\fR" matches the conversion done by
629 \&\fBunpack()\fR:
630 .Sp
631 .Vb 3
632 \& $b = "\ex4e"; # one char byte string
633 \& $x = Math::BigInt\->from_bytes($b); # = 78
634 \& $y = unpack "C", $b; # ditto, but scalar
635 \&
636 \& $b = "\exf3\ex6b"; # two char byte string
637 \& $x = Math::BigInt\->from_bytes($b); # = 62315
638 \& $y = unpack "S>", $b; # ditto, but scalar
639 \&
640 \& $b = "\ex2d\exe0\ex49\exad"; # four char byte string
641 \& $x = Math::BigInt\->from_bytes($b); # = 769673645
642 \& $y = unpack "L>", $b; # ditto, but scalar
643 \&
644 \& $b = "\ex2d\exe0\ex49\exad\ex2d\exe0\ex49\exad"; # eight char byte string
645 \& $x = Math::BigInt\->from_bytes($b); # = 3305723134637787565
646 \& $y = unpack "Q>", $b; # ditto, but scalar
647 .Ve
648 .IP \fBfrom_ieee754()\fR 4
649 .IX Item "from_ieee754()"
650 .Vb 2
651 \& # set $x to 314159
652 \& $x = Math::BigInt \-> from_ieee754("40490fdb", "binary32");
653 .Ve
654 .Sp
655 Interpret the input as a value encoded as described in IEEE754\-2008. NaN is
656 returned if the value is neither +/\-infinity nor an integer.
657 .Sp
658 See "\fBfrom_ieee754()\fR" in Math::BigFloat.
659 .IP \fBfrom_base()\fR 4
660 .IX Item "from_base()"
661 Given a string, a base, and an optional collation sequence, interpret the
662 string as a number in the given base. The collation sequence describes the
663 value of each character in the string.
664 .Sp
665 If a collation sequence is not given, a default collation sequence is used. If
666 the base is less than or equal to 36, the collation sequence is the string
667 consisting of the 36 characters "0" to "9" and "A" to "Z". In this case, the
668 letter case in the input is ignored. If the base is greater than 36, and
669 smaller than or equal to 62, the collation sequence is the string consisting of
670 the 62 characters "0" to "9", "A" to "Z", and "a" to "z". A base larger than 62
671 requires the collation sequence to be specified explicitly.
672 .Sp
673 These examples show standard binary, octal, and hexadecimal conversion. All
674 cases return 250.
675 .Sp
676 .Vb 3
677 \& $x = Math::BigInt\->from_base("11111010", 2);
678 \& $x = Math::BigInt\->from_base("372", 8);
679 \& $x = Math::BigInt\->from_base("fa", 16);
680 .Ve
681 .Sp
682 When the base is less than or equal to 36, and no collation sequence is given,
683 the letter case is ignored, so both of these also return 250:
684 .Sp
685 .Vb 2
686 \& $x = Math::BigInt\->from_base("6Y", 16);
687 \& $x = Math::BigInt\->from_base("6y", 16);
688 .Ve
689 .Sp
690 When the base greater than 36, and no collation sequence is given, the default
691 collation sequence contains both uppercase and lowercase letters, so
692 the letter case in the input is not ignored:
693 .Sp
694 .Vb 5
695 \& $x = Math::BigInt\->from_base("6S", 37); # $x is 250
696 \& $x = Math::BigInt\->from_base("6s", 37); # $x is 276
697 \& $x = Math::BigInt\->from_base("121", 3); # $x is 16
698 \& $x = Math::BigInt\->from_base("XYZ", 36); # $x is 44027
699 \& $x = Math::BigInt\->from_base("Why", 42); # $x is 58314
700 .Ve
701 .Sp
702 The collation sequence can be any set of unique characters. These two cases
703 are equivalent
704 .Sp
705 .Vb 2
706 \& $x = Math::BigInt\->from_base("100", 2, "01"); # $x is 4
707 \& $x = Math::BigInt\->from_base("|\-\-", 2, "\-|"); # $x is 4
708 .Ve
709 .IP \fBfrom_base_num()\fR 4
710 .IX Item "from_base_num()"
711 Returns a new Math::BigInt object given an array of values and a base. This
712 method is equivalent to "\fBfrom_base()\fR", but works on numbers in an array
713 rather than characters in a string. Unlike "\fBfrom_base()\fR", all input values
714 may be arbitrarily large.
715 .Sp
716 .Vb 2
717 \& $x = Math::BigInt\->from_base_num([1, 1, 0, 1], 2) # $x is 13
718 \& $x = Math::BigInt\->from_base_num([3, 125, 39], 128) # $x is 65191
719 .Ve
720 .IP \fBbzero()\fR 4
721 .IX Item "bzero()"
722 .Vb 2
723 \& $x = Math::BigInt\->bzero();
724 \& $x\->bzero();
725 .Ve
726 .Sp
727 Returns a new Math::BigInt object representing zero. If used as an instance
728 method, assigns the value to the invocand.
729 .IP \fBbone()\fR 4
730 .IX Item "bone()"
731 .Vb 6
732 \& $x = Math::BigInt\->bone(); # +1
733 \& $x = Math::BigInt\->bone("+"); # +1
734 \& $x = Math::BigInt\->bone("\-"); # \-1
735 \& $x\->bone(); # +1
736 \& $x\->bone("+"); # +1
737 \& $x\->bone(\*(Aq\-\*(Aq); # \-1
738 .Ve
739 .Sp
740 Creates a new Math::BigInt object representing one. The optional argument is
741 either \*(Aq\-\*(Aq or \*(Aq+\*(Aq, indicating whether you want plus one or minus one. If used
742 as an instance method, assigns the value to the invocand.
743 .IP \fBbinf()\fR 4
744 .IX Item "binf()"
745 .Vb 1
746 \& $x = Math::BigInt\->binf($sign);
747 .Ve
748 .Sp
749 Creates a new Math::BigInt object representing infinity. The optional argument
750 is either \*(Aq\-\*(Aq or \*(Aq+\*(Aq, indicating whether you want infinity or minus infinity.
751 If used as an instance method, assigns the value to the invocand.
752 .Sp
753 .Vb 2
754 \& $x\->binf();
755 \& $x\->binf(\*(Aq\-\*(Aq);
756 .Ve
757 .IP \fBbnan()\fR 4
758 .IX Item "bnan()"
759 .Vb 1
760 \& $x = Math::BigInt\->bnan();
761 .Ve
762 .Sp
763 Creates a new Math::BigInt object representing NaN (Not A Number). If used as
764 an instance method, assigns the value to the invocand.
765 .Sp
766 .Vb 1
767 \& $x\->bnan();
768 .Ve
769 .IP \fBbpi()\fR 4
770 .IX Item "bpi()"
771 .Vb 2
772 \& $x = Math::BigInt\->bpi(100); # 3
773 \& $x\->bpi(100); # 3
774 .Ve
775 .Sp
776 Creates a new Math::BigInt object representing PI. If used as an instance
777 method, assigns the value to the invocand. With Math::BigInt this always
778 returns 3.
779 .Sp
780 If upgrading is in effect, returns PI, rounded to N digits with the current
781 rounding mode:
782 .Sp
783 .Vb 4
784 \& use Math::BigFloat;
785 \& use Math::BigInt upgrade => "Math::BigFloat";
786 \& print Math::BigInt\->bpi(3), "\en"; # 3.14
787 \& print Math::BigInt\->bpi(100), "\en"; # 3.1415....
788 .Ve
789 .IP \fBcopy()\fR 4
790 .IX Item "copy()"
791 .Vb 1
792 \& $x\->copy(); # make a true copy of $x (unlike $y = $x)
793 .Ve
794 .IP \fBas_int()\fR 4
795 .IX Item "as_int()"
796 .Vb 1
797 \& $y = $x \-> as_int(); # $y is a Math::BigInt
798 .Ve
799 .Sp
800 Returns \f(CW$x\fR as a Math::BigInt object regardless of upgrading and downgrading. If
801 \&\f(CW$x\fR is finite, but not an integer, \f(CW$x\fR is truncated.
802 .IP \fBas_rat()\fR 4
803 .IX Item "as_rat()"
804 .Vb 1
805 \& $y = $x \-> as_rat(); # $y is a Math::BigRat
806 .Ve
807 .Sp
808 Returns \f(CW$x\fR a Math::BigRat object regardless of upgrading and downgrading. The
809 invocand is not modified.
810 .IP \fBas_float()\fR 4
811 .IX Item "as_float()"
812 .Vb 1
813 \& $y = $x \-> as_float(); # $y is a Math::BigFloat
814 .Ve
815 .Sp
816 Returns \f(CW$x\fR a Math::BigFloat object regardless of upgrading and downgrading. The
817 invocand is not modified.
818 .SS "Boolean methods"
819 .IX Subsection "Boolean methods"
820 None of these methods modify the invocand object.
821 .IP \fBis_zero()\fR 4
822 .IX Item "is_zero()"
823 .Vb 1
824 \& $x\->is_zero(); # true if $x is 0
825 .Ve
826 .Sp
827 Returns true if the invocand is zero and false otherwise.
828 .IP \fBis_one()\fR 4
829 .IX Item "is_one()"
830 .Vb 3
831 \& $x\->is_one(); # true if $x is +1
832 \& $x\->is_one("+"); # ditto
833 \& $x\->is_one("\-"); # true if $x is \-1
834 .Ve
835 .Sp
836 Returns true if the invocand is one and false otherwise.
837 .IP \fBis_finite()\fR 4
838 .IX Item "is_finite()"
839 .Vb 1
840 \& $x\->is_finite(); # true if $x is not +inf, \-inf or NaN
841 .Ve
842 .Sp
843 Returns true if the invocand is a finite number, i.e., it is neither +inf,
844 \&\-inf, nor NaN.
845 .IP \fBis_inf()\fR 4
846 .IX Item "is_inf()"
847 .Vb 3
848 \& $x\->is_inf(); # true if $x is +inf or \-inf
849 \& $x\->is_inf("+"); # true if $x is +inf
850 \& $x\->is_inf("\-"); # true if $x is \-inf
851 .Ve
852 .Sp
853 Returns true if the invocand is infinite and false otherwise.
854 .IP \fBis_nan()\fR 4
855 .IX Item "is_nan()"
856 .Vb 1
857 \& $x\->is_nan(); # true if $x is NaN
858 .Ve
859 .IP \fBis_positive()\fR 4
860 .IX Item "is_positive()"
861 .PD 0
862 .IP \fBis_pos()\fR 4
863 .IX Item "is_pos()"
864 .PD
865 .Vb 2
866 \& $x\->is_positive(); # true if > 0
867 \& $x\->is_pos(); # ditto
868 .Ve
869 .Sp
870 Returns true if the invocand is positive and false otherwise. A \f(CW\*(C`NaN\*(C'\fR is
871 neither positive nor negative.
872 .IP \fBis_negative()\fR 4
873 .IX Item "is_negative()"
874 .PD 0
875 .IP \fBis_neg()\fR 4
876 .IX Item "is_neg()"
877 .PD
878 .Vb 2
879 \& $x\->is_negative(); # true if < 0
880 \& $x\->is_neg(); # ditto
881 .Ve
882 .Sp
883 Returns true if the invocand is negative and false otherwise. A \f(CW\*(C`NaN\*(C'\fR is
884 neither positive nor negative.
885 .IP \fBis_non_positive()\fR 4
886 .IX Item "is_non_positive()"
887 .Vb 1
888 \& $x\->is_non_positive(); # true if <= 0
889 .Ve
890 .Sp
891 Returns true if the invocand is negative or zero.
892 .IP \fBis_non_negative()\fR 4
893 .IX Item "is_non_negative()"
894 .Vb 1
895 \& $x\->is_non_negative(); # true if >= 0
896 .Ve
897 .Sp
898 Returns true if the invocand is positive or zero.
899 .IP \fBis_odd()\fR 4
900 .IX Item "is_odd()"
901 .Vb 1
902 \& $x\->is_odd(); # true if odd, false for even
903 .Ve
904 .Sp
905 Returns true if the invocand is odd and false otherwise. \f(CW\*(C`NaN\*(C'\fR, \f(CW\*(C`+inf\*(C'\fR, and
906 \&\f(CW\*(C`\-inf\*(C'\fR are neither odd nor even.
907 .IP \fBis_even()\fR 4
908 .IX Item "is_even()"
909 .Vb 1
910 \& $x\->is_even(); # true if $x is even
911 .Ve
912 .Sp
913 Returns true if the invocand is even and false otherwise. \f(CW\*(C`NaN\*(C'\fR, \f(CW\*(C`+inf\*(C'\fR,
914 \&\f(CW\*(C`\-inf\*(C'\fR are not integers and are neither odd nor even.
915 .IP \fBis_int()\fR 4
916 .IX Item "is_int()"
917 .Vb 1
918 \& $x\->is_int(); # true if $x is an integer
919 .Ve
920 .Sp
921 Returns true if the invocand is an integer and false otherwise. \f(CW\*(C`NaN\*(C'\fR,
922 \&\f(CW\*(C`+inf\*(C'\fR, \f(CW\*(C`\-inf\*(C'\fR are not integers.
923 .SS "Comparison methods"
924 .IX Subsection "Comparison methods"
925 None of these methods modify the invocand object. Note that a \f(CW\*(C`NaN\*(C'\fR is neither
926 less than, greater than, or equal to anything else, even a \f(CW\*(C`NaN\*(C'\fR.
927 .IP \fBbcmp()\fR 4
928 .IX Item "bcmp()"
929 .Vb 1
930 \& $x\->bcmp($y);
931 .Ve
932 .Sp
933 Returns \-1, 0, 1 depending on whether \f(CW$x\fR is less than, equal to, or grater than
934 \&\f(CW$y\fR. Returns undef if any operand is a NaN.
935 .IP \fBbacmp()\fR 4
936 .IX Item "bacmp()"
937 .Vb 1
938 \& $x\->bacmp($y);
939 .Ve
940 .Sp
941 Returns \-1, 0, 1 depending on whether the absolute value of \f(CW$x\fR is less than,
942 equal to, or grater than the absolute value of \f(CW$y\fR. Returns undef if any operand
943 is a NaN.
944 .IP \fBbeq()\fR 4
945 .IX Item "beq()"
946 .Vb 1
947 \& $x \-> beq($y);
948 .Ve
949 .Sp
950 Returns true if and only if \f(CW$x\fR is equal to \f(CW$y\fR, and false otherwise.
951 .IP \fBbne()\fR 4
952 .IX Item "bne()"
953 .Vb 1
954 \& $x \-> bne($y);
955 .Ve
956 .Sp
957 Returns true if and only if \f(CW$x\fR is not equal to \f(CW$y\fR, and false otherwise.
958 .IP \fBblt()\fR 4
959 .IX Item "blt()"
960 .Vb 1
961 \& $x \-> blt($y);
962 .Ve
963 .Sp
964 Returns true if and only if \f(CW$x\fR is equal to \f(CW$y\fR, and false otherwise.
965 .IP \fBble()\fR 4
966 .IX Item "ble()"
967 .Vb 1
968 \& $x \-> ble($y);
969 .Ve
970 .Sp
971 Returns true if and only if \f(CW$x\fR is less than or equal to \f(CW$y\fR, and false
972 otherwise.
973 .IP \fBbgt()\fR 4
974 .IX Item "bgt()"
975 .Vb 1
976 \& $x \-> bgt($y);
977 .Ve
978 .Sp
979 Returns true if and only if \f(CW$x\fR is greater than \f(CW$y\fR, and false otherwise.
980 .IP \fBbge()\fR 4
981 .IX Item "bge()"
982 .Vb 1
983 \& $x \-> bge($y);
984 .Ve
985 .Sp
986 Returns true if and only if \f(CW$x\fR is greater than or equal to \f(CW$y\fR, and false
987 otherwise.
988 .SS "Arithmetic methods"
989 .IX Subsection "Arithmetic methods"
990 These methods modify the invocand object and returns it.
991 .IP \fBbneg()\fR 4
992 .IX Item "bneg()"
993 .Vb 1
994 \& $x\->bneg();
995 .Ve
996 .Sp
997 Negate the number, e.g. change the sign between \*(Aq+\*(Aq and \*(Aq\-\*(Aq, or between \*(Aq+inf\*(Aq
998 and \*(Aq\-inf\*(Aq, respectively. Does nothing for NaN or zero.
999 .IP \fBbabs()\fR 4
1000 .IX Item "babs()"
1001 .Vb 1
1002 \& $x\->babs();
1003 .Ve
1004 .Sp
1005 Set the number to its absolute value, e.g. change the sign from \*(Aq\-\*(Aq to \*(Aq+\*(Aq
1006 and from \*(Aq\-inf\*(Aq to \*(Aq+inf\*(Aq, respectively. Does nothing for NaN or positive
1007 numbers.
1008 .IP \fBbsgn()\fR 4
1009 .IX Item "bsgn()"
1010 .Vb 1
1011 \& $x\->bsgn();
1012 .Ve
1013 .Sp
1014 Signum function. Set the number to \-1, 0, or 1, depending on whether the
1015 number is negative, zero, or positive, respectively. Does not modify NaNs.
1016 .IP \fBbnorm()\fR 4
1017 .IX Item "bnorm()"
1018 .Vb 1
1019 \& $x\->bnorm(); # normalize (no\-op)
1020 .Ve
1021 .Sp
1022 Normalize the number. This is a no\-op and is provided only for backwards
1023 compatibility.
1024 .IP \fBbinc()\fR 4
1025 .IX Item "binc()"
1026 .Vb 1
1027 \& $x\->binc(); # increment x by 1
1028 .Ve
1029 .IP \fBbdec()\fR 4
1030 .IX Item "bdec()"
1031 .Vb 1
1032 \& $x\->bdec(); # decrement x by 1
1033 .Ve
1034 .IP \fBbadd()\fR 4
1035 .IX Item "badd()"
1036 .Vb 1
1037 \& $x\->badd($y); # addition (add $y to $x)
1038 .Ve
1039 .IP \fBbsub()\fR 4
1040 .IX Item "bsub()"
1041 .Vb 1
1042 \& $x\->bsub($y); # subtraction (subtract $y from $x)
1043 .Ve
1044 .IP \fBbmul()\fR 4
1045 .IX Item "bmul()"
1046 .Vb 1
1047 \& $x\->bmul($y); # multiplication (multiply $x by $y)
1048 .Ve
1049 .IP \fBbdiv()\fR 4
1050 .IX Item "bdiv()"
1051 .Vb 2
1052 \& $x\->bdiv($y); # set $x to quotient
1053 \& ($q, $r) = $x\->bdiv($y); # also return remainder
1054 .Ve
1055 .Sp
1056 The behaviour of "\fBbdiv()\fR" and "\fBbmod()\fR" is based on Perl\*(Aqs \f(CW\*(C`%\*(C'\fR operator,
1057 which is the remainder after performing floored division.
1058 .Sp
1059 Because of this, "\fBbdiv()\fR" and "\fBbmod()\fR" are aliases for "\fBbfdiv()\fR" and
1060 "\fBbfmod()\fR", respectively.
1061 .IP \fBbmod()\fR 4
1062 .IX Item "bmod()"
1063 .Vb 1
1064 \& $x\->bmod($y); # modulus (x % y)
1065 .Ve
1066 .Sp
1067 This is an alias for "\fBbfmod()\fR".
1068 .IP \fBbfdiv()\fR 4
1069 .IX Item "bfdiv()"
1070 .Vb 2
1071 \& $x\->bfdiv($y); # return quotient
1072 \& ($q, $r) = $x\->bfdiv($y); # return quotient and remainder
1073 .Ve
1074 .Sp
1075 Divides \f(CW$x\fR by \f(CW$y\fR by doing floored division (F\-division), where the quotient is
1076 the floored (rounded towards negative infinity) quotient of the two operands.
1077 In list context, returns the quotient and the remainder. In scalar context,
1078 only the quotient is returned.
1079 .Sp
1080 .Vb 2
1081 \& $q = floor($x / $y) # quotient
1082 \& $r = $x \- $q * $y # remainder
1083 .Ve
1084 .Sp
1085 With F\-division, the remainder is either zero or has the same sign as the
1086 divisor.
1087 .Sp
1088 .Vb 4
1089 \& 7 / 4 => ( 1, 3)
1090 \& \-7 / 4 => (\-2, 1)
1091 \& \-7 / \-4 => ( 1, \-3)
1092 \& 7 / \-4 => (\-2, \-1)
1093 .Ve
1094 .Sp
1095 The behavior of the overloaded operator % agrees with the behavior of Perl\*(Aqs
1096 built\-in % operator (as documented in the perlop manpage), and the equation
1097 .Sp
1098 .Vb 1
1099 \& $x == ($x / $y) * $y + ($x % $y)
1100 .Ve
1101 .Sp
1102 holds true for any finite \f(CW$x\fR and finite, non\-zero \f(CW$y\fR.
1103 .Sp
1104 Perl\*(Aqs "use integer" might change the behaviour of % and / for scalars. This is
1105 because under \*(Aquse integer\*(Aq Perl does what the underlying C library thinks is
1106 right, and this varies. However, "use integer" does not change the way things
1107 are done with Math::BigInt objects.
1108 .IP \fBbfmod()\fR 4
1109 .IX Item "bfmod()"
1110 .Vb 1
1111 \& $x\->bfmod($y); # floored modulus (x % y)
1112 .Ve
1113 .Sp
1114 Returns \f(CW$x\fR modulo \f(CW$y\fR, i.e., the remainder after floored division (F\-division).
1115 This method is like Perl\*(Aqs % operator. See "\fBbfdiv()\fR".
1116 .IP \fBbtdiv()\fR 4
1117 .IX Item "btdiv()"
1118 .Vb 1
1119 \& $x\->btdiv($y); # divide, set $x to quotient
1120 .Ve
1121 .Sp
1122 Divides \f(CW$x\fR by \f(CW$y\fR by doing truncated division (T\-division), where quotient is
1123 the truncated (rouneded towards zero) quotient of the two operands. In list
1124 context, returns the quotient and the remainder. The remainder is either zero
1125 or has the same sign as the first operand. In scalar context, only the quotient
1126 is returned.
1127 .IP \fBbtmod()\fR 4
1128 .IX Item "btmod()"
1129 .Vb 1
1130 \& $x\->btmod($y); # modulus
1131 .Ve
1132 .Sp
1133 Returns the remainer after truncated division (T\-division). See "\fBbtdiv()\fR".
1134 .IP \fBbinv()\fR 4
1135 .IX Item "binv()"
1136 .Vb 1
1137 \& $x\->binv();
1138 .Ve
1139 .Sp
1140 Invert the value of \f(CW$x\fR, i.e., compute 1/$x.
1141 .IP \fBbsqrt()\fR 4
1142 .IX Item "bsqrt()"
1143 .Vb 1
1144 \& $x\->bsqrt(); # calculate square root
1145 .Ve
1146 .Sp
1147 Returns the square root truncated to an integer.
1148 .Sp
1149 If you want a better approximation of the square root, then use:
1150 .Sp
1151 .Vb 4
1152 \& $x = Math::BigFloat\->new(12);
1153 \& Math::BigFloat\->precision(0);
1154 \& Math::BigFloat\->round_mode("even");
1155 \& print $x\->copy\->bsqrt(),"\en"; # 4
1156 \&
1157 \& Math::BigFloat\->precision(2);
1158 \& print $x\->bsqrt(),"\en"; # 3.46
1159 \& print $x\->bsqrt(3),"\en"; # 3.464
1160 .Ve
1161 .IP \fBbpow()\fR 4
1162 .IX Item "bpow()"
1163 .Vb 1
1164 \& $x\->bpow($y); # power of arguments (x ** y)
1165 .Ve
1166 .Sp
1167 Returns \f(CW$x\fR raised to the power of \f(CW$y\fR. The first two modifies \f(CW$x\fR, the last one
1168 doesn\*(Aqt:
1169 .Sp
1170 .Vb 3
1171 \& print $x\->bpow($i),"\en"; # modifies $x
1172 \& print $x **= $i,"\en"; # ditto
1173 \& print $x ** $i,"\en"; # leaves $x alone
1174 .Ve
1175 .Sp
1176 The form \f(CW\*(C`$x **= $y\*(C'\fR is faster than \f(CW\*(C`$x = $x ** $y;\*(C'\fR, though.
1177 .IP \fBbroot()\fR 4
1178 .IX Item "broot()"
1179 .Vb 1
1180 \& $x\->broot($N);
1181 .Ve
1182 .Sp
1183 Calculates the \f(CW$N\fR\*(Aqth root of \f(CW$x\fR.
1184 .IP \fBbmuladd()\fR 4
1185 .IX Item "bmuladd()"
1186 .Vb 1
1187 \& $x\->bmuladd($y,$z);
1188 .Ve
1189 .Sp
1190 Multiply \f(CW$x\fR by \f(CW$y\fR, and then add \f(CW$z\fR to the result,
1191 .Sp
1192 This method was added in v1.88 of Math::BigInt.
1193 .IP \fBbmodpow()\fR 4
1194 .IX Item "bmodpow()"
1195 .Vb 2
1196 \& $num\->bmodpow($exp,$mod); # modular exponentiation
1197 \& # ($num**$exp % $mod)
1198 .Ve
1199 .Sp
1200 Returns the value of \f(CW$num\fR taken to the power \f(CW$exp\fR in the modulus
1201 \&\f(CW$mod\fR using binary exponentiation. \f(CW\*(C`bmodpow\*(C'\fR is far superior to
1202 writing
1203 .Sp
1204 .Vb 1
1205 \& $num ** $exp % $mod
1206 .Ve
1207 .Sp
1208 because it is much faster \- it reduces internal variables into
1209 the modulus whenever possible, so it operates on smaller numbers.
1210 .Sp
1211 \&\f(CW\*(C`bmodpow\*(C'\fR also supports negative exponents.
1212 .Sp
1213 .Vb 1
1214 \& bmodpow($num, \-1, $mod)
1215 .Ve
1216 .Sp
1217 is exactly equivalent to
1218 .Sp
1219 .Vb 1
1220 \& bmodinv($num, $mod)
1221 .Ve
1222 .IP \fBbmodinv()\fR 4
1223 .IX Item "bmodinv()"
1224 .Vb 1
1225 \& $x\->bmodinv($mod); # modular multiplicative inverse
1226 .Ve
1227 .Sp
1228 Returns the multiplicative inverse of \f(CW$x\fR modulo \f(CW$mod\fR. If
1229 .Sp
1230 .Vb 1
1231 \& $y = $x \-> copy() \-> bmodinv($mod)
1232 .Ve
1233 .Sp
1234 then \f(CW$y\fR is the number closest to zero, and with the same sign as \f(CW$mod\fR,
1235 satisfying
1236 .Sp
1237 .Vb 1
1238 \& ($x * $y) % $mod = 1 % $mod
1239 .Ve
1240 .Sp
1241 If \f(CW$x\fR and \f(CW$y\fR are non\-zero, they must be relative primes, i.e.,
1242 \&\f(CW\*(C`bgcd($y, $mod)==1\*(C'\fR. \*(Aq\f(CW\*(C`NaN\*(C'\fR\*(Aq is returned when no modular multiplicative
1243 inverse exists.
1244 .IP \fBblog()\fR 4
1245 .IX Item "blog()"
1246 .Vb 1
1247 \& $x\->blog($base, $accuracy); # logarithm of x to the base $base
1248 .Ve
1249 .Sp
1250 If \f(CW$base\fR is not defined, Euler\*(Aqs number (e) is used:
1251 .Sp
1252 .Vb 1
1253 \& print $x\->blog(undef, 100); # log(x) to 100 digits
1254 .Ve
1255 .IP \fBbexp()\fR 4
1256 .IX Item "bexp()"
1257 .Vb 1
1258 \& $x\->bexp($accuracy); # calculate e ** X
1259 .Ve
1260 .Sp
1261 Calculates the expression \f(CW\*(C`e ** $x\*(C'\fR where \f(CW\*(C`e\*(C'\fR is Euler\*(Aqs number.
1262 .Sp
1263 This method was added in v1.82 of Math::BigInt (April 2007).
1264 .Sp
1265 See also "\fBblog()\fR".
1266 .IP \fBbilog2()\fR 4
1267 .IX Item "bilog2()"
1268 Base 2 logarithm rounded down towards the nearest integer.
1269 .Sp
1270 .Vb 1
1271 \& $x\->bilog2(); # int(log2(x)) = int(log(x)/log(2))
1272 .Ve
1273 .Sp
1274 In list context a second argument is returned. This is 1 if the result is
1275 exact, i.e., the input is an exact power of 2, and 0 otherwise.
1276 .IP \fBbilog10()\fR 4
1277 .IX Item "bilog10()"
1278 Base 10 logarithm rounded down towards the nearest integer.
1279 .Sp
1280 .Vb 1
1281 \& $x\->bilog10(); # int(log10(x)) = int(log(x)/log(10))
1282 .Ve
1283 .Sp
1284 In list context a second argument is returned. This is 1 if the result is
1285 exact, i.e., the input is an exact power of 10, and 0 otherwise.
1286 .IP \fBbclog2()\fR 4
1287 .IX Item "bclog2()"
1288 Base 2 logarithm rounded up towards the nearest integer.
1289 .Sp
1290 .Vb 1
1291 \& $x\->bclog2(); # ceil(log2(x)) = ceil(log(x)/log(2))
1292 .Ve
1293 .Sp
1294 In list context a second argument is returned. This is 1 if the result is
1295 exact, i.e., the input is an exact power of 2, and 0 otherwise.
1296 .IP \fBbclog10()\fR 4
1297 .IX Item "bclog10()"
1298 Base 10 logarithm rounded up towards the nearest integer.
1299 .Sp
1300 .Vb 1
1301 \& $x\->bclog10(); # ceil(log10(x)) = ceil(log(x)/log(10))
1302 .Ve
1303 .Sp
1304 In list context a second argument is returned. This is 1 if the result is
1305 exact, i.e., the input is an exact power of 10, and 0 otherwise.
1306 .IP \fBbnok()\fR 4
1307 .IX Item "bnok()"
1308 Combinations.
1309 .Sp
1310 .Vb 1
1311 \& $n\->bnok($k); # binomial coefficient n over k
1312 .Ve
1313 .Sp
1314 Calculates the binomial coefficient n over k, also called the "choose"
1315 function, which is the number of ways to choose a sample of k elements from a
1316 set of n distinct objects where order does not matter and replacements are not
1317 allowed. The result is equivalent to
1318 .Sp
1319 .Vb 3
1320 \& / n \e n!
1321 \& C(n, k) = | | = \-\-\-\-\-\-\-\- where 0 <= k <= n
1322 \& \e k / k!(n\-k)!
1323 .Ve
1324 .Sp
1325 when n and k are non\-negative. This method implements the full Kronenburg
1326 extension (Kronenburg, M.J. "The Binomial Coefficient for Negative Arguments."
1327 18 May 2011. http://arxiv.org/abs/1105.3689/) illustrated by the following
1328 pseudo\-code:
1329 .Sp
1330 .Vb 8
1331 \& if n >= 0 and k >= 0:
1332 \& return binomial(n, k)
1333 \& if k >= 0:
1334 \& return (\-1)^k*binomial(\-n+k\-1, k)
1335 \& if k <= n:
1336 \& return (\-1)^(n\-k)*binomial(\-k\-1, n\-k)
1337 \& else
1338 \& return 0
1339 .Ve
1340 .Sp
1341 The behaviour is identical to the behaviour of the Maple and Mathematica
1342 function for negative integers n, k.
1343 .IP \fBbperm()\fR 4
1344 .IX Item "bperm()"
1345 Permutations
1346 .Sp
1347 .Vb 1
1348 \& $n\->bperm($k);
1349 .Ve
1350 .Sp
1351 Calculates the number of ways to choose a sample of k elements from a set of n
1352 distinct objects where order does matter and replacements are not allowed.
1353 .Sp
1354 .Vb 3
1355 \& n!
1356 \& P(n, k) = \-\-\-\-\-\- where 0 <= k <= n
1357 \& (n\-k)!
1358 .Ve
1359 .IP \fBbhyperop()\fR 4
1360 .IX Item "bhyperop()"
1361 .PD 0
1362 .IP \fBhyperop()\fR 4
1363 .IX Item "hyperop()"
1364 .PD
1365 .Vb 2
1366 \& $a \-> bhyperop($n, $b); # modifies $a
1367 \& $x = $a \-> hyperop($n, $b); # does not modify $a
1368 .Ve
1369 .Sp
1370 H_n(a, b) = a[n]b is the \fIn\fRth hyperoperator,
1371 .Sp
1372 .Vb 6
1373 \& n = 0 : succession (b + 1)
1374 \& n = 1 : addition (a + b)
1375 \& n = 2 : multiplication (a * b)
1376 \& n = 3 : exponentiation (a ** b)
1377 \& n = 4 : tetration (a ** a ** ... ** a) (b occurrences of a)
1378 \& ...
1379 \&
1380 \& / b+1 if n = 0
1381 \& | a if n = 1 and b = 0
1382 \& H_n(a, b) = a[n]b = | 0 if n = 2 and b = 0
1383 \& | 1 if n >= 3 and b = 0
1384 \& \e H_(n\-1)(a, H_n(a, b\-1)) otherwise
1385 .Ve
1386 .Sp
1387 Note that the result can be a very large number, even for small operands. Also
1388 note that the backend library \f(CW\*(C`Math::BigInt::GMP\*(C'\fR silently returns the
1389 incorrect result when the numbers are larger than it can handle. It is better
1390 to use \f(CW\*(C`Math::BigInt::GMPz\*(C'\fR or \f(CW\*(C`Math::BigInt::Pari\*(C'\fR; they throw an error if
1391 they can\*(Aqt handle the number.
1392 .Sp
1393 See also "\fBbuparrow()\fR", <https://en.wikipedia.org/wiki/Hyperoperation>.
1394 .IP \fBbuparrow()\fR 4
1395 .IX Item "buparrow()"
1396 .PD 0
1397 .IP \fBuparrow()\fR 4
1398 .IX Item "uparrow()"
1399 .PD
1400 .Vb 2
1401 \& $a \-> buparrow($n, $b); # modifies $a
1402 \& $x = $a \-> uparrow($n, $b); # does not modify $a
1403 .Ve
1404 .Sp
1405 This method implements Knuth\*(Aqs up\-arrow notation, where \f(CW$n\fR is a non\-negative
1406 integer representing the number of up\-arrows. \f(CW$n\fR = 0 gives multiplication, \f(CW$n\fR =
1407 1 gives exponentiation, \f(CW$n\fR = 2 gives tetration, \f(CW$n\fR = 3 gives hexation etc. The
1408 following illustrates the relation between the first values of \f(CW$n\fR.
1409 .Sp
1410 The "\fBbuparrow()\fR" method is equivalent to the "\fBbhyperop()\fR" method with an
1411 offset of two. The following two give the same result:
1412 .Sp
1413 .Vb 2
1414 \& $x \-> buparrow($n, $b);
1415 \& $x \-> bhyperop($n + 2, $b);
1416 .Ve
1417 .Sp
1418 See also "\fBbhyperop()\fR",
1419 <https://en.wikipedia.org/wiki/Knuth%27s_up\-arrow_notation>.
1420 .IP \fBbackermann()\fR 4
1421 .IX Item "backermann()"
1422 .PD 0
1423 .IP \fBackermann()\fR 4
1424 .IX Item "ackermann()"
1425 .PD
1426 .Vb 2
1427 \& $m \-> backermann($n); # modifies $a
1428 \& $x = $m \-> ackermann($n); # does not modify $a
1429 .Ve
1430 .Sp
1431 This method implements the Ackermann function:
1432 .Sp
1433 .Vb 3
1434 \& / n + 1 if m = 0
1435 \& A(m, n) = | A(m\-1, 1) if m > 0 and n = 0
1436 \& \e A(m\-1, A(m, n\-1)) if m > 0 and n > 0
1437 .Ve
1438 .Sp
1439 Its value grows rapidly, even for small inputs. For example, A(4, 2) is an
1440 integer of 19729 decimal digits.
1441 .Sp
1442 See https://en.wikipedia.org/wiki/Ackermann_function
1443 .IP \fBbsin()\fR 4
1444 .IX Item "bsin()"
1445 .Vb 2
1446 \& my $x = Math::BigInt\->new(1);
1447 \& print $x\->bsin(100), "\en";
1448 .Ve
1449 .Sp
1450 Calculate the sine of \f(CW$x\fR, modifying \f(CW$x\fR in place.
1451 .Sp
1452 In Math::BigInt, unless upgrading is in effect, the result is truncated to an
1453 integer.
1454 .Sp
1455 This method was added in v1.87 of Math::BigInt (June 2007).
1456 .IP \fBbcos()\fR 4
1457 .IX Item "bcos()"
1458 .Vb 2
1459 \& my $x = Math::BigInt\->new(1);
1460 \& print $x\->bcos(100), "\en";
1461 .Ve
1462 .Sp
1463 Calculate the cosine of \f(CW$x\fR, modifying \f(CW$x\fR in place.
1464 .Sp
1465 In Math::BigInt, unless upgrading is in effect, the result is truncated to an
1466 integer.
1467 .Sp
1468 This method was added in v1.87 of Math::BigInt (June 2007).
1469 .IP \fBbatan()\fR 4
1470 .IX Item "batan()"
1471 .Vb 2
1472 \& my $x = Math::BigFloat\->new(0.5);
1473 \& print $x\->batan(100), "\en";
1474 .Ve
1475 .Sp
1476 Calculate the arcus tangens of \f(CW$x\fR, modifying \f(CW$x\fR in place.
1477 .Sp
1478 In Math::BigInt, unless upgrading is in effect, the result is truncated to an
1479 integer.
1480 .Sp
1481 This method was added in v1.87 of Math::BigInt (June 2007).
1482 .IP \fBbatan2()\fR 4
1483 .IX Item "batan2()"
1484 .Vb 3
1485 \& my $x = Math::BigInt\->new(1);
1486 \& my $y = Math::BigInt\->new(1);
1487 \& print $y\->batan2($x), "\en";
1488 .Ve
1489 .Sp
1490 Calculate the arcus tangens of \f(CW$y\fR divided by \f(CW$x\fR, modifying \f(CW$y\fR in place.
1491 .Sp
1492 In Math::BigInt, unless upgrading is in effect, the result is truncated to an
1493 integer.
1494 .Sp
1495 This method was added in v1.87 of Math::BigInt (June 2007).
1496 .IP \fBbfac()\fR 4
1497 .IX Item "bfac()"
1498 .Vb 1
1499 \& $x\->bfac(); # factorial of $x
1500 .Ve
1501 .Sp
1502 Returns the factorial of \f(CW$x\fR, i.e., \f(CW$x\fR*($x\-1)*($x\-2)*...*2*1, the product of
1503 all positive integers up to and including \f(CW$x\fR. \f(CW$x\fR must be > \-1. The
1504 factorial of N is commonly written as N!, or N!1, when using the multifactorial
1505 notation.
1506 .IP \fBbdfac()\fR 4
1507 .IX Item "bdfac()"
1508 .Vb 1
1509 \& $x\->bdfac(); # double factorial of $x
1510 .Ve
1511 .Sp
1512 Returns the double factorial of \f(CW$x\fR, i.e., \f(CW$x\fR*($x\-2)*($x\-4)*... \f(CW$x\fR must be
1513 > \-2. The double factorial of N is commonly written as N!!, or N!2, when using
1514 the multifactorial notation.
1515 .IP \fBbtfac()\fR 4
1516 .IX Item "btfac()"
1517 .Vb 1
1518 \& $x\->btfac(); # triple factorial of $x
1519 .Ve
1520 .Sp
1521 Returns the triple factorial of \f(CW$x\fR, i.e., \f(CW$x\fR*($x\-3)*($x\-6)*... \f(CW$x\fR must be
1522 > \-3. The triple factorial of N is commonly written as N!!!, or N!3, when using
1523 the multifactorial notation.
1524 .IP \fBbmfac()\fR 4
1525 .IX Item "bmfac()"
1526 .Vb 1
1527 \& $x\->bmfac($k); # $k\*(Aqth multifactorial of $x
1528 .Ve
1529 .Sp
1530 Returns the multi\-factorial of \f(CW$x\fR, i.e., \f(CW$x\fR*($x\-$k)*($x\-2*$k)*... \f(CW$x\fR must
1531 be > \-$k. The multi\-factorial of N is commonly written as N!K.
1532 .IP \fBbfib()\fR 4
1533 .IX Item "bfib()"
1534 .Vb 2
1535 \& $F = $n\->bfib(); # a single Fibonacci number
1536 \& @F = $n\->bfib(); # a list of Fibonacci numbers
1537 .Ve
1538 .Sp
1539 In scalar context, returns a single Fibonacci number. In list context, returns
1540 a list of Fibonacci numbers. The invocand is the last element in the output.
1541 .Sp
1542 The Fibonacci sequence is defined by
1543 .Sp
1544 .Vb 3
1545 \& F(0) = 0
1546 \& F(1) = 1
1547 \& F(n) = F(n\-1) + F(n\-2)
1548 .Ve
1549 .Sp
1550 In list context, F(0) and F(n) is the first and last number in the output,
1551 respectively. For example, if \f(CW$n\fR is 12, then \f(CW\*(C`@F = $n\->bfib()\*(C'\fR returns the
1552 following values, F(0) to F(12):
1553 .Sp
1554 .Vb 1
1555 \& 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144
1556 .Ve
1557 .Sp
1558 The sequence can also be extended to negative index n using the re\-arranged
1559 recurrence relation
1560 .Sp
1561 .Vb 1
1562 \& F(n\-2) = F(n) \- F(n\-1)
1563 .Ve
1564 .Sp
1565 giving the bidirectional sequence
1566 .Sp
1567 .Vb 2
1568 \& n \-7 \-6 \-5 \-4 \-3 \-2 \-1 0 1 2 3 4 5 6 7
1569 \& F(n) 13 \-8 5 \-3 2 \-1 1 0 1 1 2 3 5 8 13
1570 .Ve
1571 .Sp
1572 If \f(CW$n\fR is \-12, the following values, F(0) to F(12), are returned:
1573 .Sp
1574 .Vb 1
1575 \& 0, 1, \-1, 2, \-3, 5, \-8, 13, \-21, 34, \-55, 89, \-144
1576 .Ve
1577 .IP \fBblucas()\fR 4
1578 .IX Item "blucas()"
1579 .Vb 2
1580 \& $F = $n\->blucas(); # a single Lucas number
1581 \& @F = $n\->blucas(); # a list of Lucas numbers
1582 .Ve
1583 .Sp
1584 In scalar context, returns a single Lucas number. In list context, returns a
1585 list of Lucas numbers. The invocand is the last element in the output.
1586 .Sp
1587 The Lucas sequence is defined by
1588 .Sp
1589 .Vb 3
1590 \& L(0) = 2
1591 \& L(1) = 1
1592 \& L(n) = L(n\-1) + L(n\-2)
1593 .Ve
1594 .Sp
1595 In list context, L(0) and L(n) is the first and last number in the output,
1596 respectively. For example, if \f(CW$n\fR is 12, then \f(CW\*(C`@L = $n\->blucas()\*(C'\fR returns
1597 the following values, L(0) to L(12):
1598 .Sp
1599 .Vb 1
1600 \& 2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, 199, 322
1601 .Ve
1602 .Sp
1603 The sequence can also be extended to negative index n using the re\-arranged
1604 recurrence relation
1605 .Sp
1606 .Vb 1
1607 \& L(n\-2) = L(n) \- L(n\-1)
1608 .Ve
1609 .Sp
1610 giving the bidirectional sequence
1611 .Sp
1612 .Vb 2
1613 \& n \-7 \-6 \-5 \-4 \-3 \-2 \-1 0 1 2 3 4 5 6 7
1614 \& L(n) 29 \-18 11 \-7 4 \-3 1 2 1 3 4 7 11 18 29
1615 .Ve
1616 .Sp
1617 If \f(CW$n\fR is \-12, the following values, L(0) to L(\-12), are returned:
1618 .Sp
1619 .Vb 1
1620 \& 2, 1, \-3, 4, \-7, 11, \-18, 29, \-47, 76, \-123, 199, \-322
1621 .Ve
1622 .IP \fBblsft()\fR 4
1623 .IX Item "blsft()"
1624 Left shift.
1625 .Sp
1626 .Vb 2
1627 \& $x\->blsft($n); # left shift $n places in base 2
1628 \& $x\->blsft($n, $b); # left shift $n places in base $b
1629 .Ve
1630 .Sp
1631 The latter is equivalent to
1632 .Sp
1633 .Vb 1
1634 \& $x \-> bmul($b \-> copy() \-> bpow($n));
1635 .Ve
1636 .IP \fBbrsft()\fR 4
1637 .IX Item "brsft()"
1638 Right shift.
1639 .Sp
1640 .Vb 2
1641 \& $x\->brsft($n); # right shift $n places in base 2
1642 \& $x\->brsft($n, $b); # right shift $n places in base $b
1643 .Ve
1644 .Sp
1645 The latter is equivalent to
1646 .Sp
1647 .Vb 1
1648 \& $x \-> bdiv($b \-> copy() \-> bpow($n));
1649 .Ve
1650 .SS "Bitwise methods"
1651 .IX Subsection "Bitwise methods"
1652 For all bitwise methods, the operands are truncated to integers, i.e., rounded
1653 towards zero, if necessary, before the method is applied. The bitwise methods
1654 never upgrade, and they always return an integer.
1655 .IP \fBbblsft()\fR 4
1656 .IX Item "bblsft()"
1657 Bitwise left shift. This is equivalent to Perl\*(Aqs \f(CW\*(C`<<\*(C'\fR operator.
1658 .Sp
1659 .Vb 1
1660 \& $x \-> bblsft($n); # left shift $n places in base 2
1661 .Ve
1662 .Sp
1663 If \f(CW$n\fR is negative, the shifting is done in the opposite direction, so these
1664 two are equivalent for all \f(CW$x\fR and \f(CW$n\fR
1665 .Sp
1666 .Vb 2
1667 \& $y = $x \-> bblsft($n);
1668 \& $y = $x \-> bbrsft(\-$n);
1669 .Ve
1670 .Sp
1671 and also equivalent to
1672 .Sp
1673 .Vb 2
1674 \& $y = $x \-> bmul(ref($x) \-> new(2) \-> bpow($n)); # if $n > 0
1675 \& $y = $x \-> bdiv(ref($x) \-> new(2) \-> bpow($n)); # if $n < 0
1676 .Ve
1677 .IP \fBbbrsft()\fR 4
1678 .IX Item "bbrsft()"
1679 Bitwise right shift. This is equivalent to Perl\*(Aqs \f(CW\*(C`>>\*(C'\fR operator.
1680 .Sp
1681 .Vb 1
1682 \& $x \-> bbrsft($n); # right shift $n places in base 2
1683 .Ve
1684 .Sp
1685 If \f(CW$n\fR is negative, the shifting is done in the opposite direction, so these
1686 two are equivalent for all \f(CW$x\fR and \f(CW$n\fR
1687 .Sp
1688 .Vb 2
1689 \& $y = $x \-> bbrsft($n);
1690 \& $y = $x \-> bblsft(\-$n);
1691 .Ve
1692 .Sp
1693 and also equivalent to
1694 .Sp
1695 .Vb 2
1696 \& $y = $x \-> bdiv(ref($x) \-> new(2) \-> bpow($n)); # if $n > 0
1697 \& $y = $x \-> bmul(ref($x) \-> new(2) \-> bpow(\-$n)); # if $n < 0
1698 .Ve
1699 .IP \fBband()\fR 4
1700 .IX Item "band()"
1701 .Vb 1
1702 \& $x\->band($y); # bitwise and
1703 .Ve
1704 .IP \fBbior()\fR 4
1705 .IX Item "bior()"
1706 .Vb 1
1707 \& $x\->bior($y); # bitwise inclusive or
1708 .Ve
1709 .IP \fBbxor()\fR 4
1710 .IX Item "bxor()"
1711 .Vb 1
1712 \& $x\->bxor($y); # bitwise exclusive or
1713 .Ve
1714 .IP \fBbnot()\fR 4
1715 .IX Item "bnot()"
1716 .Vb 1
1717 \& $x\->bnot(); # bitwise not (two\*(Aqs complement)
1718 .Ve
1719 .Sp
1720 Two\*(Aqs complement (bitwise not). This is equivalent to, but faster than,
1721 .Sp
1722 .Vb 1
1723 \& $x\->binc()\->bneg();
1724 .Ve
1725 .SS "Rounding methods"
1726 .IX Subsection "Rounding methods"
1727 .IP \fBround()\fR 4
1728 .IX Item "round()"
1729 .Vb 1
1730 \& $x\->round($A,$P,$round_mode);
1731 .Ve
1732 .Sp
1733 Round \f(CW$x\fR to accuracy \f(CW$A\fR or precision \f(CW$P\fR using the round mode
1734 \&\f(CW$round_mode\fR.
1735 .IP \fBbround()\fR 4
1736 .IX Item "bround()"
1737 .Vb 1
1738 \& $x\->bround($N); # accuracy: preserve $N digits
1739 .Ve
1740 .Sp
1741 Rounds \f(CW$x\fR to an accuracy of \f(CW$N\fR digits.
1742 .IP \fBbfround()\fR 4
1743 .IX Item "bfround()"
1744 .Vb 1
1745 \& $x\->bfround($N);
1746 .Ve
1747 .Sp
1748 Rounds to a multiple of 10**$N. Examples:
1749 .Sp
1750 .Vb 1
1751 \& Input N Result
1752 \&
1753 \& 123456.123456 3 123500
1754 \& 123456.123456 2 123450
1755 \& 123456.123456 \-2 123456.12
1756 \& 123456.123456 \-3 123456.123
1757 .Ve
1758 .IP \fBbfloor()\fR 4
1759 .IX Item "bfloor()"
1760 .Vb 1
1761 \& $x\->bfloor();
1762 .Ve
1763 .Sp
1764 Round \f(CW$x\fR towards minus infinity, i.e., set \f(CW$x\fR to the largest integer less than
1765 or equal to \f(CW$x\fR.
1766 .IP \fBbceil()\fR 4
1767 .IX Item "bceil()"
1768 .Vb 1
1769 \& $x\->bceil();
1770 .Ve
1771 .Sp
1772 Round \f(CW$x\fR towards plus infinity, i.e., set \f(CW$x\fR to the smallest integer greater
1773 than or equal to \f(CW$x\fR.
1774 .IP \fBbint()\fR 4
1775 .IX Item "bint()"
1776 .Vb 1
1777 \& $x\->bint();
1778 .Ve
1779 .Sp
1780 Round \f(CW$x\fR towards zero.
1781 .SS "Other mathematical methods"
1782 .IX Subsection "Other mathematical methods"
1783 .IP \fBbgcd()\fR 4
1784 .IX Item "bgcd()"
1785 .Vb 2
1786 \& $x \-> bgcd($y); # GCD of $x and $y
1787 \& $x \-> bgcd($y, $z, ...); # GCD of $x, $y, $z, ...
1788 .Ve
1789 .Sp
1790 Returns the greatest common divisor (GCD), which is the largest positive
1791 integer that divides each of the operands.
1792 .IP \fBblcm()\fR 4
1793 .IX Item "blcm()"
1794 .Vb 2
1795 \& $x \-> blcm($y); # LCM of $x and $y
1796 \& $x \-> blcm($y, $z, ...); # LCM of $x, $y, $z, ...
1797 .Ve
1798 .Sp
1799 Returns the least common multiple (LCM).
1800 .SS "Object property methods"
1801 .IX Subsection "Object property methods"
1802 .IP \fBsign()\fR 4
1803 .IX Item "sign()"
1804 .Vb 1
1805 \& $x\->sign();
1806 .Ve
1807 .Sp
1808 Return the sign, of \f(CW$x\fR, meaning either \f(CW\*(C`+\*(C'\fR, \f(CW\*(C`\-\*(C'\fR, \f(CW\*(C`\-inf\*(C'\fR, \f(CW\*(C`+inf\*(C'\fR or NaN.
1809 .Sp
1810 If you want \f(CW$x\fR to have a certain sign, use one of the following methods:
1811 .Sp
1812 .Vb 5
1813 \& $x\->babs(); # \*(Aq+\*(Aq
1814 \& $x\->babs()\->bneg(); # \*(Aq\-\*(Aq
1815 \& $x\->bnan(); # \*(AqNaN\*(Aq
1816 \& $x\->binf(); # \*(Aq+inf\*(Aq
1817 \& $x\->binf(\*(Aq\-\*(Aq); # \*(Aq\-inf\*(Aq
1818 .Ve
1819 .IP \fBdigit()\fR 4
1820 .IX Item "digit()"
1821 .Vb 1
1822 \& $x\->digit($n); # return the nth digit, counting from right
1823 .Ve
1824 .Sp
1825 If \f(CW$n\fR is negative, returns the digit counting from left.
1826 .IP \fBbdigitsum()\fR 4
1827 .IX Item "bdigitsum()"
1828 .Vb 1
1829 \& $x\->bdigitsum();
1830 .Ve
1831 .Sp
1832 Computes the sum of the base 10 digits and assigns the result to the invocand.
1833 .IP \fBdigitsum()\fR 4
1834 .IX Item "digitsum()"
1835 .Vb 1
1836 \& $x\->digitsum();
1837 .Ve
1838 .Sp
1839 Computes the sum of the base 10 digits and returns it.
1840 .IP \fBlength()\fR 4
1841 .IX Item "length()"
1842 .Vb 2
1843 \& $x\->length();
1844 \& ($xl, $fl) = $x\->length();
1845 .Ve
1846 .Sp
1847 Returns the number of digits in the decimal representation of the number. In
1848 list context, returns the length of the integer and fraction part. For
1849 Math::BigInt objects, the length of the fraction part is always 0.
1850 .Sp
1851 The following probably doesn\*(Aqt do what you expect:
1852 .Sp
1853 .Vb 2
1854 \& $c = Math::BigInt\->new(123);
1855 \& print $c\->length(),"\en"; # prints 30
1856 .Ve
1857 .Sp
1858 It prints both the number of digits in the number and in the fraction part
1859 since print calls "\fBlength()\fR" in list context. Use something like:
1860 .Sp
1861 .Vb 1
1862 \& print scalar $c\->length(),"\en"; # prints 3
1863 .Ve
1864 .IP \fBmantissa()\fR 4
1865 .IX Item "mantissa()"
1866 .Vb 1
1867 \& $x\->mantissa();
1868 .Ve
1869 .Sp
1870 Return the signed mantissa of \f(CW$x\fR as a Math::BigInt.
1871 .IP \fBexponent()\fR 4
1872 .IX Item "exponent()"
1873 .Vb 1
1874 \& $x\->exponent();
1875 .Ve
1876 .Sp
1877 Return the exponent of \f(CW$x\fR as a Math::BigInt.
1878 .IP \fBparts()\fR 4
1879 .IX Item "parts()"
1880 .Vb 1
1881 \& $x\->parts();
1882 .Ve
1883 .Sp
1884 Returns the significand (mantissa) and the exponent as integers. In
1885 Math::BigFloat, both are returned as Math::BigInt objects.
1886 .IP \fBsparts()\fR 4
1887 .IX Item "sparts()"
1888 Returns the significand (mantissa) and the exponent as integers. In scalar
1889 context, only the significand is returned. The significand is the integer with
1890 the smallest absolute value. The output of "\fBsparts()\fR" corresponds to the
1891 output from "\fBbsstr()\fR".
1892 .Sp
1893 In Math::BigInt, this method is identical to "\fBparts()\fR".
1894 .IP \fBnparts()\fR 4
1895 .IX Item "nparts()"
1896 Returns the significand (mantissa) and exponent corresponding to normalized
1897 notation. In scalar context, only the significand is returned. For finite
1898 non\-zero numbers, the significand\*(Aqs absolute value is greater than or equal to
1899 1 and less than 10. The output of "\fBnparts()\fR" corresponds to the output from
1900 "\fBbnstr()\fR". In Math::BigInt, if the significand can not be represented as an
1901 integer, upgrading is performed or NaN is returned.
1902 .IP \fBeparts()\fR 4
1903 .IX Item "eparts()"
1904 Returns the significand (mantissa) and exponent corresponding to engineering
1905 notation. In scalar context, only the significand is returned. For finite
1906 non\-zero numbers, the significand\*(Aqs absolute value is greater than or equal to
1907 1 and less than 1000, and the exponent is a multiple of 3. The output of
1908 "\fBeparts()\fR" corresponds to the output from "\fBbestr()\fR". In Math::BigInt, if
1909 the significand can not be represented as an integer, upgrading is performed or
1910 NaN is returned.
1911 .IP \fBdparts()\fR 4
1912 .IX Item "dparts()"
1913 Returns the integer part and the fraction part. If the fraction part can not be
1914 represented as an integer, upgrading is performed or NaN is returned. The
1915 output of "\fBdparts()\fR" corresponds to the output from "\fBbdstr()\fR".
1916 .IP \fBfparts()\fR 4
1917 .IX Item "fparts()"
1918 Returns the smallest possible numerator and denominator so that the numerator
1919 divided by the denominator gives back the original value. For finite numbers,
1920 both values are integers. Mnemonic: fraction.
1921 .IP \fBnumerator()\fR 4
1922 .IX Item "numerator()"
1923 Together with "\fBdenominator()\fR", returns the smallest integers so that the
1924 numerator divided by the denominator reproduces the original value. With
1925 Math::BigInt, "\fBnumerator()\fR" simply returns a copy of the invocand.
1926 .IP \fBdenominator()\fR 4
1927 .IX Item "denominator()"
1928 Together with "\fBnumerator()\fR", returns the smallest integers so that the
1929 numerator divided by the denominator reproduces the original value. With
1930 Math::BigInt, "\fBdenominator()\fR" always returns either a 1 or a NaN.
1931 .SS "String conversion methods"
1932 .IX Subsection "String conversion methods"
1933 .IP \fBbstr()\fR 4
1934 .IX Item "bstr()"
1935 Returns a string representing the number using decimal notation. In
1936 Math::BigFloat, the output is zero padded according to the current accuracy or
1937 precision, if any of those are defined.
1938 .IP \fBbsstr()\fR 4
1939 .IX Item "bsstr()"
1940 Returns a string representing the number using scientific notation where both
1941 the significand (mantissa) and the exponent are integers. The output
1942 corresponds to the output from "\fBsparts()\fR".
1943 .Sp
1944 .Vb 5
1945 \& 123 is returned as "123e+0"
1946 \& 1230 is returned as "123e+1"
1947 \& 12300 is returned as "123e+2"
1948 \& 12000 is returned as "12e+3"
1949 \& 10000 is returned as "1e+4"
1950 .Ve
1951 .IP \fBbnstr()\fR 4
1952 .IX Item "bnstr()"
1953 Returns a string representing the number using normalized notation, the most
1954 common variant of scientific notation. For finite non\-zero numbers, the
1955 absolute value of the significand is greater than or equal to 1 and less than
1956 10. The output corresponds to the output from "\fBnparts()\fR".
1957 .Sp
1958 .Vb 5
1959 \& 123 is returned as "1.23e+2"
1960 \& 1230 is returned as "1.23e+3"
1961 \& 12300 is returned as "1.23e+4"
1962 \& 12000 is returned as "1.2e+4"
1963 \& 10000 is returned as "1e+4"
1964 .Ve
1965 .IP \fBbestr()\fR 4
1966 .IX Item "bestr()"
1967 Returns a string representing the number using engineering notation. For finite
1968 non\-zero numbers, the absolute value of the significand is greater than or
1969 equal to 1 and less than 1000, and the exponent is a multiple of 3. The output
1970 corresponds to the output from "\fBeparts()\fR".
1971 .Sp
1972 .Vb 5
1973 \& 123 is returned as "123e+0"
1974 \& 1230 is returned as "1.23e+3"
1975 \& 12300 is returned as "12.3e+3"
1976 \& 12000 is returned as "12e+3"
1977 \& 10000 is returned as "10e+3"
1978 .Ve
1979 .IP \fBbdstr()\fR 4
1980 .IX Item "bdstr()"
1981 Returns a string representing the number using decimal notation. The output
1982 corresponds to the output from "\fBdparts()\fR".
1983 .Sp
1984 .Vb 5
1985 \& 123 is returned as "123"
1986 \& 1230 is returned as "1230"
1987 \& 12300 is returned as "12300"
1988 \& 12000 is returned as "12000"
1989 \& 10000 is returned as "10000"
1990 .Ve
1991 .IP \fBbfstr()\fR 4
1992 .IX Item "bfstr()"
1993 Returns a string representing the number using fractional notation. The output
1994 corresponds to the output from "\fBfparts()\fR".
1995 .Sp
1996 .Vb 5
1997 \& 12.345 is returned as "2469/200"
1998 \& 123.45 is returned as "2469/20"
1999 \& 1234.5 is returned as "2469/2"
2000 \& 12345 is returned as "12345"
2001 \& 123450 is returned as "123450"
2002 .Ve
2003 .IP \fBto_hex()\fR 4
2004 .IX Item "to_hex()"
2005 .Vb 1
2006 \& $x\->to_hex();
2007 .Ve
2008 .Sp
2009 Returns a hexadecimal string representation of the number. See also
2010 "\fBfrom_hex()\fR".
2011 .IP \fBto_oct()\fR 4
2012 .IX Item "to_oct()"
2013 .Vb 1
2014 \& $x\->to_oct();
2015 .Ve
2016 .Sp
2017 Returns an octal string representation of the number. See also "\fBfrom_oct()\fR".
2018 .IP \fBto_bin()\fR 4
2019 .IX Item "to_bin()"
2020 .Vb 1
2021 \& $x\->to_bin();
2022 .Ve
2023 .Sp
2024 Returns a binary string representation of the number. See also "\fBfrom_bin()\fR".
2025 .IP \fBto_bytes()\fR 4
2026 .IX Item "to_bytes()"
2027 .Vb 2
2028 \& $x = Math::BigInt\->new("1667327589");
2029 \& $s = $x\->to_bytes(); # $s = "cafe"
2030 .Ve
2031 .Sp
2032 Returns a byte string representation of the number using big endian byte order.
2033 The invocand must be a non\-negative, finite integer. See also "\fBfrom_bytes()\fR".
2034 .IP \fBto_ieee754()\fR 4
2035 .IX Item "to_ieee754()"
2036 See "\fBto_ieee754()\fR" in Math::BigFloat.
2037 .IP \fBto_base()\fR 4
2038 .IX Item "to_base()"
2039 .Vb 4
2040 \& $x = Math::BigInt\->new("250");
2041 \& $x\->to_base(2); # returns "11111010"
2042 \& $x\->to_base(8); # returns "372"
2043 \& $x\->to_base(16); # returns "fa"
2044 .Ve
2045 .Sp
2046 Returns a string representation of the number in the given base. If a collation
2047 sequence is given, the collation sequence determines which characters are used
2048 in the output.
2049 .Sp
2050 Here are some more examples
2051 .Sp
2052 .Vb 4
2053 \& $x = Math::BigInt\->new("16")\->to_base(3); # returns "121"
2054 \& $x = Math::BigInt\->new("44027")\->to_base(36); # returns "XYZ"
2055 \& $x = Math::BigInt\->new("58314")\->to_base(42); # returns "Why"
2056 \& $x = Math::BigInt\->new("4")\->to_base(2, "\-|"); # returns "|\-\-"
2057 .Ve
2058 .Sp
2059 If the collation sequence are the bytes from "\ex00" to "\exff", and the base is
2060 256, then "\fBto_base()\fR" returns the same output as "\fBto_bytes()\fR". In the
2061 following example, \f(CW$x\fR and \f(CW$y\fR are identical:
2062 .Sp
2063 .Vb 3
2064 \& $cs = join "", map chr, 0 .. 255; # collation sequence
2065 \& $x = Math::BigInt \-> to_base("1230129310", 256, $cs)
2066 \& $y = Math::BigInt \-> to_bytes("1230129310");
2067 .Ve
2068 .Sp
2069 See "\fBfrom_base()\fR" for information and examples.
2070 .IP \fBto_base_num()\fR 4
2071 .IX Item "to_base_num()"
2072 Converts the given number to the given base. This method is equivalent to
2073 "\fBto_base()\fR", but returns numbers in an array rather than characters in a
2074 string. In the output, the first element is the most significant.
2075 .Sp
2076 .Vb 2
2077 \& $x = Math::BigInt\->new(13); # decimal 13 is binary 1101
2078 \& $x\->to_base_num(2); # returns [1, 1, 0, 1]
2079 \&
2080 \& $x = Math::BigInt\->new(65191);
2081 \& $x\->to_base_num(128); # returns [3, 125, 39]
2082 .Ve
2083 .IP \fBas_hex()\fR 4
2084 .IX Item "as_hex()"
2085 .Vb 1
2086 \& $x\->as_hex();
2087 .Ve
2088 .Sp
2089 As, "\fBto_hex()\fR", but with a "0x" prefix.
2090 .IP \fBas_oct()\fR 4
2091 .IX Item "as_oct()"
2092 .Vb 1
2093 \& $x\->as_oct();
2094 .Ve
2095 .Sp
2096 As, "\fBto_oct()\fR", but with a "0" prefix.
2097 .IP \fBas_bin()\fR 4
2098 .IX Item "as_bin()"
2099 .Vb 1
2100 \& $x\->as_bin();
2101 .Ve
2102 .Sp
2103 As, "\fBto_bin()\fR", but with a "0b" prefix.
2104 .IP \fBas_bytes()\fR 4
2105 .IX Item "as_bytes()"
2106 This is an alias for "\fBto_bytes()\fR".
2107 .SS "Other conversion methods"
2108 .IX Subsection "Other conversion methods"
2109 .IP \fBnumify()\fR 4
2110 .IX Item "numify()"
2111 .Vb 1
2112 \& print $x\->numify();
2113 .Ve
2114 .Sp
2115 Returns a Perl scalar from \f(CW$x\fR. It is used automatically whenever a scalar is
2116 needed, for instance in array index operations.
2117 .SS "Utility methods"
2118 .IX Subsection "Utility methods"
2119 These utility methods are made public
2120 .IP \fBdec_str_to_dec_flt_str()\fR 4
2121 .IX Item "dec_str_to_dec_flt_str()"
2122 Takes a string representing any valid number using decimal notation and
2123 converts it to a string representing the same number using decimal floating
2124 point notation. The output consists of five parts joined together: the sign of
2125 the significand, the absolute value of the significand as the smallest possible
2126 integer, the letter "e", the sign of the exponent, and the absolute value of
2127 the exponent. If the input is invalid, nothing is returned.
2128 .Sp
2129 .Vb 1
2130 \& $str2 = $class \-> dec_str_to_dec_flt_str($str1);
2131 .Ve
2132 .Sp
2133 Some examples
2134 .Sp
2135 .Vb 4
2136 \& Input Output
2137 \& 31400.00e\-4 +314e\-2
2138 \& \-0.00012300e8 \-123e+2
2139 \& 0 +0e+0
2140 .Ve
2141 .IP \fBhex_str_to_dec_flt_str()\fR 4
2142 .IX Item "hex_str_to_dec_flt_str()"
2143 Takes a string representing any valid number using hexadecimal notation and
2144 converts it to a string representing the same number using decimal floating
2145 point notation. The output has the same format as that of
2146 "\fBdec_str_to_dec_flt_str()\fR".
2147 .Sp
2148 .Vb 1
2149 \& $str2 = $class \-> hex_str_to_dec_flt_str($str1);
2150 .Ve
2151 .Sp
2152 Some examples
2153 .Sp
2154 .Vb 2
2155 \& Input Output
2156 \& 0xff +255e+0
2157 .Ve
2158 .Sp
2159 Some examples
2160 .IP \fBoct_str_to_dec_flt_str()\fR 4
2161 .IX Item "oct_str_to_dec_flt_str()"
2162 Takes a string representing any valid number using octal notation and converts
2163 it to a string representing the same number using decimal floating point
2164 notation. The output has the same format as that of
2165 "\fBdec_str_to_dec_flt_str()\fR".
2166 .Sp
2167 .Vb 1
2168 \& $str2 = $class \-> oct_str_to_dec_flt_str($str1);
2169 .Ve
2170 .IP \fBbin_str_to_dec_flt_str()\fR 4
2171 .IX Item "bin_str_to_dec_flt_str()"
2172 Takes a string representing any valid number using binary notation and converts
2173 it to a string representing the same number using decimal floating point
2174 notation. The output has the same format as that of
2175 "\fBdec_str_to_dec_flt_str()\fR".
2176 .Sp
2177 .Vb 1
2178 \& $str2 = $class \-> bin_str_to_dec_flt_str($str1);
2179 .Ve
2180 .IP \fBdec_str_to_dec_str()\fR 4
2181 .IX Item "dec_str_to_dec_str()"
2182 Takes a string representing any valid number using decimal notation and
2183 converts it to a string representing the same number using decimal notation. If
2184 the number represents an integer, the output consists of a sign and the
2185 absolute value. If the number represents a non\-integer, the output consists of
2186 a sign, the integer part of the number, the decimal point ".", and the fraction
2187 part of the number without any trailing zeros. If the input is invalid, nothing
2188 is returned.
2189 .IP \fBhex_str_to_dec_str()\fR 4
2190 .IX Item "hex_str_to_dec_str()"
2191 Takes a string representing any valid number using hexadecimal notation and
2192 converts it to a string representing the same number using decimal notation.
2193 The output has the same format as that of "\fBdec_str_to_dec_str()\fR".
2194 .IP \fBoct_str_to_dec_str()\fR 4
2195 .IX Item "oct_str_to_dec_str()"
2196 Takes a string representing any valid number using octal notation and converts
2197 it to a string representing the same number using decimal notation. The output
2198 has the same format as that of "\fBdec_str_to_dec_str()\fR".
2199 .IP \fBbin_str_to_dec_str()\fR 4
2200 .IX Item "bin_str_to_dec_str()"
2201 Takes a string representing any valid number using binary notation and converts
2202 it to a string representing the same number using decimal notation. The output
2203 has the same format as that of "\fBdec_str_to_dec_str()\fR".
2204 .SH "ACCURACY AND PRECISION"
2205 .IX Header "ACCURACY AND PRECISION"
2206 Math::BigInt and Math::BigFloat have full support for accuracy and precision
2207 based rounding, both automatically after every operation, as well as manually.
2208 .PP
2209 This section describes the accuracy/precision handling in Math::BigInt and
2210 Math::BigFloat as it used to be and as it is now, complete with an explanation
2211 of all terms and abbreviations.
2212 .PP
2213 Not yet implemented things (but with correct description) are marked with \*(Aq!\*(Aq,
2214 things that need to be answered are marked with \*(Aq?\*(Aq.
2215 .PP
2216 In the next paragraph follows a short description of terms used here (because
2217 these may differ from terms used by others people or documentation).
2218 .PP
2219 During the rest of this document, the shortcuts A (for accuracy), P (for
2220 precision), R (rounding mode), and F (fallback) are be used.
2221 .SS "Accuracy A"
2222 .IX Subsection "Accuracy A"
2223 Number of significant digits. Leading zeros are not counted. A number may have
2224 an accuracy greater than the non\-zero digits when there are zeros in it or
2225 trailing zeros. For example, 123.456 has A of 6, 10203 has 5, 123.0506 has 7,
2226 123.45000 has 8 and 0.000123 has 3.
2227 .PP
2228 The string output (of floating point numbers) is padded with zeros:
2229 .PP
2230 .Vb 5
2231 \& Initial value P A Result String
2232 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
2233 \& 1234.01 3 1230 1230
2234 \& 1234.01 6 1234.01 1234.01
2235 \& 1234.1 8 1234.1 1234.1000
2236 .Ve
2237 .PP
2238 For Math::BigInt objects, no padding occurs.
2239 .SS "Precision P"
2240 .IX Subsection "Precision P"
2241 Precision is a fixed number of digits before (positive) or after (negative) the
2242 decimal point. For example, 123.45 has a precision of \-2. 0 means an integer
2243 like 123 (or 120). A precision of 2 means at least two digits to the left of
2244 the decimal point are zero, so 123 with P = 1 becomes 120. Note that numbers
2245 with zeros before the decimal point may have different precisions, because 1200
2246 can have P = 0, 1 or 2 (depending on what the initial value was). It could also
2247 have p < 0, when the digits after the decimal point are zero.
2248 .PP
2249 The string output (of floating point numbers) is padded with zeros:
2250 .PP
2251 .Vb 9
2252 \& Initial value P A Result String
2253 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
2254 \& 1234.01 \-3 1000 1000
2255 \& 1234 \-2 1200 1200
2256 \& 1234.5 \-1 1230 1230
2257 \& 1234.001 1 1234 1234.0
2258 \& 1234.01 0 1234 1234
2259 \& 1234.01 2 1234.01 1234.01
2260 \& 1234.01 5 1234.01 1234.01000
2261 .Ve
2262 .PP
2263 For Math::BigInt objects, no padding occurs.
2264 .SS "Rounding mode R"
2265 .IX Subsection "Rounding mode R"
2266 When rounding a number, different \*(Aqstyles\*(Aq or \*(Aqkinds\*(Aq of rounding are possible.
2267 (Note that random rounding, as in Math::Round, is not implemented.)
2268 .PP
2269 \fIDirected rounding\fR
2270 .IX Subsection "Directed rounding"
2271 .PP
2272 These round modes always round in the same direction.
2273 .IP \*(Aqtrunc\*(Aq 4
2274 .IX Item "'trunc'"
2275 Round towards zero. Remove all digits following the rounding place, i.e.,
2276 replace them with zeros. Thus, 987.65 rounded to tens (P=1) becomes 980, and
2277 rounded to the fourth significant digit becomes 987.6 (A=4). 123.456 rounded to
2278 the second place after the decimal point (P=\-2) becomes 123.46. This
2279 corresponds to the IEEE 754 rounding mode \*(AqroundTowardZero\*(Aq.
2280 .PP
2281 \fIRounding to nearest\fR
2282 .IX Subsection "Rounding to nearest"
2283 .PP
2284 These rounding modes round to the nearest digit. They differ in how they
2285 determine which way to round in the ambiguous case when there is a tie.
2286 .IP \*(Aqeven\*(Aq 4
2287 .IX Item "'even'"
2288 Round towards the nearest even digit, e.g., when rounding to nearest integer,
2289 \&\-5.5 becomes \-6, 4.5 becomes 4, but 4.501 becomes 5. This corresponds to the
2290 IEEE 754 rounding mode \*(AqroundTiesToEven\*(Aq.
2291 .IP \*(Aqodd\*(Aq 4
2292 .IX Item "'odd'"
2293 Round towards the nearest odd digit, e.g., when rounding to nearest integer,
2294 4.5 becomes 5, \-5.5 becomes \-5, but 5.501 becomes 6. This corresponds to the
2295 IEEE 754 rounding mode \*(AqroundTiesToOdd\*(Aq.
2296 .IP \*(Aq+inf\*(Aq 4
2297 .IX Item "'+inf'"
2298 Round towards plus infinity, i.e., always round up. E.g., when rounding to the
2299 nearest integer, 4.5 becomes 5, \-5.5 becomes \-5, and 4.501 also becomes 5. This
2300 corresponds to the IEEE 754 rounding mode \*(AqroundTiesToPositive\*(Aq.
2301 .IP \*(Aq\-inf\*(Aq 4
2302 .IX Item "'-inf'"
2303 Round towards minus infinity, i.e., always round down. E.g., when rounding to
2304 the nearest integer, 4.5 becomes 4, \-5.5 becomes \-6, but 4.501 becomes 5. This
2305 corresponds to the IEEE 754 rounding mode \*(AqroundTiesToNegative\*(Aq.
2306 .IP \*(Aqzero\*(Aq 4
2307 .IX Item "'zero'"
2308 Round towards zero, i.e., round positive numbers down and negative numbers up.
2309 E.g., when rounding to the nearest integer, 4.5 becomes 4, \-5.5 becomes \-5, but
2310 4.501 becomes 5. This corresponds to the IEEE 754 rounding mode
2311 \&\*(AqroundTiesToZero\*(Aq.
2312 .IP \*(Aqcommon\*(Aq 4
2313 .IX Item "'common'"
2314 Round away from zero, i.e., round to the number with the largest absolute
2315 value. E.g., when rounding to the nearest integer, \-1.5 becomes \-2, 1.5 becomes
2316 2 and 1.49 becomes 1. This corresponds to the IEEE 754 rounding mode
2317 \&\*(AqroundTiesToAway\*(Aq.
2318 .SS "Fallback F"
2319 .IX Subsection "Fallback F"
2320 When neither A nor P are defined, the fallback accuracy is used when computing
2321 values that would potentially give an infinite number of digits, e.g.,
2322 division, roots, logarithms, trigonometric functions etc.
2323 .SS "More details on rounding"
2324 .IX Subsection "More details on rounding"
2325 The handling of A & P in MBI/MBF (the old core code shipped with Perl versions
2326 <= 5.7.2) is like this:
2327 .IP Precision 4
2328 .IX Item "Precision"
2329 .Vb 3
2330 \& * bfround($p) is able to round to $p number of digits after the decimal
2331 \& point
2332 \& * otherwise P is unused
2333 .Ve
2334 .IP "Accuracy (significant digits)" 4
2335 .IX Item "Accuracy (significant digits)"
2336 .Vb 10
2337 \& * bround($a) rounds to $a significant digits
2338 \& * only bdiv() and bsqrt() take A as (optional) parameter
2339 \& + other operations simply create the same number (bneg etc), or
2340 \& more (bmul) of digits
2341 \& + rounding/truncating is only done when explicitly calling one
2342 \& of bround or bfround, and never for Math::BigInt (not implemented)
2343 \& * bsqrt() simply hands its accuracy argument over to bdiv.
2344 \& * the documentation and the comment in the code indicate two
2345 \& different ways on how bdiv() determines the maximum number
2346 \& of digits it should calculate, and the actual code does yet
2347 \& another thing
2348 \& POD:
2349 \& max($Math::BigFloat::div_scale,length(dividend)+length(divisor))
2350 \& Comment:
2351 \& result has at most max(scale, length(dividend), length(divisor)) digits
2352 \& Actual code:
2353 \& scale = max(scale, length(dividend)\-1,length(divisor)\-1);
2354 \& scale += length(divisor) \- length(dividend);
2355 \& So for lx = 3, ly = 9, scale = 10, scale will actually be 16 (10
2356 \& So for lx = 3, ly = 9, scale = 10, scale will actually be 16
2357 \& (10+9\-3). Actually, the \*(Aqdifference\*(Aq added to the scale is cal\-
2358 \& culated from the number of "significant digits" in dividend and
2359 \& divisor, which is derived by looking at the length of the man\-
2360 \& tissa. Which is wrong, since it includes the + sign (oops) and
2361 \& actually gets 2 for \*(Aq+100\*(Aq and 4 for \*(Aq+101\*(Aq. Oops again. Thus
2362 \& 124/3 with div_scale=1 will get you \*(Aq41.3\*(Aq based on the strange
2363 \& assumption that 124 has 3 significant digits, while 120/7 will
2364 \& get you \*(Aq17\*(Aq, not \*(Aq17.1\*(Aq since 120 is thought to have 2 signif\-
2365 \& icant digits. The rounding after the division then uses the
2366 \& remainder and $y to determine whether it must round up or down.
2367 \& ? I have no idea which is the right way. That\*(Aqs why I used a slightly more
2368 \& ? simple scheme and tweaked the few failing testcases to match it.
2369 .Ve
2370 .PP
2371 This is how it works now:
2372 .IP Setting/Accessing 4
2373 .IX Item "Setting/Accessing"
2374 .Vb 10
2375 \& * You can set the A global via Math::BigInt\->accuracy() or
2376 \& Math::BigFloat\->accuracy() or whatever class you are using.
2377 \& * You can also set P globally by using Math::SomeClass\->precision()
2378 \& likewise.
2379 \& * Globals are classwide, and not inherited by subclasses.
2380 \& * to undefine A, use Math::SomeClass\->accuracy(undef);
2381 \& * to undefine P, use Math::SomeClass\->precision(undef);
2382 \& * Setting Math::SomeClass\->accuracy() clears automatically
2383 \& Math::SomeClass\->precision(), and vice versa.
2384 \& * To be valid, A must be > 0, P can have any value.
2385 \& * If P is negative, this means round to the P\*(Aqth place to the right of the
2386 \& decimal point; positive values mean to the left of the decimal point.
2387 \& P of 0 means round to integer.
2388 \& * to find out the current global A, use Math::SomeClass\->accuracy()
2389 \& * to find out the current global P, use Math::SomeClass\->precision()
2390 \& * use $x\->accuracy() respective $x\->precision() for the local
2391 \& setting of $x.
2392 \& * Please note that $x\->accuracy() respective $x\->precision()
2393 \& return eventually defined global A or P, when $x\*(Aqs A or P is not
2394 \& set.
2395 .Ve
2396 .IP "Creating numbers" 4
2397 .IX Item "Creating numbers"
2398 .Vb 12
2399 \& * When you create a number, you can give the desired A or P via:
2400 \& $x = Math::BigInt\->new($number,$A,$P);
2401 \& * Only one of A or P can be defined, otherwise the result is NaN
2402 \& * If no A or P is give ($x = Math::BigInt\->new($number) form), then the
2403 \& globals (if set) will be used. Thus changing the global defaults later on
2404 \& will not change the A or P of previously created numbers (i.e., A and P of
2405 \& $x will be what was in effect when $x was created)
2406 \& * If given undef for A and P, NO rounding will occur, and the globals will
2407 \& NOT be used. This is used by subclasses to create numbers without
2408 \& suffering rounding in the parent. Thus a subclass is able to have its own
2409 \& globals enforced upon creation of a number by using
2410 \& $x = Math::BigInt\->new($number,undef,undef):
2411 \&
2412 \& use Math::BigInt::SomeSubclass;
2413 \& use Math::BigInt;
2414 \&
2415 \& Math::BigInt\->accuracy(2);
2416 \& Math::BigInt::SomeSubclass\->accuracy(3);
2417 \& $x = Math::BigInt::SomeSubclass\->new(1234);
2418 \&
2419 \& $x is now 1230, and not 1200. A subclass might choose to implement
2420 \& this otherwise, e.g. falling back to the parent\*(Aqs A and P.
2421 .Ve
2422 .IP Usage 4
2423 .IX Item "Usage"
2424 .Vb 7
2425 \& * If A or P are enabled/defined, they are used to round the result of each
2426 \& operation according to the rules below
2427 \& * Negative P is ignored in Math::BigInt, since Math::BigInt objects never
2428 \& have digits after the decimal point
2429 \& * Math::BigFloat uses Math::BigInt internally, but setting A or P inside
2430 \& Math::BigInt as globals does not tamper with the parts of a Math::BigFloat.
2431 \& A flag is used to mark all Math::BigFloat numbers as \*(Aqnever round\*(Aq.
2432 .Ve
2433 .IP Precedence 4
2434 .IX Item "Precedence"
2435 .Vb 10
2436 \& * It only makes sense that a number has only one of A or P at a time.
2437 \& If you set either A or P on one object, or globally, the other one will
2438 \& be automatically cleared.
2439 \& * If two objects are involved in an operation, and one of them has A in
2440 \& effect, and the other P, this results in an error (NaN).
2441 \& * A takes precedence over P (Hint: A comes before P).
2442 \& If neither of them is defined, nothing is used, i.e. the result will have
2443 \& as many digits as it can (with an exception for bdiv/bsqrt) and will not
2444 \& be rounded.
2445 \& * There is another setting for bdiv() (and thus for bsqrt()). If neither of
2446 \& A or P is defined, bdiv() will use a fallback (F) of $div_scale digits.
2447 \& If either the dividend\*(Aqs or the divisor\*(Aqs mantissa has more digits than
2448 \& the value of F, the higher value will be used instead of F.
2449 \& This is to limit the digits (A) of the result (just consider what would
2450 \& happen with unlimited A and P in the case of 1/3 :\-)
2451 \& * bdiv will calculate (at least) 4 more digits than required (determined by
2452 \& A, P or F), and, if F is not used, round the result
2453 \& (this will still fail in the case of a result like 0.12345000000001 with A
2454 \& or P of 5, but this can not be helped \- or can it?)
2455 \& * Thus you can have the math done by on Math::Big* class in two modi:
2456 \& + never round (this is the default):
2457 \& This is done by setting A and P to undef. No math operation
2458 \& will round the result, with bdiv() and bsqrt() as exceptions to guard
2459 \& against overflows. You must explicitly call bround(), bfround() or
2460 \& round() (the latter with parameters).
2461 \& Note: Once you have rounded a number, the settings will \*(Aqstick\*(Aq on it
2462 \& and \*(Aqinfect\*(Aq all other numbers engaged in math operations with it, since
2463 \& local settings have the highest precedence. So, to get SaferRound[tm],
2464 \& use a copy() before rounding like this:
2465 \&
2466 \& $x = Math::BigFloat\->new(12.34);
2467 \& $y = Math::BigFloat\->new(98.76);
2468 \& $z = $x * $y; # 1218.6984
2469 \& print $x\->copy()\->bround(3); # 12.3 (but A is now 3!)
2470 \& $z = $x * $y; # still 1218.6984, without
2471 \& # copy would have been 1210!
2472 \&
2473 \& + round after each op:
2474 \& After each single operation (except for testing like is_zero()), the
2475 \& method round() is called and the result is rounded appropriately. By
2476 \& setting proper values for A and P, you can have all\-the\-same\-A or
2477 \& all\-the\-same\-P modes. For example, Math::Currency might set A to undef,
2478 \& and P to \-2, globally.
2479 \&
2480 \& ?Maybe an extra option that forbids local A & P settings would be in order,
2481 \& ?so that intermediate rounding does not \*(Aqpoison\*(Aq further math?
2482 .Ve
2483 .IP "Overriding globals" 4
2484 .IX Item "Overriding globals"
2485 .Vb 10
2486 \& * you will be able to give A, P and R as an argument to all the calculation
2487 \& routines; the second parameter is A, the third one is P, and the fourth is
2488 \& R (shift right by one for binary operations like badd). P is used only if
2489 \& the first parameter (A) is undefined. These three parameters override the
2490 \& globals in the order detailed as follows, i.e. the first defined value
2491 \& wins:
2492 \& (local: per object, global: global default, parameter: argument to sub)
2493 \& + parameter A
2494 \& + parameter P
2495 \& + local A (if defined on both of the operands: smaller one is taken)
2496 \& + local P (if defined on both of the operands: bigger one is taken)
2497 \& + global A
2498 \& + global P
2499 \& + global F
2500 \& * bsqrt() will hand its arguments to bdiv(), as it used to, only now for two
2501 \& arguments (A and P) instead of one
2502 .Ve
2503 .IP "Local settings" 4
2504 .IX Item "Local settings"
2505 .Vb 5
2506 \& * You can set A or P locally by using $x\->accuracy() or
2507 \& $x\->precision()
2508 \& and thus force different A and P for different objects/numbers.
2509 \& * Setting A or P this way immediately rounds $x to the new value.
2510 \& * $x\->accuracy() clears $x\->precision(), and vice versa.
2511 .Ve
2512 .IP Rounding 4
2513 .IX Item "Rounding"
2514 .Vb 10
2515 \& * the rounding routines will use the respective global or local settings.
2516 \& bround() is for accuracy rounding, while bfround() is for precision
2517 \& * the two rounding functions take as the second parameter one of the
2518 \& following rounding modes (R):
2519 \& \*(Aqeven\*(Aq, \*(Aqodd\*(Aq, \*(Aq+inf\*(Aq, \*(Aq\-inf\*(Aq, \*(Aqzero\*(Aq, \*(Aqtrunc\*(Aq, \*(Aqcommon\*(Aq
2520 \& * you can set/get the global R by using Math::SomeClass\->round_mode()
2521 \& or by setting $Math::SomeClass::round_mode
2522 \& * after each operation, $result\->round() is called, and the result may
2523 \& eventually be rounded (that is, if A or P were set either locally,
2524 \& globally or as parameter to the operation)
2525 \& * to manually round a number, call $x\->round($A,$P,$round_mode);
2526 \& this will round the number by using the appropriate rounding function
2527 \& and then normalize it.
2528 \& * rounding modifies the local settings of the number:
2529 \&
2530 \& $x = Math::BigFloat\->new(123.456);
2531 \& $x\->accuracy(5);
2532 \& $x\->bround(4);
2533 \&
2534 \& Here 4 takes precedence over 5, so 123.5 is the result and $x\->accuracy()
2535 \& will be 4 from now on.
2536 .Ve
2537 .IP "Default values" 4
2538 .IX Item "Default values"
2539 .Vb 4
2540 \& * A: undef
2541 \& * P: undef
2542 \& * R: \*(Aqeven\*(Aq
2543 \& * F: 40
2544 .Ve
2545 .IP Remarks 4
2546 .IX Item "Remarks"
2547 .Vb 5
2548 \& * The defaults are set up so that the new code gives the same results as
2549 \& the old code (except in a few cases on bdiv):
2550 \& + Both A and P are undefined and thus will not be used for rounding
2551 \& after each operation.
2552 \& + round() is thus a no\-op, unless given extra parameters A and P
2553 .Ve
2554 .SH INTERNALS
2555 .IX Header "INTERNALS"
2556 You should neither care about nor depend on the internal representation; it
2557 might change without notice. Use \fBONLY\fR method calls like \f(CW\*(C`$x\->sign();\*(C'\fR
2558 instead relying on the internal representation.
2559 .SS "Math Library"
2560 .IX Subsection "Math Library"
2561 The mathematical computations are performed by a backend library. It is not
2562 required to specify which backend library to use, but some backend libraries
2563 are much faster than the default library.
2564 .PP
2565 \fIThe default library\fR
2566 .IX Subsection "The default library"
2567 .PP
2568 The default library is Math::BigInt::Calc, which is implemented in pure Perl
2569 and hence does not require a compiler.
2570 .PP
2571 \fISpecifying a library\fR
2572 .IX Subsection "Specifying a library"
2573 .PP
2574 The simple case
2575 .PP
2576 .Vb 1
2577 \& use Math::BigInt;
2578 .Ve
2579 .PP
2580 is equivalent to saying
2581 .PP
2582 .Vb 1
2583 \& use Math::BigInt try => \*(AqCalc\*(Aq;
2584 .Ve
2585 .PP
2586 You can use a different backend library with, e.g.,
2587 .PP
2588 .Vb 1
2589 \& use Math::BigInt try => \*(AqGMP\*(Aq;
2590 .Ve
2591 .PP
2592 which attempts to load the Math::BigInt::GMP library, and falls back to the
2593 default library if the specified library can\*(Aqt be loaded.
2594 .PP
2595 Multiple libraries can be specified by separating them by a comma, e.g.,
2596 .PP
2597 .Vb 1
2598 \& use Math::BigInt try => \*(AqGMP,Pari\*(Aq;
2599 .Ve
2600 .PP
2601 If you request a specific set of libraries and do not allow fallback to the
2602 default library, specify them using "only",
2603 .PP
2604 .Vb 1
2605 \& use Math::BigInt only => \*(AqGMP,Pari\*(Aq;
2606 .Ve
2607 .PP
2608 If you prefer a specific set of libraries, but want to see a warning if the
2609 fallback library is used, specify them using "lib",
2610 .PP
2611 .Vb 1
2612 \& use Math::BigInt lib => \*(AqGMP,Pari\*(Aq;
2613 .Ve
2614 .PP
2615 The following first tries to find Math::BigInt::Foo, then Math::BigInt::Bar,
2616 and if this also fails, reverts to Math::BigInt::Calc:
2617 .PP
2618 .Vb 1
2619 \& use Math::BigInt try => \*(AqFoo,Math::BigInt::Bar\*(Aq;
2620 .Ve
2621 .PP
2622 \fIWhich library to use?\fR
2623 .IX Subsection "Which library to use?"
2624 .PP
2625 \&\fBNote\fR: General purpose packages should not be explicit about the library to
2626 use; let the script author decide which is best.
2627 .PP
2628 Math::BigInt::GMP, Math::BigInt::Pari, and Math::BigInt::GMPz are in
2629 cases involving big numbers much faster than Math::BigInt::Calc. However
2630 these libraries are slower when dealing with very small numbers (less than
2631 about 20 digits) and when converting very large numbers to decimal (for
2632 instance for printing, rounding, calculating their length in decimal etc.).
2633 .PP
2634 So please select carefully what library you want to use.
2635 .PP
2636 Different low\-level libraries use different formats to store the numbers, so
2637 mixing them won\*(Aqt work. You should not depend on the number having a specific
2638 internal format.
2639 .PP
2640 See the respective math library module documentation for further details.
2641 .PP
2642 \fILoading multiple libraries\fR
2643 .IX Subsection "Loading multiple libraries"
2644 .PP
2645 The first library that is successfully loaded is the one that will be used. Any
2646 further attempts at loading a different module will be ignored. This is to
2647 avoid the situation where module A requires math library X, and module B
2648 requires math library Y, causing modules A and B to be incompatible. For
2649 example,
2650 .PP
2651 .Vb 2
2652 \& use Math::BigInt; # loads default "Calc"
2653 \& use Math::BigFloat only => "GMP"; # ignores "GMP"
2654 .Ve
2655 .SS Sign
2656 .IX Subsection "Sign"
2657 The sign is either \*(Aq+\*(Aq, \*(Aq\-\*(Aq, \*(AqNaN\*(Aq, \*(Aq+inf\*(Aq or \*(Aq\-inf\*(Aq.
2658 .PP
2659 A sign of \*(AqNaN\*(Aq is used to represent values that are not numbers, e.g., the
2660 result of 0/0. \*(Aq+inf\*(Aq and \*(Aq\-inf\*(Aq represen positive and negative infinity,
2661 respectively. For example you get \*(Aq+inf\*(Aq when dividing a positive number by 0,
2662 and \*(Aq\-inf\*(Aq when dividing any negative number by 0.
2663 .SH EXAMPLES
2664 .IX Header "EXAMPLES"
2665 .Vb 1
2666 \& use Math::BigInt;
2667 \&
2668 \& sub bigint { Math::BigInt\->new(shift); }
2669 \&
2670 \& $x = Math::BigInt\->bstr("1234") # string "1234"
2671 \& $x = "$x"; # same as bstr()
2672 \& $x = Math::BigInt\->bneg("1234"); # Math::BigInt "\-1234"
2673 \& $x = Math::BigInt\->babs("\-12345"); # Math::BigInt "12345"
2674 \& $x = Math::BigInt\->bnorm("\-0.00"); # Math::BigInt "0"
2675 \& $x = bigint(1) + bigint(2); # Math::BigInt "3"
2676 \& $x = bigint(1) + "2"; # ditto ("2" becomes a Math::BigInt)
2677 \& $x = bigint(1); # Math::BigInt "1"
2678 \& $x = $x + 5 / 2; # Math::BigInt "3"
2679 \& $x = $x ** 3; # Math::BigInt "27"
2680 \& $x *= 2; # Math::BigInt "54"
2681 \& $x = Math::BigInt\->new(0); # Math::BigInt "0"
2682 \& $x\-\-; # Math::BigInt "\-1"
2683 \& $x = Math::BigInt\->badd(4,5) # Math::BigInt "9"
2684 \& print $x\->bsstr(); # 9e+0
2685 .Ve
2686 .PP
2687 Examples for rounding:
2688 .PP
2689 .Vb 2
2690 \& use Math::BigFloat;
2691 \& use Test::More;
2692 \&
2693 \& $x = Math::BigFloat\->new(123.4567);
2694 \& $y = Math::BigFloat\->new(123.456789);
2695 \& Math::BigFloat\->accuracy(4); # no more A than 4
2696 \&
2697 \& is ($x\->copy()\->bround(),123.4); # even rounding
2698 \& print $x\->copy()\->bround(),"\en"; # 123.4
2699 \& Math::BigFloat\->round_mode(\*(Aqodd\*(Aq); # round to odd
2700 \& print $x\->copy()\->bround(),"\en"; # 123.5
2701 \& Math::BigFloat\->accuracy(5); # no more A than 5
2702 \& Math::BigFloat\->round_mode(\*(Aqodd\*(Aq); # round to odd
2703 \& print $x\->copy()\->bround(),"\en"; # 123.46
2704 \& $y = $x\->copy()\->bround(4),"\en"; # A = 4: 123.4
2705 \& print "$y, ",$y\->accuracy(),"\en"; # 123.4, 4
2706 \&
2707 \& Math::BigFloat\->accuracy(undef); # A not important now
2708 \& Math::BigFloat\->precision(2); # P important
2709 \& print $x\->copy()\->bnorm(),"\en"; # 123.46
2710 \& print $x\->copy()\->bround(),"\en"; # 123.46
2711 .Ve
2712 .PP
2713 Examples for converting:
2714 .PP
2715 .Vb 2
2716 \& my $x = Math::BigInt\->new(\*(Aq0b1\*(Aq.\*(Aq01\*(Aq x 123);
2717 \& print "bin: ",$x\->as_bin()," hex:",$x\->as_hex()," dec: ",$x,"\en";
2718 .Ve
2719 .SH "NUMERIC LITERALS"
2720 .IX Header "NUMERIC LITERALS"
2721 After \f(CW\*(C`use Math::BigInt \*(Aq:constant\*(Aq\*(C'\fR all numeric literals in the given scope
2722 are converted to \f(CW\*(C`Math::BigInt\*(C'\fR objects. This conversion happens at compile
2723 time. Every non\-integer is convert to a NaN.
2724 .PP
2725 For example,
2726 .PP
2727 .Vb 1
2728 \& perl \-MMath::BigInt=:constant \-le \*(Aqprint 2**150\*(Aq
2729 .Ve
2730 .PP
2731 prints the exact value of \f(CW\*(C`2**150\*(C'\fR. Note that without conversion of constants
2732 to objects the expression \f(CW\*(C`2**150\*(C'\fR is calculated using Perl scalars, which
2733 leads to an inaccurate result.
2734 .PP
2735 Please note that strings are not affected, so that
2736 .PP
2737 .Vb 1
2738 \& use Math::BigInt qw/:constant/;
2739 \&
2740 \& $x = "1234567890123456789012345678901234567890"
2741 \& + "123456789123456789";
2742 .Ve
2743 .PP
2744 does give you what you expect. You need an explicit Math::BigInt\->\fBnew()\fR around
2745 at least one of the operands. You should also quote large constants to prevent
2746 loss of precision:
2747 .PP
2748 .Vb 1
2749 \& use Math::BigInt;
2750 \&
2751 \& $x = Math::BigInt\->new("1234567889123456789123456789123456789");
2752 .Ve
2753 .PP
2754 Without the quotes Perl first converts the large number to a floating point
2755 constant at compile time, and then converts the result to a Math::BigInt object
2756 at run time, which results in an inaccurate result.
2757 .SS "Hexadecimal, octal, and binary floating point literals"
2758 .IX Subsection "Hexadecimal, octal, and binary floating point literals"
2759 Perl (and this module) accepts hexadecimal, octal, and binary floating point
2760 literals, but use them with care with Perl versions before v5.32.0, because
2761 some versions of Perl silently give the wrong result. Below are some examples
2762 of different ways to write the number decimal 314.
2763 .PP
2764 Hexadecimal floating point literals:
2765 .PP
2766 .Vb 3
2767 \& 0x1.3ap+8 0X1.3AP+8
2768 \& 0x1.3ap8 0X1.3AP8
2769 \& 0x13a0p\-4 0X13A0P\-4
2770 .Ve
2771 .PP
2772 Octal floating point literals (with "0" prefix):
2773 .PP
2774 .Vb 3
2775 \& 01.164p+8 01.164P+8
2776 \& 01.164p8 01.164P8
2777 \& 011640p\-4 011640P\-4
2778 .Ve
2779 .PP
2780 Octal floating point literals (with "0o" prefix) (requires v5.34.0):
2781 .PP
2782 .Vb 3
2783 \& 0o1.164p+8 0O1.164P+8
2784 \& 0o1.164p8 0O1.164P8
2785 \& 0o11640p\-4 0O11640P\-4
2786 .Ve
2787 .PP
2788 Binary floating point literals:
2789 .PP
2790 .Vb 3
2791 \& 0b1.0011101p+8 0B1.0011101P+8
2792 \& 0b1.0011101p8 0B1.0011101P8
2793 \& 0b10011101000p\-2 0B10011101000P\-2
2794 .Ve
2795 .SH PERFORMANCE
2796 .IX Header "PERFORMANCE"
2797 Using the form \f(CW$x\fR += \f(CW$y\fR; etc over \f(CW$x\fR = \f(CW$x\fR + \f(CW$y\fR is faster, since a copy of \f(CW$x\fR
2798 must be made in the second case. For long numbers, the copy can eat up to 20%
2799 of the work (in the case of addition/subtraction, less for
2800 multiplication/division). If \f(CW$y\fR is very small compared to \f(CW$x\fR, the form \f(CW$x\fR += \f(CW$y\fR
2801 is MUCH faster than \f(CW$x\fR = \f(CW$x\fR + \f(CW$y\fR since making the copy of \f(CW$x\fR takes more time
2802 then the actual addition.
2803 .PP
2804 With a technique called copy\-on\-write, the cost of copying with overload could
2805 be minimized or even completely avoided. A test implementation of COW did show
2806 performance gains for overloaded math, but introduced a performance loss due to
2807 a constant overhead for all other operations. So Math::BigInt does currently
2808 not COW.
2809 .PP
2810 The rewritten version of this module (vs. v0.01) is slower on certain
2811 operations, like "\fBnew()\fR", "\fBbstr()\fR" and "\fBnumify()\fR". The reason are that it
2812 does now more work and handles much more cases. The time spent in these
2813 operations is usually gained in the other math operations so that code on the
2814 average should get (much) faster. If they don\*(Aqt, please contact the author.
2815 .PP
2816 Some operations may be slower for small numbers, but are significantly faster
2817 for big numbers. Other operations are now constant (O(1), like "\fBbneg()\fR",
2818 "\fBbabs()\fR" etc), instead of O(N) and thus nearly always take much less time.
2819 These optimizations were done on purpose.
2820 .PP
2821 If you find the Calc module to slow, try to install any of the replacement
2822 modules and see if they help you.
2823 .SS "Alternative math libraries"
2824 .IX Subsection "Alternative math libraries"
2825 You can use an alternative library to drive Math::BigInt. See the section
2826 "Math Library" for more information.
2827 .PP
2828 For more benchmark results see <http://bloodgate.com/perl/benchmarks.html>.
2829 .SH SUBCLASSING
2830 .IX Header "SUBCLASSING"
2831 .SS "Subclassing Math::BigInt"
2832 .IX Subsection "Subclassing Math::BigInt"
2833 The basic design of Math::BigInt allows simple subclasses with very little
2834 work, as long as a few simple rules are followed:
2835 .IP \(bu 4
2836 The public API must remain consistent, i.e. if a sub\-class is overloading
2837 addition, the sub\-class must use the same name, in this case \fBbadd()\fR. The reason
2838 for this is that Math::BigInt is optimized to call the object methods directly.
2839 .IP \(bu 4
2840 The private object hash keys like \f(CW\*(C`$x\->{sign}\*(C'\fR may not be changed, but
2841 additional keys can be added, like \f(CW\*(C`$x\->{_custom}\*(C'\fR.
2842 .IP \(bu 4
2843 Accessor functions are available for all existing object hash keys and should
2844 be used instead of directly accessing the internal hash keys. The reason for
2845 this is that Math::BigInt itself has a pluggable interface which permits it to
2846 support different storage methods.
2847 .PP
2848 More complex sub\-classes may have to replicate more of the logic internal of
2849 Math::BigInt if they need to change more basic behaviors. A subclass that needs
2850 to merely change the output only needs to overload "\fBbstr()\fR".
2851 .PP
2852 All other object methods and overloaded functions can be directly inherited
2853 from the parent class.
2854 .PP
2855 At the very minimum, any subclass needs to provide its own "\fBnew()\fR" and can
2856 store additional hash keys in the object. There are also some package globals
2857 that must be defined, e.g.:
2858 .PP
2859 .Vb 5
2860 \& # Globals
2861 \& our $accuracy = 2; # round to 2 decimal places
2862 \& our $precision = undef;
2863 \& our $round_mode = \*(Aqeven\*(Aq;
2864 \& our $div_scale = 40;
2865 .Ve
2866 .PP
2867 Additionally, you might want to provide the following two globals to allow
2868 auto\-upgrading and auto\-downgrading:
2869 .PP
2870 .Vb 2
2871 \& our $upgrade = undef;
2872 \& our $downgrade = undef;
2873 .Ve
2874 .PP
2875 This allows Math::BigInt to correctly retrieve package globals from the
2876 subclass, like \f(CW$SubClass::precision\fR. See \f(CW\*(C`t/Math/BigInt/Subclass.pm\*(C'\fR,
2877 \&\f(CW\*(C`t/Math/BigFloat/SubClass.pm\*(C'\fR, or \f(CW\*(C`t/Math/BigRat/SubClass.pm\*(C'\fR for subclass
2878 examples.
2879 .PP
2880 Don\*(Aqt forget to
2881 .PP
2882 .Vb 1
2883 \& use overload;
2884 .Ve
2885 .PP
2886 in your subclass to automatically inherit the overloading from the parent. If
2887 you like, you can change part of the overloading, look at Math::String for an
2888 example.
2889 .SH UPGRADING
2890 .IX Header "UPGRADING"
2891 When used like this:
2892 .PP
2893 .Vb 1
2894 \& use Math::BigInt upgrade => \*(AqFoo::Bar\*(Aq;
2895 \&
2896 \& use Math::BigInt;
2897 \& Math::BigInt \-> upgrade(\*(AqFoo::Bar\*(Aq);
2898 .Ve
2899 .PP
2900 any operation whose result cannot be represented as an Math::BigInt object is
2901 upgraded to the class Foo::Bar. Usually this is used in conjunction with
2902 Math::BigRat or Math::BigFloat:
2903 .PP
2904 .Vb 1
2905 \& use Math::BigInt upgrade => \*(AqMath::BigFloat\*(Aq;
2906 .Ve
2907 .PP
2908 For example, the following returns 3 as a Math::BigInt when no upgrading is
2909 defined, and 3.125 as a Math::BigFloat if Math::BigInt is set to upgrade to
2910 Math::BigFloat:
2911 .PP
2912 .Vb 1
2913 \& $x = Math::BigInt \-> new(25) \-> bdiv(8);
2914 .Ve
2915 .PP
2916 As a shortcut, you can use the module bignum:
2917 .PP
2918 .Vb 1
2919 \& use bignum;
2920 .Ve
2921 .PP
2922 which is also good for one\-liners:
2923 .PP
2924 .Vb 1
2925 \& perl \-Mbignum \-le \*(Aqprint 2 ** 255\*(Aq
2926 .Ve
2927 .PP
2928 This makes it possible to mix arguments of different classes (as in 2.5 + 2) as
2929 well as preserve accuracy (as in \fBsqrt\fR\|(3)).
2930 .SS Auto\-upgrade
2931 .IX Subsection "Auto-upgrade"
2932 The following methods upgrade themselves unconditionally; that is if upgrade is
2933 in effect, they always hands up their work:
2934 .PP
2935 .Vb 1
2936 \& bdiv bfdiv btdiv bsqrt blog bexp bpi bsin bcos batan batan2
2937 .Ve
2938 .PP
2939 All other methods upgrade themselves only when one (or all) of their arguments
2940 are of the class mentioned in \f(CW$upgrade\fR.
2941 .SH EXPORTS
2942 .IX Header "EXPORTS"
2943 \&\f(CW\*(C`Math::BigInt\*(C'\fR exports nothing by default, but can export the following
2944 methods:
2945 .PP
2946 .Vb 2
2947 \& bgcd
2948 \& blcm
2949 .Ve
2950 .SH CAVEATS
2951 .IX Header "CAVEATS"
2952 Some things might not work as you expect them. Below is documented what is
2953 known to be troublesome:
2954 .IP "Comparing numbers as strings" 4
2955 .IX Item "Comparing numbers as strings"
2956 Both "\fBbstr()\fR" and "\fBbsstr()\fR" as well as stringify via overload drop the
2957 leading \*(Aq+\*(Aq. This is to be consistent with Perl and to make \f(CW\*(C`cmp\*(C'\fR (especially
2958 with overloading) to work as you expect. It also solves problems with
2959 \&\f(CW\*(C`Test.pm\*(C'\fR and Test::More, which stringify arguments before comparing them.
2960 .Sp
2961 Mark Biggar said, when asked about to drop the \*(Aq+\*(Aq altogether, or make only
2962 \&\f(CW\*(C`cmp\*(C'\fR work:
2963 .Sp
2964 .Vb 4
2965 \& I agree (with the first alternative), don\*(Aqt add the \*(Aq+\*(Aq on positive
2966 \& numbers. It\*(Aqs not as important anymore with the new internal form
2967 \& for numbers. It made doing things like abs and neg easier, but
2968 \& those have to be done differently now anyway.
2969 .Ve
2970 .Sp
2971 So, the following examples now works as expected:
2972 .Sp
2973 .Vb 2
2974 \& use Test::More tests => 1;
2975 \& use Math::BigInt;
2976 \&
2977 \& my $x = Math::BigInt \-> new(3*3);
2978 \& my $y = Math::BigInt \-> new(3*3);
2979 \&
2980 \& is($x,3*3, \*(Aqmultiplication\*(Aq);
2981 \& print "$x eq 9" if $x eq $y;
2982 \& print "$x eq 9" if $x eq \*(Aq9\*(Aq;
2983 \& print "$x eq 9" if $x eq 3*3;
2984 .Ve
2985 .Sp
2986 Additionally, the following still works:
2987 .Sp
2988 .Vb 3
2989 \& print "$x == 9" if $x == $y;
2990 \& print "$x == 9" if $x == 9;
2991 \& print "$x == 9" if $x == 3*3;
2992 .Ve
2993 .Sp
2994 There is now a "\fBbsstr()\fR" method to get the string in scientific notation aka
2995 \&\f(CW1e+2\fR instead of \f(CW100\fR. Be advised that overloaded \*(Aqeq\*(Aq always uses \fBbstr()\fR
2996 for comparison, but Perl represents some numbers as 100 and others as 1e+308.
2997 If in doubt, convert both arguments to Math::BigInt before comparing them as
2998 strings:
2999 .Sp
3000 .Vb 2
3001 \& use Test::More tests => 3;
3002 \& use Math::BigInt;
3003 \&
3004 \& $x = Math::BigInt\->new(\*(Aq1e56\*(Aq);
3005 \& $y = 1e56;
3006 \& is($x,$y); # fails
3007 \& is($x\->bsstr(), $y); # okay
3008 \& $y = Math::BigInt\->new($y);
3009 \& is($x, $y); # okay
3010 .Ve
3011 .Sp
3012 Alternatively, simply use \f(CW\*(C`<=>\*(C'\fR for comparisons, this always gets it
3013 right. There is not yet a way to get a number automatically represented as a
3014 string that matches exactly the way Perl represents it.
3015 .IP \fBoct()\fR/\fBhex()\fR 4
3016 .IX Item "oct()/hex()"
3017 These perl routines currently (as of Perl v.5.8.6) cannot handle passed inf.
3018 .Sp
3019 .Vb 9
3020 \& te@linux:~> perl \-wle \*(Aqprint 2 ** 3333\*(Aq
3021 \& Inf
3022 \& te@linux:~> perl \-wle \*(Aqprint 2 ** 3333 == 2 ** 3333\*(Aq
3023 \& 1
3024 \& te@linux:~> perl \-wle \*(Aqprint oct(2 ** 3333)\*(Aq
3025 \& 0
3026 \& te@linux:~> perl \-wle \*(Aqprint hex(2 ** 3333)\*(Aq
3027 \& Illegal hexadecimal digit \*(AqI\*(Aq ignored at \-e line 1.
3028 \& 0
3029 .Ve
3030 .Sp
3031 The same problems occur if you pass them Math::BigInt\->\fBbinf()\fR objects. Since
3032 overloading these routines is not possible, this cannot be fixed from
3033 Math::BigInt.
3034 .IP \fBint()\fR 4
3035 .IX Item "int()"
3036 "\fBint()\fR" returns (at least for Perl v5.7.1 and up) another Math::BigInt, not a
3037 Perl scalar:
3038 .Sp
3039 .Vb 4
3040 \& $x = Math::BigInt\->new(123);
3041 \& $y = int($x); # 123 as a Math::BigInt
3042 \& $x = Math::BigFloat\->new(123.45);
3043 \& $y = int($x); # 123 as a Math::BigFloat
3044 .Ve
3045 .Sp
3046 If you want a real Perl scalar, use "\fBnumify()\fR":
3047 .Sp
3048 .Vb 1
3049 \& $y = $x\->numify(); # 123 as a scalar
3050 .Ve
3051 .Sp
3052 This is seldom necessary, though, because this is done automatically, like when
3053 you access an array:
3054 .Sp
3055 .Vb 1
3056 \& $z = $array[$x]; # does work automatically
3057 .Ve
3058 .IP "Modifying and =" 4
3059 .IX Item "Modifying and ="
3060 Beware of:
3061 .Sp
3062 .Vb 2
3063 \& $x = Math::BigFloat\->new(5);
3064 \& $y = $x;
3065 .Ve
3066 .Sp
3067 This makes a second reference to the \fBsame\fR object and stores it in \f(CW$y\fR. Thus
3068 anything that modifies \f(CW$x\fR (except overloaded operators) also modifies \f(CW$y\fR, and
3069 vice versa. Or in other words, \f(CW\*(C`=\*(C'\fR is only safe if you modify your
3070 Math::BigInt objects only via overloaded math. As soon as you use a method call
3071 it breaks:
3072 .Sp
3073 .Vb 2
3074 \& $x\->bmul(2);
3075 \& print "$x, $y\en"; # prints \*(Aq10, 10\*(Aq
3076 .Ve
3077 .Sp
3078 If you want a true copy of \f(CW$x\fR, use:
3079 .Sp
3080 .Vb 1
3081 \& $y = $x\->copy();
3082 .Ve
3083 .Sp
3084 You can also chain the calls like this, this first makes a copy and then
3085 multiply it by 2:
3086 .Sp
3087 .Vb 1
3088 \& $y = $x\->copy()\->bmul(2);
3089 .Ve
3090 .Sp
3091 See also the documentation for overload.pm regarding \f(CW\*(C`=\*(C'\fR.
3092 .IP "Overloading \-$x" 4
3093 .IX Item "Overloading -$x"
3094 The following:
3095 .Sp
3096 .Vb 1
3097 \& $x = \-$x;
3098 .Ve
3099 .Sp
3100 is slower than
3101 .Sp
3102 .Vb 1
3103 \& $x\->bneg();
3104 .Ve
3105 .Sp
3106 since overload calls \f(CW\*(C`sub($x,0,1);\*(C'\fR instead of \f(CWneg($x)\fR. The first variant
3107 needs to preserve \f(CW$x\fR since it does not know that it later gets overwritten.
3108 This makes a copy of \f(CW$x\fR and takes O(N), but \f(CW$x\fR\->\fBbneg()\fR is O(1).
3109 .IP "Mixing different object types" 4
3110 .IX Item "Mixing different object types"
3111 With overloaded operators, it is the first (dominating) operand that determines
3112 which method is called. Here are some examples showing what actually gets
3113 called in various cases.
3114 .Sp
3115 .Vb 2
3116 \& use Math::BigInt;
3117 \& use Math::BigFloat;
3118 \&
3119 \& $mbf = Math::BigFloat\->new(5);
3120 \& $mbi2 = Math::BigInt\->new(5);
3121 \& $mbi = Math::BigInt\->new(2);
3122 \& # what actually gets called:
3123 \& $float = $mbf + $mbi; # $mbf\->badd($mbi)
3124 \& $float = $mbf / $mbi; # $mbf\->bdiv($mbi)
3125 \& $integer = $mbi + $mbf; # $mbi\->badd($mbf)
3126 \& $integer = $mbi2 / $mbi; # $mbi2\->bdiv($mbi)
3127 \& $integer = $mbi2 / $mbf; # $mbi2\->bdiv($mbf)
3128 .Ve
3129 .Sp
3130 For instance, Math::BigInt\->\fBbdiv()\fR always returns a Math::BigInt, regardless of
3131 whether the second operant is a Math::BigFloat. To get a Math::BigFloat you
3132 either need to call the operation manually, make sure each operand already is a
3133 Math::BigFloat, or cast to that type via Math::BigFloat\->\fBnew()\fR:
3134 .Sp
3135 .Vb 1
3136 \& $float = Math::BigFloat\->new($mbi2) / $mbi; # = 2.5
3137 .Ve
3138 .Sp
3139 Beware of casting the entire expression, as this would cast the
3140 result, at which point it is too late:
3141 .Sp
3142 .Vb 1
3143 \& $float = Math::BigFloat\->new($mbi2 / $mbi); # = 2
3144 .Ve
3145 .Sp
3146 Beware also of the order of more complicated expressions like:
3147 .Sp
3148 .Vb 2
3149 \& $integer = ($mbi2 + $mbi) / $mbf; # int / float => int
3150 \& $integer = $mbi2 / Math::BigFloat\->new($mbi); # ditto
3151 .Ve
3152 .Sp
3153 If in doubt, break the expression into simpler terms, or cast all operands
3154 to the desired resulting type.
3155 .Sp
3156 Scalar values are a bit different, since:
3157 .Sp
3158 .Vb 2
3159 \& $float = 2 + $mbf;
3160 \& $float = $mbf + 2;
3161 .Ve
3162 .Sp
3163 both result in the proper type due to the way the overloaded math works.
3164 .Sp
3165 This section also applies to other overloaded math packages, like Math::String.
3166 .Sp
3167 One solution to you problem might be autoupgrading|upgrading. See the
3168 pragmas bignum, bigint and bigrat for an easy way to do this.
3169 .SH BUGS
3170 .IX Header "BUGS"
3171 Please report any bugs or feature requests to
3172 \&\f(CW\*(C`bug\-math\-bigint at rt.cpan.org\*(C'\fR, or through the web interface at
3173 <https://rt.cpan.org/Ticket/Create.html?Queue=Math\-BigInt> (requires login).
3174 We will be notified, and then you\*(Aqll automatically be notified of progress on
3175 your bug as I make changes.
3176 .SH SUPPORT
3177 .IX Header "SUPPORT"
3178 You can find documentation for this module with the perldoc command.
3179 .PP
3180 .Vb 1
3181 \& perldoc Math::BigInt
3182 .Ve
3183 .PP
3184 You can also look for information at:
3185 .IP \(bu 4
3186 GitHub
3187 .Sp
3188 <https://github.com/pjacklam/p5\-Math\-BigInt>
3189 .IP \(bu 4
3190 RT: CPAN\*(Aqs request tracker
3191 .Sp
3192 <https://rt.cpan.org/Dist/Display.html?Name=Math\-BigInt>
3193 .IP \(bu 4
3194 MetaCPAN
3195 .Sp
3196 <https://metacpan.org/release/Math\-BigInt>
3197 .IP \(bu 4
3198 CPAN Testers Matrix
3199 .Sp
3200 <http://matrix.cpantesters.org/?dist=Math\-BigInt>
3201 .SH LICENSE
3202 .IX Header "LICENSE"
3203 This program is free software; you may redistribute it and/or modify it under
3204 the same terms as Perl itself.
3205 .SH "SEE ALSO"
3206 .IX Header "SEE ALSO"
3207 Math::BigFloat and Math::BigRat as well as the backend libraries
3208 Math::BigInt::FastCalc, Math::BigInt::GMP, and Math::BigInt::Pari,
3209 Math::BigInt::GMPz, and Math::BigInt::BitVect.
3210 .PP
3211 The pragmas bigint, bigfloat, and bigrat might also be of interest. In
3212 addition there is the bignum pragma which does upgrading and downgrading.
3213 .SH AUTHORS
3214 .IX Header "AUTHORS"
3215 .IP \(bu 4
3216 Mark Biggar, overloaded interface by Ilya Zakharevich, 1996\-2001.
3217 .IP \(bu 4
3218 Completely rewritten by Tels <http://bloodgate.com>, 2001\-2008.
3219 .IP \(bu 4
3220 Florian Ragwitz <[email protected]>, 2010.
3221 .IP \(bu 4
3222 Peter John Acklam <[email protected]>, 2011\-.
3223 .PP
3224 Many people contributed in one or more ways to the final beast, see the file
3225 CREDITS for an (incomplete) list. If you miss your name, please drop me a
3226 mail. Thank you!