Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "Math::BigInt 3" | |
132 | .TH Math::BigInt 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | Math::BigInt \- Arbitrary size integer/float math package | |
135 | .SH "SYNOPSIS" | |
136 | .IX Header "SYNOPSIS" | |
137 | .Vb 1 | |
138 | \& use Math::BigInt; | |
139 | .Ve | |
140 | .PP | |
141 | .Vb 3 | |
142 | \& # or make it faster: install (optional) Math::BigInt::GMP | |
143 | \& # and always use (it will fall back to pure Perl if the | |
144 | \& # GMP library is not installed): | |
145 | .Ve | |
146 | .PP | |
147 | .Vb 1 | |
148 | \& use Math::BigInt lib => 'GMP'; | |
149 | .Ve | |
150 | .PP | |
151 | .Vb 3 | |
152 | \& my $str = '1234567890'; | |
153 | \& my @values = (64,74,18); | |
154 | \& my $n = 1; my $sign = '-'; | |
155 | .Ve | |
156 | .PP | |
157 | .Vb 9 | |
158 | \& # Number creation | |
159 | \& $x = Math::BigInt->new($str); # defaults to 0 | |
160 | \& $y = $x->copy(); # make a true copy | |
161 | \& $nan = Math::BigInt->bnan(); # create a NotANumber | |
162 | \& $zero = Math::BigInt->bzero(); # create a +0 | |
163 | \& $inf = Math::BigInt->binf(); # create a +inf | |
164 | \& $inf = Math::BigInt->binf('-'); # create a -inf | |
165 | \& $one = Math::BigInt->bone(); # create a +1 | |
166 | \& $one = Math::BigInt->bone('-'); # create a -1 | |
167 | .Ve | |
168 | .PP | |
169 | .Vb 2 | |
170 | \& # Testing (don't modify their arguments) | |
171 | \& # (return true if the condition is met, otherwise false) | |
172 | .Ve | |
173 | .PP | |
174 | .Vb 10 | |
175 | \& $x->is_zero(); # if $x is +0 | |
176 | \& $x->is_nan(); # if $x is NaN | |
177 | \& $x->is_one(); # if $x is +1 | |
178 | \& $x->is_one('-'); # if $x is -1 | |
179 | \& $x->is_odd(); # if $x is odd | |
180 | \& $x->is_even(); # if $x is even | |
181 | \& $x->is_pos(); # if $x >= 0 | |
182 | \& $x->is_neg(); # if $x < 0 | |
183 | \& $x->is_inf($sign); # if $x is +inf, or -inf (sign is default '+') | |
184 | \& $x->is_int(); # if $x is an integer (not a float) | |
185 | .Ve | |
186 | .PP | |
187 | .Vb 6 | |
188 | \& # comparing and digit/sign extration | |
189 | \& $x->bcmp($y); # compare numbers (undef,<0,=0,>0) | |
190 | \& $x->bacmp($y); # compare absolutely (undef,<0,=0,>0) | |
191 | \& $x->sign(); # return the sign, either +,- or NaN | |
192 | \& $x->digit($n); # return the nth digit, counting from right | |
193 | \& $x->digit(-$n); # return the nth digit, counting from left | |
194 | .Ve | |
195 | .PP | |
196 | .Vb 3 | |
197 | \& # The following all modify their first argument. If you want to preserve | |
198 | \& # $x, use $z = $x->copy()->bXXX($y); See under L<CAVEATS> for why this is | |
199 | \& # neccessary when mixing $a = $b assigments with non-overloaded math. | |
200 | .Ve | |
201 | .PP | |
202 | .Vb 6 | |
203 | \& $x->bzero(); # set $x to 0 | |
204 | \& $x->bnan(); # set $x to NaN | |
205 | \& $x->bone(); # set $x to +1 | |
206 | \& $x->bone('-'); # set $x to -1 | |
207 | \& $x->binf(); # set $x to inf | |
208 | \& $x->binf('-'); # set $x to -inf | |
209 | .Ve | |
210 | .PP | |
211 | .Vb 6 | |
212 | \& $x->bneg(); # negation | |
213 | \& $x->babs(); # absolute value | |
214 | \& $x->bnorm(); # normalize (no-op in BigInt) | |
215 | \& $x->bnot(); # two's complement (bit wise not) | |
216 | \& $x->binc(); # increment $x by 1 | |
217 | \& $x->bdec(); # decrement $x by 1 | |
218 | .Ve | |
219 | .PP | |
220 | .Vb 5 | |
221 | \& $x->badd($y); # addition (add $y to $x) | |
222 | \& $x->bsub($y); # subtraction (subtract $y from $x) | |
223 | \& $x->bmul($y); # multiplication (multiply $x by $y) | |
224 | \& $x->bdiv($y); # divide, set $x to quotient | |
225 | \& # return (quo,rem) or quo if scalar | |
226 | .Ve | |
227 | .PP | |
228 | .Vb 3 | |
229 | \& $x->bmod($y); # modulus (x % y) | |
230 | \& $x->bmodpow($exp,$mod); # modular exponentation (($num**$exp) % $mod)) | |
231 | \& $x->bmodinv($mod); # the inverse of $x in the given modulus $mod | |
232 | .Ve | |
233 | .PP | |
234 | .Vb 5 | |
235 | \& $x->bpow($y); # power of arguments (x ** y) | |
236 | \& $x->blsft($y); # left shift | |
237 | \& $x->brsft($y); # right shift | |
238 | \& $x->blsft($y,$n); # left shift, by base $n (like 10) | |
239 | \& $x->brsft($y,$n); # right shift, by base $n (like 10) | |
240 | .Ve | |
241 | .PP | |
242 | .Vb 4 | |
243 | \& $x->band($y); # bitwise and | |
244 | \& $x->bior($y); # bitwise inclusive or | |
245 | \& $x->bxor($y); # bitwise exclusive or | |
246 | \& $x->bnot(); # bitwise not (two's complement) | |
247 | .Ve | |
248 | .PP | |
249 | .Vb 3 | |
250 | \& $x->bsqrt(); # calculate square-root | |
251 | \& $x->broot($y); # $y'th root of $x (e.g. $y == 3 => cubic root) | |
252 | \& $x->bfac(); # factorial of $x (1*2*3*4*..$x) | |
253 | .Ve | |
254 | .PP | |
255 | .Vb 3 | |
256 | \& $x->round($A,$P,$mode); # round to accuracy or precision using mode $mode | |
257 | \& $x->bround($n); # accuracy: preserve $n digits | |
258 | \& $x->bfround($n); # round to $nth digit, no-op for BigInts | |
259 | .Ve | |
260 | .PP | |
261 | .Vb 2 | |
262 | \& # The following do not modify their arguments in BigInt (are no-ops), | |
263 | \& # but do so in BigFloat: | |
264 | .Ve | |
265 | .PP | |
266 | .Vb 2 | |
267 | \& $x->bfloor(); # return integer less or equal than $x | |
268 | \& $x->bceil(); # return integer greater or equal than $x | |
269 | .Ve | |
270 | .PP | |
271 | .Vb 1 | |
272 | \& # The following do not modify their arguments: | |
273 | .Ve | |
274 | .PP | |
275 | .Vb 4 | |
276 | \& # greatest common divisor (no OO style) | |
277 | \& my $gcd = Math::BigInt::bgcd(@values); | |
278 | \& # lowest common multiplicator (no OO style) | |
279 | \& my $lcm = Math::BigInt::blcm(@values); | |
280 | .Ve | |
281 | .PP | |
282 | .Vb 3 | |
283 | \& $x->length(); # return number of digits in number | |
284 | \& ($xl,$f) = $x->length(); # length of number and length of fraction part, | |
285 | \& # latter is always 0 digits long for BigInts | |
286 | .Ve | |
287 | .PP | |
288 | .Vb 6 | |
289 | \& $x->exponent(); # return exponent as BigInt | |
290 | \& $x->mantissa(); # return (signed) mantissa as BigInt | |
291 | \& $x->parts(); # return (mantissa,exponent) as BigInt | |
292 | \& $x->copy(); # make a true copy of $x (unlike $y = $x;) | |
293 | \& $x->as_int(); # return as BigInt (in BigInt: same as copy()) | |
294 | \& $x->numify(); # return as scalar (might overflow!) | |
295 | .Ve | |
296 | .PP | |
297 | .Vb 5 | |
298 | \& # conversation to string (do not modify their argument) | |
299 | \& $x->bstr(); # normalized string (e.g. '3') | |
300 | \& $x->bsstr(); # norm. string in scientific notation (e.g. '3E0') | |
301 | \& $x->as_hex(); # as signed hexadecimal string with prefixed 0x | |
302 | \& $x->as_bin(); # as signed binary string with prefixed 0b | |
303 | .Ve | |
304 | .PP | |
305 | .Vb 5 | |
306 | \& # precision and accuracy (see section about rounding for more) | |
307 | \& $x->precision(); # return P of $x (or global, if P of $x undef) | |
308 | \& $x->precision($n); # set P of $x to $n | |
309 | \& $x->accuracy(); # return A of $x (or global, if A of $x undef) | |
310 | \& $x->accuracy($n); # set A $x to $n | |
311 | .Ve | |
312 | .PP | |
313 | .Vb 6 | |
314 | \& # Global methods | |
315 | \& Math::BigInt->precision(); # get/set global P for all BigInt objects | |
316 | \& Math::BigInt->accuracy(); # get/set global A for all BigInt objects | |
317 | \& Math::BigInt->round_mode(); # get/set global round mode, one of | |
318 | \& # 'even', 'odd', '+inf', '-inf', 'zero' or 'trunc' | |
319 | \& Math::BigInt->config(); # return hash containing configuration | |
320 | .Ve | |
321 | .SH "DESCRIPTION" | |
322 | .IX Header "DESCRIPTION" | |
323 | All operators (inlcuding basic math operations) are overloaded if you | |
324 | declare your big integers as | |
325 | .PP | |
326 | .Vb 1 | |
327 | \& $i = new Math::BigInt '123_456_789_123_456_789'; | |
328 | .Ve | |
329 | .PP | |
330 | Operations with overloaded operators preserve the arguments which is | |
331 | exactly what you expect. | |
332 | .IP "Input" 2 | |
333 | .IX Item "Input" | |
334 | Input values to these routines may be any string, that looks like a number | |
335 | and results in an integer, including hexadecimal and binary numbers. | |
336 | .Sp | |
337 | Scalars holding numbers may also be passed, but note that non-integer numbers | |
338 | may already have lost precision due to the conversation to float. Quote | |
339 | your input if you want BigInt to see all the digits: | |
340 | .Sp | |
341 | .Vb 2 | |
342 | \& $x = Math::BigInt->new(12345678890123456789); # bad | |
343 | \& $x = Math::BigInt->new('12345678901234567890'); # good | |
344 | .Ve | |
345 | .Sp | |
346 | You can include one underscore between any two digits. | |
347 | .Sp | |
348 | This means integer values like 1.01E2 or even 1000E\-2 are also accepted. | |
349 | Non-integer values result in NaN. | |
350 | .Sp | |
351 | Currently, \fIMath::BigInt::new()\fR defaults to 0, while Math::BigInt::new('') | |
352 | results in 'NaN'. This might change in the future, so use always the following | |
353 | explicit forms to get a zero or NaN: | |
354 | .Sp | |
355 | .Vb 2 | |
356 | \& $zero = Math::BigInt->bzero(); | |
357 | \& $nan = Math::BigInt->bnan(); | |
358 | .Ve | |
359 | .Sp | |
360 | \&\f(CW\*(C`bnorm()\*(C'\fR on a BigInt object is now effectively a no\-op, since the numbers | |
361 | are always stored in normalized form. If passed a string, creates a BigInt | |
362 | object from the input. | |
363 | .IP "Output" 2 | |
364 | .IX Item "Output" | |
365 | Output values are BigInt objects (normalized), except for the methods which | |
366 | return a string (see \s-1SYNOPSIS\s0). | |
367 | .Sp | |
368 | Some routines (\f(CW\*(C`is_odd()\*(C'\fR, \f(CW\*(C`is_even()\*(C'\fR, \f(CW\*(C`is_zero()\*(C'\fR, \f(CW\*(C`is_one()\*(C'\fR, | |
369 | \&\f(CW\*(C`is_nan()\*(C'\fR, etc.) return true or false, while others (\f(CW\*(C`bcmp()\*(C'\fR, \f(CW\*(C`bacmp()\*(C'\fR) | |
370 | return either undef (if NaN is involved), <0, 0 or >0 and are suited for sort. | |
371 | .SH "METHODS" | |
372 | .IX Header "METHODS" | |
373 | Each of the methods below (except \fIconfig()\fR, \fIaccuracy()\fR and \fIprecision()\fR) | |
374 | accepts three additional parameters. These arguments \f(CW$A\fR, \f(CW$P\fR and \f(CW$R\fR | |
375 | are \f(CW\*(C`accuracy\*(C'\fR, \f(CW\*(C`precision\*(C'\fR and \f(CW\*(C`round_mode\*(C'\fR. Please see the section about | |
376 | \&\*(L"\s-1ACCURACY\s0 and \s-1PRECISION\s0\*(R" for more information. | |
377 | .Sh "config" | |
378 | .IX Subsection "config" | |
379 | .Vb 1 | |
380 | \& use Data::Dumper; | |
381 | .Ve | |
382 | .PP | |
383 | .Vb 2 | |
384 | \& print Dumper ( Math::BigInt->config() ); | |
385 | \& print Math::BigInt->config()->{lib},"\en"; | |
386 | .Ve | |
387 | .PP | |
388 | Returns a hash containing the configuration, e.g. the version number, lib | |
389 | loaded etc. The following hash keys are currently filled in with the | |
390 | appropriate information. | |
391 | .PP | |
392 | .Vb 27 | |
393 | \& key Description | |
394 | \& Example | |
395 | \& ============================================================ | |
396 | \& lib Name of the low-level math library | |
397 | \& Math::BigInt::Calc | |
398 | \& lib_version Version of low-level math library (see 'lib') | |
399 | \& 0.30 | |
400 | \& class The class name of config() you just called | |
401 | \& Math::BigInt | |
402 | \& upgrade To which class math operations might be upgraded | |
403 | \& Math::BigFloat | |
404 | \& downgrade To which class math operations might be downgraded | |
405 | \& undef | |
406 | \& precision Global precision | |
407 | \& undef | |
408 | \& accuracy Global accuracy | |
409 | \& undef | |
410 | \& round_mode Global round mode | |
411 | \& even | |
412 | \& version version number of the class you used | |
413 | \& 1.61 | |
414 | \& div_scale Fallback acccuracy for div | |
415 | \& 40 | |
416 | \& trap_nan If true, traps creation of NaN via croak() | |
417 | \& 1 | |
418 | \& trap_inf If true, traps creation of +inf/-inf via croak() | |
419 | \& 1 | |
420 | .Ve | |
421 | .PP | |
422 | The following values can be set by passing \f(CW\*(C`config()\*(C'\fR a reference to a hash: | |
423 | .PP | |
424 | .Vb 2 | |
425 | \& trap_inf trap_nan | |
426 | \& upgrade downgrade precision accuracy round_mode div_scale | |
427 | .Ve | |
428 | .PP | |
429 | Example: | |
430 | .PP | |
431 | .Vb 1 | |
432 | \& $new_cfg = Math::BigInt->config( { trap_inf => 1, precision => 5 } ); | |
433 | .Ve | |
434 | .Sh "accuracy" | |
435 | .IX Subsection "accuracy" | |
436 | .Vb 3 | |
437 | \& $x->accuracy(5); # local for $x | |
438 | \& CLASS->accuracy(5); # global for all members of CLASS | |
439 | \& # Note: This also applies to new()! | |
440 | .Ve | |
441 | .PP | |
442 | .Vb 2 | |
443 | \& $A = $x->accuracy(); # read out accuracy that affects $x | |
444 | \& $A = CLASS->accuracy(); # read out global accuracy | |
445 | .Ve | |
446 | .PP | |
447 | Set or get the global or local accuracy, aka how many significant digits the | |
448 | results have. If you set a global accuracy, then this also applies to \fInew()\fR! | |
449 | .PP | |
450 | Warning! The accuracy \fIsticks\fR, e.g. once you created a number under the | |
451 | influence of \f(CW\*(C`CLASS\->accuracy($A)\*(C'\fR, all results from math operations with | |
452 | that number will also be rounded. | |
453 | .PP | |
454 | In most cases, you should probably round the results explicitely using one of | |
455 | \&\fIround()\fR, \fIbround()\fR or \fIbfround()\fR or by passing the desired accuracy | |
456 | to the math operation as additional parameter: | |
457 | .PP | |
458 | .Vb 4 | |
459 | \& my $x = Math::BigInt->new(30000); | |
460 | \& my $y = Math::BigInt->new(7); | |
461 | \& print scalar $x->copy()->bdiv($y, 2); # print 4300 | |
462 | \& print scalar $x->copy()->bdiv($y)->bround(2); # print 4300 | |
463 | .Ve | |
464 | .PP | |
465 | Please see the section about \*(L"\s-1ACCURACY\s0 \s-1AND\s0 \s-1PRECISION\s0\*(R" for further details. | |
466 | .PP | |
467 | Value must be greater than zero. Pass an undef value to disable it: | |
468 | .PP | |
469 | .Vb 2 | |
470 | \& $x->accuracy(undef); | |
471 | \& Math::BigInt->accuracy(undef); | |
472 | .Ve | |
473 | .PP | |
474 | Returns the current accuracy. For \f(CW\*(C`$x\-\*(C'\fR\fIaccuracy()\fR> it will return either the | |
475 | local accuracy, or if not defined, the global. This means the return value | |
476 | represents the accuracy that will be in effect for \f(CW$x:\fR | |
477 | .PP | |
478 | .Vb 9 | |
479 | \& $y = Math::BigInt->new(1234567); # unrounded | |
480 | \& print Math::BigInt->accuracy(4),"\en"; # set 4, print 4 | |
481 | \& $x = Math::BigInt->new(123456); # $x will be automatically rounded! | |
482 | \& print "$x $y\en"; # '123500 1234567' | |
483 | \& print $x->accuracy(),"\en"; # will be 4 | |
484 | \& print $y->accuracy(),"\en"; # also 4, since global is 4 | |
485 | \& print Math::BigInt->accuracy(5),"\en"; # set to 5, print 5 | |
486 | \& print $x->accuracy(),"\en"; # still 4 | |
487 | \& print $y->accuracy(),"\en"; # 5, since global is 5 | |
488 | .Ve | |
489 | .PP | |
490 | Note: Works also for subclasses like Math::BigFloat. Each class has it's own | |
491 | globals separated from Math::BigInt, but it is possible to subclass | |
492 | Math::BigInt and make the globals of the subclass aliases to the ones from | |
493 | Math::BigInt. | |
494 | .Sh "precision" | |
495 | .IX Subsection "precision" | |
496 | .Vb 2 | |
497 | \& $x->precision(-2); # local for $x, round at the second digit right of the dot | |
498 | \& $x->precision(2); # ditto, round at the second digit left of the dot | |
499 | .Ve | |
500 | .PP | |
501 | .Vb 3 | |
502 | \& CLASS->precision(5); # Global for all members of CLASS | |
503 | \& # This also applies to new()! | |
504 | \& CLASS->precision(-5); # ditto | |
505 | .Ve | |
506 | .PP | |
507 | .Vb 2 | |
508 | \& $P = CLASS->precision(); # read out global precision | |
509 | \& $P = $x->precision(); # read out precision that affects $x | |
510 | .Ve | |
511 | .PP | |
512 | Note: You probably want to use \fIaccuracy()\fR instead. With accuracy you | |
513 | set the number of digits each result should have, with precision you | |
514 | set the place where to round! | |
515 | .PP | |
516 | \&\f(CW\*(C`precision()\*(C'\fR sets or gets the global or local precision, aka at which digit | |
517 | before or after the dot to round all results. A set global precision also | |
518 | applies to all newly created numbers! | |
519 | .PP | |
520 | In Math::BigInt, passing a negative number precision has no effect since no | |
521 | numbers have digits after the dot. In Math::BigFloat, it will round all | |
522 | results to P digits after the dot. | |
523 | .PP | |
524 | Please see the section about \*(L"\s-1ACCURACY\s0 \s-1AND\s0 \s-1PRECISION\s0\*(R" for further details. | |
525 | .PP | |
526 | Pass an undef value to disable it: | |
527 | .PP | |
528 | .Vb 2 | |
529 | \& $x->precision(undef); | |
530 | \& Math::BigInt->precision(undef); | |
531 | .Ve | |
532 | .PP | |
533 | Returns the current precision. For \f(CW\*(C`$x\-\*(C'\fR\fIprecision()\fR> it will return either the | |
534 | local precision of \f(CW$x\fR, or if not defined, the global. This means the return | |
535 | value represents the prevision that will be in effect for \f(CW$x:\fR | |
536 | .PP | |
537 | .Vb 4 | |
538 | \& $y = Math::BigInt->new(1234567); # unrounded | |
539 | \& print Math::BigInt->precision(4),"\en"; # set 4, print 4 | |
540 | \& $x = Math::BigInt->new(123456); # will be automatically rounded | |
541 | \& print $x; # print "120000"! | |
542 | .Ve | |
543 | .PP | |
544 | Note: Works also for subclasses like Math::BigFloat. Each class has its | |
545 | own globals separated from Math::BigInt, but it is possible to subclass | |
546 | Math::BigInt and make the globals of the subclass aliases to the ones from | |
547 | Math::BigInt. | |
548 | .Sh "brsft" | |
549 | .IX Subsection "brsft" | |
550 | .Vb 1 | |
551 | \& $x->brsft($y,$n); | |
552 | .Ve | |
553 | .PP | |
554 | Shifts \f(CW$x\fR right by \f(CW$y\fR in base \f(CW$n\fR. Default is base 2, used are usually 10 and | |
555 | 2, but others work, too. | |
556 | .PP | |
557 | Right shifting usually amounts to dividing \f(CW$x\fR by \f(CW$n\fR ** \f(CW$y\fR and truncating the | |
558 | result: | |
559 | .PP | |
560 | .Vb 4 | |
561 | \& $x = Math::BigInt->new(10); | |
562 | \& $x->brsft(1); # same as $x >> 1: 5 | |
563 | \& $x = Math::BigInt->new(1234); | |
564 | \& $x->brsft(2,10); # result 12 | |
565 | .Ve | |
566 | .PP | |
567 | There is one exception, and that is base 2 with negative \f(CW$x:\fR | |
568 | .PP | |
569 | .Vb 2 | |
570 | \& $x = Math::BigInt->new(-5); | |
571 | \& print $x->brsft(1); | |
572 | .Ve | |
573 | .PP | |
574 | This will print \-3, not \-2 (as it would if you divide \-5 by 2 and truncate the | |
575 | result). | |
576 | .Sh "new" | |
577 | .IX Subsection "new" | |
578 | .Vb 1 | |
579 | \& $x = Math::BigInt->new($str,$A,$P,$R); | |
580 | .Ve | |
581 | .PP | |
582 | Creates a new BigInt object from a scalar or another BigInt object. The | |
583 | input is accepted as decimal, hex (with leading '0x') or binary (with leading | |
584 | \&'0b'). | |
585 | .PP | |
586 | See Input for more info on accepted input formats. | |
587 | .Sh "bnan" | |
588 | .IX Subsection "bnan" | |
589 | .Vb 1 | |
590 | \& $x = Math::BigInt->bnan(); | |
591 | .Ve | |
592 | .PP | |
593 | Creates a new BigInt object representing NaN (Not A Number). | |
594 | If used on an object, it will set it to NaN: | |
595 | .PP | |
596 | .Vb 1 | |
597 | \& $x->bnan(); | |
598 | .Ve | |
599 | .Sh "bzero" | |
600 | .IX Subsection "bzero" | |
601 | .Vb 1 | |
602 | \& $x = Math::BigInt->bzero(); | |
603 | .Ve | |
604 | .PP | |
605 | Creates a new BigInt object representing zero. | |
606 | If used on an object, it will set it to zero: | |
607 | .PP | |
608 | .Vb 1 | |
609 | \& $x->bzero(); | |
610 | .Ve | |
611 | .Sh "binf" | |
612 | .IX Subsection "binf" | |
613 | .Vb 1 | |
614 | \& $x = Math::BigInt->binf($sign); | |
615 | .Ve | |
616 | .PP | |
617 | Creates a new BigInt object representing infinity. The optional argument is | |
618 | either '\-' or '+', indicating whether you want infinity or minus infinity. | |
619 | If used on an object, it will set it to infinity: | |
620 | .PP | |
621 | .Vb 2 | |
622 | \& $x->binf(); | |
623 | \& $x->binf('-'); | |
624 | .Ve | |
625 | .Sh "bone" | |
626 | .IX Subsection "bone" | |
627 | .Vb 1 | |
628 | \& $x = Math::BigInt->binf($sign); | |
629 | .Ve | |
630 | .PP | |
631 | Creates a new BigInt object representing one. The optional argument is | |
632 | either '\-' or '+', indicating whether you want one or minus one. | |
633 | If used on an object, it will set it to one: | |
634 | .PP | |
635 | .Vb 2 | |
636 | \& $x->bone(); # +1 | |
637 | \& $x->bone('-'); # -1 | |
638 | .Ve | |
639 | .Sh "\fIis_one()\fP/\fIis_zero()\fP/\fIis_nan()\fP/\fIis_inf()\fP" | |
640 | .IX Subsection "is_one()/is_zero()/is_nan()/is_inf()" | |
641 | .Vb 6 | |
642 | \& $x->is_zero(); # true if arg is +0 | |
643 | \& $x->is_nan(); # true if arg is NaN | |
644 | \& $x->is_one(); # true if arg is +1 | |
645 | \& $x->is_one('-'); # true if arg is -1 | |
646 | \& $x->is_inf(); # true if +inf | |
647 | \& $x->is_inf('-'); # true if -inf (sign is default '+') | |
648 | .Ve | |
649 | .PP | |
650 | These methods all test the BigInt for beeing one specific value and return | |
651 | true or false depending on the input. These are faster than doing something | |
652 | like: | |
653 | .PP | |
654 | .Vb 1 | |
655 | \& if ($x == 0) | |
656 | .Ve | |
657 | .Sh "\fIis_pos()\fP/\fIis_neg()\fP" | |
658 | .IX Subsection "is_pos()/is_neg()" | |
659 | .Vb 2 | |
660 | \& $x->is_pos(); # true if > 0 | |
661 | \& $x->is_neg(); # true if < 0 | |
662 | .Ve | |
663 | .PP | |
664 | The methods return true if the argument is positive or negative, respectively. | |
665 | \&\f(CW\*(C`NaN\*(C'\fR is neither positive nor negative, while \f(CW\*(C`+inf\*(C'\fR counts as positive, and | |
666 | \&\f(CW\*(C`\-inf\*(C'\fR is negative. A \f(CW\*(C`zero\*(C'\fR is neither positive nor negative. | |
667 | .PP | |
668 | These methods are only testing the sign, and not the value. | |
669 | .PP | |
670 | \&\f(CW\*(C`is_positive()\*(C'\fR and \f(CW\*(C`is_negative()\*(C'\fR are aliase to \f(CW\*(C`is_pos()\*(C'\fR and | |
671 | \&\f(CW\*(C`is_neg()\*(C'\fR, respectively. \f(CW\*(C`is_positive()\*(C'\fR and \f(CW\*(C`is_negative()\*(C'\fR were | |
672 | introduced in v1.36, while \f(CW\*(C`is_pos()\*(C'\fR and \f(CW\*(C`is_neg()\*(C'\fR were only introduced | |
673 | in v1.68. | |
674 | .Sh "\fIis_odd()\fP/\fIis_even()\fP/\fIis_int()\fP" | |
675 | .IX Subsection "is_odd()/is_even()/is_int()" | |
676 | .Vb 3 | |
677 | \& $x->is_odd(); # true if odd, false for even | |
678 | \& $x->is_even(); # true if even, false for odd | |
679 | \& $x->is_int(); # true if $x is an integer | |
680 | .Ve | |
681 | .PP | |
682 | The return true when the argument satisfies the condition. \f(CW\*(C`NaN\*(C'\fR, \f(CW\*(C`+inf\*(C'\fR, | |
683 | \&\f(CW\*(C`\-inf\*(C'\fR are not integers and are neither odd nor even. | |
684 | .PP | |
685 | In BigInt, all numbers except \f(CW\*(C`NaN\*(C'\fR, \f(CW\*(C`+inf\*(C'\fR and \f(CW\*(C`\-inf\*(C'\fR are integers. | |
686 | .Sh "bcmp" | |
687 | .IX Subsection "bcmp" | |
688 | .Vb 1 | |
689 | \& $x->bcmp($y); | |
690 | .Ve | |
691 | .PP | |
692 | Compares \f(CW$x\fR with \f(CW$y\fR and takes the sign into account. | |
693 | Returns \-1, 0, 1 or undef. | |
694 | .Sh "bacmp" | |
695 | .IX Subsection "bacmp" | |
696 | .Vb 1 | |
697 | \& $x->bacmp($y); | |
698 | .Ve | |
699 | .PP | |
700 | Compares \f(CW$x\fR with \f(CW$y\fR while ignoring their. Returns \-1, 0, 1 or undef. | |
701 | .Sh "sign" | |
702 | .IX Subsection "sign" | |
703 | .Vb 1 | |
704 | \& $x->sign(); | |
705 | .Ve | |
706 | .PP | |
707 | 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. | |
708 | .PP | |
709 | If you want \f(CW$x\fR to have a certain sign, use one of the following methods: | |
710 | .PP | |
711 | .Vb 5 | |
712 | \& $x->babs(); # '+' | |
713 | \& $x->babs()->bneg(); # '-' | |
714 | \& $x->bnan(); # 'NaN' | |
715 | \& $x->binf(); # '+inf' | |
716 | \& $x->binf('-'); # '-inf' | |
717 | .Ve | |
718 | .Sh "digit" | |
719 | .IX Subsection "digit" | |
720 | .Vb 1 | |
721 | \& $x->digit($n); # return the nth digit, counting from right | |
722 | .Ve | |
723 | .PP | |
724 | If \f(CW$n\fR is negative, returns the digit counting from left. | |
725 | .Sh "bneg" | |
726 | .IX Subsection "bneg" | |
727 | .Vb 1 | |
728 | \& $x->bneg(); | |
729 | .Ve | |
730 | .PP | |
731 | Negate the number, e.g. change the sign between '+' and '\-', or between '+inf' | |
732 | and '\-inf', respectively. Does nothing for NaN or zero. | |
733 | .Sh "babs" | |
734 | .IX Subsection "babs" | |
735 | .Vb 1 | |
736 | \& $x->babs(); | |
737 | .Ve | |
738 | .PP | |
739 | Set the number to it's absolute value, e.g. change the sign from '\-' to '+' | |
740 | and from '\-inf' to '+inf', respectively. Does nothing for NaN or positive | |
741 | numbers. | |
742 | .Sh "bnorm" | |
743 | .IX Subsection "bnorm" | |
744 | .Vb 1 | |
745 | \& $x->bnorm(); # normalize (no-op) | |
746 | .Ve | |
747 | .Sh "bnot" | |
748 | .IX Subsection "bnot" | |
749 | .Vb 1 | |
750 | \& $x->bnot(); | |
751 | .Ve | |
752 | .PP | |
753 | Two's complement (bit wise not). This is equivalent to | |
754 | .PP | |
755 | .Vb 1 | |
756 | \& $x->binc()->bneg(); | |
757 | .Ve | |
758 | .PP | |
759 | but faster. | |
760 | .Sh "binc" | |
761 | .IX Subsection "binc" | |
762 | .Vb 1 | |
763 | \& $x->binc(); # increment x by 1 | |
764 | .Ve | |
765 | .Sh "bdec" | |
766 | .IX Subsection "bdec" | |
767 | .Vb 1 | |
768 | \& $x->bdec(); # decrement x by 1 | |
769 | .Ve | |
770 | .Sh "badd" | |
771 | .IX Subsection "badd" | |
772 | .Vb 1 | |
773 | \& $x->badd($y); # addition (add $y to $x) | |
774 | .Ve | |
775 | .Sh "bsub" | |
776 | .IX Subsection "bsub" | |
777 | .Vb 1 | |
778 | \& $x->bsub($y); # subtraction (subtract $y from $x) | |
779 | .Ve | |
780 | .Sh "bmul" | |
781 | .IX Subsection "bmul" | |
782 | .Vb 1 | |
783 | \& $x->bmul($y); # multiplication (multiply $x by $y) | |
784 | .Ve | |
785 | .Sh "bdiv" | |
786 | .IX Subsection "bdiv" | |
787 | .Vb 2 | |
788 | \& $x->bdiv($y); # divide, set $x to quotient | |
789 | \& # return (quo,rem) or quo if scalar | |
790 | .Ve | |
791 | .Sh "bmod" | |
792 | .IX Subsection "bmod" | |
793 | .Vb 1 | |
794 | \& $x->bmod($y); # modulus (x % y) | |
795 | .Ve | |
796 | .Sh "bmodinv" | |
797 | .IX Subsection "bmodinv" | |
798 | .Vb 1 | |
799 | \& num->bmodinv($mod); # modular inverse | |
800 | .Ve | |
801 | .PP | |
802 | Returns the inverse of \f(CW$num\fR in the given modulus \f(CW$mod\fR. '\f(CW\*(C`NaN\*(C'\fR' is | |
803 | returned unless \f(CW$num\fR is relatively prime to \f(CW$mod\fR, i.e. unless | |
804 | \&\f(CW\*(C`bgcd($num, $mod)==1\*(C'\fR. | |
805 | .Sh "bmodpow" | |
806 | .IX Subsection "bmodpow" | |
807 | .Vb 2 | |
808 | \& $num->bmodpow($exp,$mod); # modular exponentation | |
809 | \& # ($num**$exp % $mod) | |
810 | .Ve | |
811 | .PP | |
812 | Returns the value of \f(CW$num\fR taken to the power \f(CW$exp\fR in the modulus | |
813 | \&\f(CW$mod\fR using binary exponentation. \f(CW\*(C`bmodpow\*(C'\fR is far superior to | |
814 | writing | |
815 | .PP | |
816 | .Vb 1 | |
817 | \& $num ** $exp % $mod | |
818 | .Ve | |
819 | .PP | |
820 | because it is much faster \- it reduces internal variables into | |
821 | the modulus whenever possible, so it operates on smaller numbers. | |
822 | .PP | |
823 | \&\f(CW\*(C`bmodpow\*(C'\fR also supports negative exponents. | |
824 | .PP | |
825 | .Vb 1 | |
826 | \& bmodpow($num, -1, $mod) | |
827 | .Ve | |
828 | .PP | |
829 | is exactly equivalent to | |
830 | .PP | |
831 | .Vb 1 | |
832 | \& bmodinv($num, $mod) | |
833 | .Ve | |
834 | .Sh "bpow" | |
835 | .IX Subsection "bpow" | |
836 | .Vb 1 | |
837 | \& $x->bpow($y); # power of arguments (x ** y) | |
838 | .Ve | |
839 | .Sh "blsft" | |
840 | .IX Subsection "blsft" | |
841 | .Vb 2 | |
842 | \& $x->blsft($y); # left shift | |
843 | \& $x->blsft($y,$n); # left shift, in base $n (like 10) | |
844 | .Ve | |
845 | .Sh "brsft" | |
846 | .IX Subsection "brsft" | |
847 | .Vb 2 | |
848 | \& $x->brsft($y); # right shift | |
849 | \& $x->brsft($y,$n); # right shift, in base $n (like 10) | |
850 | .Ve | |
851 | .Sh "band" | |
852 | .IX Subsection "band" | |
853 | .Vb 1 | |
854 | \& $x->band($y); # bitwise and | |
855 | .Ve | |
856 | .Sh "bior" | |
857 | .IX Subsection "bior" | |
858 | .Vb 1 | |
859 | \& $x->bior($y); # bitwise inclusive or | |
860 | .Ve | |
861 | .Sh "bxor" | |
862 | .IX Subsection "bxor" | |
863 | .Vb 1 | |
864 | \& $x->bxor($y); # bitwise exclusive or | |
865 | .Ve | |
866 | .Sh "bnot" | |
867 | .IX Subsection "bnot" | |
868 | .Vb 1 | |
869 | \& $x->bnot(); # bitwise not (two's complement) | |
870 | .Ve | |
871 | .Sh "bsqrt" | |
872 | .IX Subsection "bsqrt" | |
873 | .Vb 1 | |
874 | \& $x->bsqrt(); # calculate square-root | |
875 | .Ve | |
876 | .Sh "bfac" | |
877 | .IX Subsection "bfac" | |
878 | .Vb 1 | |
879 | \& $x->bfac(); # factorial of $x (1*2*3*4*..$x) | |
880 | .Ve | |
881 | .Sh "round" | |
882 | .IX Subsection "round" | |
883 | .Vb 1 | |
884 | \& $x->round($A,$P,$round_mode); | |
885 | .Ve | |
886 | .PP | |
887 | Round \f(CW$x\fR to accuracy \f(CW$A\fR or precision \f(CW$P\fR using the round mode | |
888 | \&\f(CW$round_mode\fR. | |
889 | .Sh "bround" | |
890 | .IX Subsection "bround" | |
891 | .Vb 1 | |
892 | \& $x->bround($N); # accuracy: preserve $N digits | |
893 | .Ve | |
894 | .Sh "bfround" | |
895 | .IX Subsection "bfround" | |
896 | .Vb 1 | |
897 | \& $x->bfround($N); # round to $Nth digit, no-op for BigInts | |
898 | .Ve | |
899 | .Sh "bfloor" | |
900 | .IX Subsection "bfloor" | |
901 | .Vb 1 | |
902 | \& $x->bfloor(); | |
903 | .Ve | |
904 | .PP | |
905 | Set \f(CW$x\fR to the integer less or equal than \f(CW$x\fR. This is a no-op in BigInt, but | |
906 | does change \f(CW$x\fR in BigFloat. | |
907 | .Sh "bceil" | |
908 | .IX Subsection "bceil" | |
909 | .Vb 1 | |
910 | \& $x->bceil(); | |
911 | .Ve | |
912 | .PP | |
913 | Set \f(CW$x\fR to the integer greater or equal than \f(CW$x\fR. This is a no-op in BigInt, but | |
914 | does change \f(CW$x\fR in BigFloat. | |
915 | .Sh "bgcd" | |
916 | .IX Subsection "bgcd" | |
917 | .Vb 1 | |
918 | \& bgcd(@values); # greatest common divisor (no OO style) | |
919 | .Ve | |
920 | .Sh "blcm" | |
921 | .IX Subsection "blcm" | |
922 | .Vb 1 | |
923 | \& blcm(@values); # lowest common multiplicator (no OO style) | |
924 | .Ve | |
925 | .PP | |
926 | head2 length | |
927 | .PP | |
928 | .Vb 2 | |
929 | \& $x->length(); | |
930 | \& ($xl,$fl) = $x->length(); | |
931 | .Ve | |
932 | .PP | |
933 | Returns the number of digits in the decimal representation of the number. | |
934 | In list context, returns the length of the integer and fraction part. For | |
935 | BigInt's, the length of the fraction part will always be 0. | |
936 | .Sh "exponent" | |
937 | .IX Subsection "exponent" | |
938 | .Vb 1 | |
939 | \& $x->exponent(); | |
940 | .Ve | |
941 | .PP | |
942 | Return the exponent of \f(CW$x\fR as BigInt. | |
943 | .Sh "mantissa" | |
944 | .IX Subsection "mantissa" | |
945 | .Vb 1 | |
946 | \& $x->mantissa(); | |
947 | .Ve | |
948 | .PP | |
949 | Return the signed mantissa of \f(CW$x\fR as BigInt. | |
950 | .Sh "parts" | |
951 | .IX Subsection "parts" | |
952 | .Vb 1 | |
953 | \& $x->parts(); # return (mantissa,exponent) as BigInt | |
954 | .Ve | |
955 | .Sh "copy" | |
956 | .IX Subsection "copy" | |
957 | .Vb 1 | |
958 | \& $x->copy(); # make a true copy of $x (unlike $y = $x;) | |
959 | .Ve | |
960 | .Sh "as_int" | |
961 | .IX Subsection "as_int" | |
962 | .Vb 1 | |
963 | \& $x->as_int(); | |
964 | .Ve | |
965 | .PP | |
966 | Returns \f(CW$x\fR as a BigInt (truncated towards zero). In BigInt this is the same as | |
967 | \&\f(CW\*(C`copy()\*(C'\fR. | |
968 | .PP | |
969 | \&\f(CW\*(C`as_number()\*(C'\fR is an alias to this method. \f(CW\*(C`as_number\*(C'\fR was introduced in | |
970 | v1.22, while \f(CW\*(C`as_int()\*(C'\fR was only introduced in v1.68. | |
971 | .Sh "bstr" | |
972 | .IX Subsection "bstr" | |
973 | .Vb 1 | |
974 | \& $x->bstr(); | |
975 | .Ve | |
976 | .PP | |
977 | Returns a normalized string represantation of \f(CW$x\fR. | |
978 | .Sh "bsstr" | |
979 | .IX Subsection "bsstr" | |
980 | .Vb 1 | |
981 | \& $x->bsstr(); # normalized string in scientific notation | |
982 | .Ve | |
983 | .Sh "as_hex" | |
984 | .IX Subsection "as_hex" | |
985 | .Vb 1 | |
986 | \& $x->as_hex(); # as signed hexadecimal string with prefixed 0x | |
987 | .Ve | |
988 | .Sh "as_bin" | |
989 | .IX Subsection "as_bin" | |
990 | .Vb 1 | |
991 | \& $x->as_bin(); # as signed binary string with prefixed 0b | |
992 | .Ve | |
993 | .SH "ACCURACY and PRECISION" | |
994 | .IX Header "ACCURACY and PRECISION" | |
995 | Since version v1.33, Math::BigInt and Math::BigFloat have full support for | |
996 | accuracy and precision based rounding, both automatically after every | |
997 | operation, as well as manually. | |
998 | .PP | |
999 | This section describes the accuracy/precision handling in Math::Big* as it | |
1000 | used to be and as it is now, complete with an explanation of all terms and | |
1001 | abbreviations. | |
1002 | .PP | |
1003 | Not yet implemented things (but with correct description) are marked with '!', | |
1004 | things that need to be answered are marked with '?'. | |
1005 | .PP | |
1006 | In the next paragraph follows a short description of terms used here (because | |
1007 | these may differ from terms used by others people or documentation). | |
1008 | .PP | |
1009 | During the rest of this document, the shortcuts A (for accuracy), P (for | |
1010 | precision), F (fallback) and R (rounding mode) will be used. | |
1011 | .Sh "Precision P" | |
1012 | .IX Subsection "Precision P" | |
1013 | A fixed number of digits before (positive) or after (negative) | |
1014 | the decimal point. For example, 123.45 has a precision of \-2. 0 means an | |
1015 | integer like 123 (or 120). A precision of 2 means two digits to the left | |
1016 | of the decimal point are zero, so 123 with P = 1 becomes 120. Note that | |
1017 | numbers with zeros before the decimal point may have different precisions, | |
1018 | because 1200 can have p = 0, 1 or 2 (depending on what the inital value | |
1019 | was). It could also have p < 0, when the digits after the decimal point | |
1020 | are zero. | |
1021 | .PP | |
1022 | The string output (of floating point numbers) will be padded with zeros: | |
1023 | .PP | |
1024 | .Vb 9 | |
1025 | \& Initial value P A Result String | |
1026 | \& ------------------------------------------------------------ | |
1027 | \& 1234.01 -3 1000 1000 | |
1028 | \& 1234 -2 1200 1200 | |
1029 | \& 1234.5 -1 1230 1230 | |
1030 | \& 1234.001 1 1234 1234.0 | |
1031 | \& 1234.01 0 1234 1234 | |
1032 | \& 1234.01 2 1234.01 1234.01 | |
1033 | \& 1234.01 5 1234.01 1234.01000 | |
1034 | .Ve | |
1035 | .PP | |
1036 | For BigInts, no padding occurs. | |
1037 | .Sh "Accuracy A" | |
1038 | .IX Subsection "Accuracy A" | |
1039 | Number of significant digits. Leading zeros are not counted. A | |
1040 | number may have an accuracy greater than the non-zero digits | |
1041 | when there are zeros in it or trailing zeros. For example, 123.456 has | |
1042 | A of 6, 10203 has 5, 123.0506 has 7, 123.450000 has 8 and 0.000123 has 3. | |
1043 | .PP | |
1044 | The string output (of floating point numbers) will be padded with zeros: | |
1045 | .PP | |
1046 | .Vb 5 | |
1047 | \& Initial value P A Result String | |
1048 | \& ------------------------------------------------------------ | |
1049 | \& 1234.01 3 1230 1230 | |
1050 | \& 1234.01 6 1234.01 1234.01 | |
1051 | \& 1234.1 8 1234.1 1234.1000 | |
1052 | .Ve | |
1053 | .PP | |
1054 | For BigInts, no padding occurs. | |
1055 | .Sh "Fallback F" | |
1056 | .IX Subsection "Fallback F" | |
1057 | When both A and P are undefined, this is used as a fallback accuracy when | |
1058 | dividing numbers. | |
1059 | .Sh "Rounding mode R" | |
1060 | .IX Subsection "Rounding mode R" | |
1061 | When rounding a number, different 'styles' or 'kinds' | |
1062 | of rounding are possible. (Note that random rounding, as in | |
1063 | Math::Round, is not implemented.) | |
1064 | .IP "'trunc'" 2 | |
1065 | .IX Item "'trunc'" | |
1066 | truncation invariably removes all digits following the | |
1067 | rounding place, replacing them with zeros. Thus, 987.65 rounded | |
1068 | to tens (P=1) becomes 980, and rounded to the fourth sigdig | |
1069 | becomes 987.6 (A=4). 123.456 rounded to the second place after the | |
1070 | decimal point (P=\-2) becomes 123.46. | |
1071 | .Sp | |
1072 | All other implemented styles of rounding attempt to round to the | |
1073 | \&\*(L"nearest digit.\*(R" If the digit D immediately to the right of the | |
1074 | rounding place (skipping the decimal point) is greater than 5, the | |
1075 | number is incremented at the rounding place (possibly causing a | |
1076 | cascade of incrementation): e.g. when rounding to units, 0.9 rounds | |
1077 | to 1, and \-19.9 rounds to \-20. If D < 5, the number is similarly | |
1078 | truncated at the rounding place: e.g. when rounding to units, 0.4 | |
1079 | rounds to 0, and \-19.4 rounds to \-19. | |
1080 | .Sp | |
1081 | However the results of other styles of rounding differ if the | |
1082 | digit immediately to the right of the rounding place (skipping the | |
1083 | decimal point) is 5 and if there are no digits, or no digits other | |
1084 | than 0, after that 5. In such cases: | |
1085 | .IP "'even'" 2 | |
1086 | .IX Item "'even'" | |
1087 | rounds the digit at the rounding place to 0, 2, 4, 6, or 8 | |
1088 | if it is not already. E.g., when rounding to the first sigdig, 0.45 | |
1089 | becomes 0.4, \-0.55 becomes \-0.6, but 0.4501 becomes 0.5. | |
1090 | .IP "'odd'" 2 | |
1091 | .IX Item "'odd'" | |
1092 | rounds the digit at the rounding place to 1, 3, 5, 7, or 9 if | |
1093 | it is not already. E.g., when rounding to the first sigdig, 0.45 | |
1094 | becomes 0.5, \-0.55 becomes \-0.5, but 0.5501 becomes 0.6. | |
1095 | .IP "'+inf'" 2 | |
1096 | .IX Item "'+inf'" | |
1097 | round to plus infinity, i.e. always round up. E.g., when | |
1098 | rounding to the first sigdig, 0.45 becomes 0.5, \-0.55 becomes \-0.5, | |
1099 | and 0.4501 also becomes 0.5. | |
1100 | .IP "'\-inf'" 2 | |
1101 | .IX Item "'-inf'" | |
1102 | round to minus infinity, i.e. always round down. E.g., when | |
1103 | rounding to the first sigdig, 0.45 becomes 0.4, \-0.55 becomes \-0.6, | |
1104 | but 0.4501 becomes 0.5. | |
1105 | .IP "'zero'" 2 | |
1106 | .IX Item "'zero'" | |
1107 | round to zero, i.e. positive numbers down, negative ones up. | |
1108 | E.g., when rounding to the first sigdig, 0.45 becomes 0.4, \-0.55 | |
1109 | becomes \-0.5, but 0.4501 becomes 0.5. | |
1110 | .PP | |
1111 | The handling of A & P in \s-1MBI/MBF\s0 (the old core code shipped with Perl | |
1112 | versions <= 5.7.2) is like this: | |
1113 | .IP "Precision" 2 | |
1114 | .IX Item "Precision" | |
1115 | .Vb 3 | |
1116 | \& * ffround($p) is able to round to $p number of digits after the decimal | |
1117 | \& point | |
1118 | \& * otherwise P is unused | |
1119 | .Ve | |
1120 | .IP "Accuracy (significant digits)" 2 | |
1121 | .IX Item "Accuracy (significant digits)" | |
1122 | .Vb 29 | |
1123 | \& * fround($a) rounds to $a significant digits | |
1124 | \& * only fdiv() and fsqrt() take A as (optional) paramater | |
1125 | \& + other operations simply create the same number (fneg etc), or more (fmul) | |
1126 | \& of digits | |
1127 | \& + rounding/truncating is only done when explicitly calling one of fround | |
1128 | \& or ffround, and never for BigInt (not implemented) | |
1129 | \& * fsqrt() simply hands its accuracy argument over to fdiv. | |
1130 | \& * the documentation and the comment in the code indicate two different ways | |
1131 | \& on how fdiv() determines the maximum number of digits it should calculate, | |
1132 | \& and the actual code does yet another thing | |
1133 | \& POD: | |
1134 | \& max($Math::BigFloat::div_scale,length(dividend)+length(divisor)) | |
1135 | \& Comment: | |
1136 | \& result has at most max(scale, length(dividend), length(divisor)) digits | |
1137 | \& Actual code: | |
1138 | \& scale = max(scale, length(dividend)-1,length(divisor)-1); | |
1139 | \& scale += length(divisior) - length(dividend); | |
1140 | \& So for lx = 3, ly = 9, scale = 10, scale will actually be 16 (10+9-3). | |
1141 | \& Actually, the 'difference' added to the scale is calculated from the | |
1142 | \& number of "significant digits" in dividend and divisor, which is derived | |
1143 | \& by looking at the length of the mantissa. Which is wrong, since it includes | |
1144 | \& the + sign (oops) and actually gets 2 for '+100' and 4 for '+101'. Oops | |
1145 | \& again. Thus 124/3 with div_scale=1 will get you '41.3' based on the strange | |
1146 | \& assumption that 124 has 3 significant digits, while 120/7 will get you | |
1147 | \& '17', not '17.1' since 120 is thought to have 2 significant digits. | |
1148 | \& The rounding after the division then uses the remainder and $y to determine | |
1149 | \& wether it must round up or down. | |
1150 | \& ? I have no idea which is the right way. That's why I used a slightly more | |
1151 | \& ? simple scheme and tweaked the few failing testcases to match it. | |
1152 | .Ve | |
1153 | .PP | |
1154 | This is how it works now: | |
1155 | .IP "Setting/Accessing" 2 | |
1156 | .IX Item "Setting/Accessing" | |
1157 | .Vb 20 | |
1158 | \& * You can set the A global via C<< Math::BigInt->accuracy() >> or | |
1159 | \& C<< Math::BigFloat->accuracy() >> or whatever class you are using. | |
1160 | \& * You can also set P globally by using C<< Math::SomeClass->precision() >> | |
1161 | \& likewise. | |
1162 | \& * Globals are classwide, and not inherited by subclasses. | |
1163 | \& * to undefine A, use C<< Math::SomeCLass->accuracy(undef); >> | |
1164 | \& * to undefine P, use C<< Math::SomeClass->precision(undef); >> | |
1165 | \& * Setting C<< Math::SomeClass->accuracy() >> clears automatically | |
1166 | \& C<< Math::SomeClass->precision() >>, and vice versa. | |
1167 | \& * To be valid, A must be > 0, P can have any value. | |
1168 | \& * If P is negative, this means round to the P'th place to the right of the | |
1169 | \& decimal point; positive values mean to the left of the decimal point. | |
1170 | \& P of 0 means round to integer. | |
1171 | \& * to find out the current global A, use C<< Math::SomeClass->accuracy() >> | |
1172 | \& * to find out the current global P, use C<< Math::SomeClass->precision() >> | |
1173 | \& * use C<< $x->accuracy() >> respective C<< $x->precision() >> for the local | |
1174 | \& setting of C<< $x >>. | |
1175 | \& * Please note that C<< $x->accuracy() >> respecive C<< $x->precision() >> | |
1176 | \& return eventually defined global A or P, when C<< $x >>'s A or P is not | |
1177 | \& set. | |
1178 | .Ve | |
1179 | .IP "Creating numbers" 2 | |
1180 | .IX Item "Creating numbers" | |
1181 | .Vb 12 | |
1182 | \& * When you create a number, you can give it's desired A or P via: | |
1183 | \& $x = Math::BigInt->new($number,$A,$P); | |
1184 | \& * Only one of A or P can be defined, otherwise the result is NaN | |
1185 | \& * If no A or P is give ($x = Math::BigInt->new($number) form), then the | |
1186 | \& globals (if set) will be used. Thus changing the global defaults later on | |
1187 | \& will not change the A or P of previously created numbers (i.e., A and P of | |
1188 | \& $x will be what was in effect when $x was created) | |
1189 | \& * If given undef for A and P, B<no> rounding will occur, and the globals will | |
1190 | \& B<not> be used. This is used by subclasses to create numbers without | |
1191 | \& suffering rounding in the parent. Thus a subclass is able to have it's own | |
1192 | \& globals enforced upon creation of a number by using | |
1193 | \& C<< $x = Math::BigInt->new($number,undef,undef) >>: | |
1194 | .Ve | |
1195 | .Sp | |
1196 | .Vb 2 | |
1197 | \& use Math::BigInt::SomeSubclass; | |
1198 | \& use Math::BigInt; | |
1199 | .Ve | |
1200 | .Sp | |
1201 | .Vb 3 | |
1202 | \& Math::BigInt->accuracy(2); | |
1203 | \& Math::BigInt::SomeSubClass->accuracy(3); | |
1204 | \& $x = Math::BigInt::SomeSubClass->new(1234); | |
1205 | .Ve | |
1206 | .Sp | |
1207 | .Vb 2 | |
1208 | \& $x is now 1230, and not 1200. A subclass might choose to implement | |
1209 | \& this otherwise, e.g. falling back to the parent's A and P. | |
1210 | .Ve | |
1211 | .IP "Usage" 2 | |
1212 | .IX Item "Usage" | |
1213 | .Vb 7 | |
1214 | \& * If A or P are enabled/defined, they are used to round the result of each | |
1215 | \& operation according to the rules below | |
1216 | \& * Negative P is ignored in Math::BigInt, since BigInts never have digits | |
1217 | \& after the decimal point | |
1218 | \& * Math::BigFloat uses Math::BigInt internally, but setting A or P inside | |
1219 | \& Math::BigInt as globals does not tamper with the parts of a BigFloat. | |
1220 | \& A flag is used to mark all Math::BigFloat numbers as 'never round'. | |
1221 | .Ve | |
1222 | .IP "Precedence" 2 | |
1223 | .IX Item "Precedence" | |
1224 | .Vb 29 | |
1225 | \& * It only makes sense that a number has only one of A or P at a time. | |
1226 | \& If you set either A or P on one object, or globally, the other one will | |
1227 | \& be automatically cleared. | |
1228 | \& * If two objects are involved in an operation, and one of them has A in | |
1229 | \& effect, and the other P, this results in an error (NaN). | |
1230 | \& * A takes precendence over P (Hint: A comes before P). | |
1231 | \& If neither of them is defined, nothing is used, i.e. the result will have | |
1232 | \& as many digits as it can (with an exception for fdiv/fsqrt) and will not | |
1233 | \& be rounded. | |
1234 | \& * There is another setting for fdiv() (and thus for fsqrt()). If neither of | |
1235 | \& A or P is defined, fdiv() will use a fallback (F) of $div_scale digits. | |
1236 | \& If either the dividend's or the divisor's mantissa has more digits than | |
1237 | \& the value of F, the higher value will be used instead of F. | |
1238 | \& This is to limit the digits (A) of the result (just consider what would | |
1239 | \& happen with unlimited A and P in the case of 1/3 :-) | |
1240 | \& * fdiv will calculate (at least) 4 more digits than required (determined by | |
1241 | \& A, P or F), and, if F is not used, round the result | |
1242 | \& (this will still fail in the case of a result like 0.12345000000001 with A | |
1243 | \& or P of 5, but this can not be helped - or can it?) | |
1244 | \& * Thus you can have the math done by on Math::Big* class in two modi: | |
1245 | \& + never round (this is the default): | |
1246 | \& This is done by setting A and P to undef. No math operation | |
1247 | \& will round the result, with fdiv() and fsqrt() as exceptions to guard | |
1248 | \& against overflows. You must explicitely call bround(), bfround() or | |
1249 | \& round() (the latter with parameters). | |
1250 | \& Note: Once you have rounded a number, the settings will 'stick' on it | |
1251 | \& and 'infect' all other numbers engaged in math operations with it, since | |
1252 | \& local settings have the highest precedence. So, to get SaferRound[tm], | |
1253 | \& use a copy() before rounding like this: | |
1254 | .Ve | |
1255 | .Sp | |
1256 | .Vb 6 | |
1257 | \& $x = Math::BigFloat->new(12.34); | |
1258 | \& $y = Math::BigFloat->new(98.76); | |
1259 | \& $z = $x * $y; # 1218.6984 | |
1260 | \& print $x->copy()->fround(3); # 12.3 (but A is now 3!) | |
1261 | \& $z = $x * $y; # still 1218.6984, without | |
1262 | \& # copy would have been 1210! | |
1263 | .Ve | |
1264 | .Sp | |
1265 | .Vb 6 | |
1266 | \& + round after each op: | |
1267 | \& After each single operation (except for testing like is_zero()), the | |
1268 | \& method round() is called and the result is rounded appropriately. By | |
1269 | \& setting proper values for A and P, you can have all-the-same-A or | |
1270 | \& all-the-same-P modes. For example, Math::Currency might set A to undef, | |
1271 | \& and P to -2, globally. | |
1272 | .Ve | |
1273 | .Sp | |
1274 | .Vb 2 | |
1275 | \& ?Maybe an extra option that forbids local A & P settings would be in order, | |
1276 | \& ?so that intermediate rounding does not 'poison' further math? | |
1277 | .Ve | |
1278 | .IP "Overriding globals" 2 | |
1279 | .IX Item "Overriding globals" | |
1280 | .Vb 16 | |
1281 | \& * you will be able to give A, P and R as an argument to all the calculation | |
1282 | \& routines; the second parameter is A, the third one is P, and the fourth is | |
1283 | \& R (shift right by one for binary operations like badd). P is used only if | |
1284 | \& the first parameter (A) is undefined. These three parameters override the | |
1285 | \& globals in the order detailed as follows, i.e. the first defined value | |
1286 | \& wins: | |
1287 | \& (local: per object, global: global default, parameter: argument to sub) | |
1288 | \& + parameter A | |
1289 | \& + parameter P | |
1290 | \& + local A (if defined on both of the operands: smaller one is taken) | |
1291 | \& + local P (if defined on both of the operands: bigger one is taken) | |
1292 | \& + global A | |
1293 | \& + global P | |
1294 | \& + global F | |
1295 | \& * fsqrt() will hand its arguments to fdiv(), as it used to, only now for two | |
1296 | \& arguments (A and P) instead of one | |
1297 | .Ve | |
1298 | .IP "Local settings" 2 | |
1299 | .IX Item "Local settings" | |
1300 | .Vb 5 | |
1301 | \& * You can set A or P locally by using C<< $x->accuracy() >> or | |
1302 | \& C<< $x->precision() >> | |
1303 | \& and thus force different A and P for different objects/numbers. | |
1304 | \& * Setting A or P this way immediately rounds $x to the new value. | |
1305 | \& * C<< $x->accuracy() >> clears C<< $x->precision() >>, and vice versa. | |
1306 | .Ve | |
1307 | .IP "Rounding" 2 | |
1308 | .IX Item "Rounding" | |
1309 | .Vb 15 | |
1310 | \& * the rounding routines will use the respective global or local settings. | |
1311 | \& fround()/bround() is for accuracy rounding, while ffround()/bfround() | |
1312 | \& is for precision | |
1313 | \& * the two rounding functions take as the second parameter one of the | |
1314 | \& following rounding modes (R): | |
1315 | \& 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' | |
1316 | \& * you can set/get the global R by using C<< Math::SomeClass->round_mode() >> | |
1317 | \& or by setting C<< $Math::SomeClass::round_mode >> | |
1318 | \& * after each operation, C<< $result->round() >> is called, and the result may | |
1319 | \& eventually be rounded (that is, if A or P were set either locally, | |
1320 | \& globally or as parameter to the operation) | |
1321 | \& * to manually round a number, call C<< $x->round($A,$P,$round_mode); >> | |
1322 | \& this will round the number by using the appropriate rounding function | |
1323 | \& and then normalize it. | |
1324 | \& * rounding modifies the local settings of the number: | |
1325 | .Ve | |
1326 | .Sp | |
1327 | .Vb 3 | |
1328 | \& $x = Math::BigFloat->new(123.456); | |
1329 | \& $x->accuracy(5); | |
1330 | \& $x->bround(4); | |
1331 | .Ve | |
1332 | .Sp | |
1333 | .Vb 2 | |
1334 | \& Here 4 takes precedence over 5, so 123.5 is the result and $x->accuracy() | |
1335 | \& will be 4 from now on. | |
1336 | .Ve | |
1337 | .IP "Default values" 2 | |
1338 | .IX Item "Default values" | |
1339 | .Vb 4 | |
1340 | \& * R: 'even' | |
1341 | \& * F: 40 | |
1342 | \& * A: undef | |
1343 | \& * P: undef | |
1344 | .Ve | |
1345 | .IP "Remarks" 2 | |
1346 | .IX Item "Remarks" | |
1347 | .Vb 5 | |
1348 | \& * The defaults are set up so that the new code gives the same results as | |
1349 | \& the old code (except in a few cases on fdiv): | |
1350 | \& + Both A and P are undefined and thus will not be used for rounding | |
1351 | \& after each operation. | |
1352 | \& + round() is thus a no-op, unless given extra parameters A and P | |
1353 | .Ve | |
1354 | .SH "Infinity and Not a Number" | |
1355 | .IX Header "Infinity and Not a Number" | |
1356 | While BigInt has extensive handling of inf and NaN, certain quirks remain. | |
1357 | .IP "\fIoct()\fR/\fIhex()\fR" 2 | |
1358 | .IX Item "oct()/hex()" | |
1359 | These perl routines currently (as of Perl v.5.8.6) cannot handle passed | |
1360 | inf. | |
1361 | .Sp | |
1362 | .Vb 9 | |
1363 | \& te@linux:~> perl -wle 'print 2 ** 3333' | |
1364 | \& inf | |
1365 | \& te@linux:~> perl -wle 'print 2 ** 3333 == 2 ** 3333' | |
1366 | \& 1 | |
1367 | \& te@linux:~> perl -wle 'print oct(2 ** 3333)' | |
1368 | \& 0 | |
1369 | \& te@linux:~> perl -wle 'print hex(2 ** 3333)' | |
1370 | \& Illegal hexadecimal digit 'i' ignored at -e line 1. | |
1371 | \& 0 | |
1372 | .Ve | |
1373 | .Sp | |
1374 | The same problems occur if you pass them Math::BigInt\->\fIbinf()\fR objects. Since | |
1375 | overloading these routines is not possible, this cannot be fixed from BigInt. | |
1376 | .IP "==, !=, <, >, <=, >= with NaNs" 2 | |
1377 | .IX Item "==, !=, <, >, <=, >= with NaNs" | |
1378 | BigInt's \fIbcmp()\fR routine currently returns undef to signal that a NaN was | |
1379 | involved in a comparisation. However, the overload code turns that into | |
1380 | either 1 or '' and thus operations like \f(CW\*(C`NaN != NaN\*(C'\fR might return | |
1381 | wrong values. | |
1382 | .IP "log(\-inf)" 2 | |
1383 | .IX Item "log(-inf)" | |
1384 | \&\f(CW\*(C`log(\-inf)\*(C'\fR is highly weird. Since log(\-x)=pi*i+log(x), then | |
1385 | log(\-inf)=pi*i+inf. However, since the imaginary part is finite, the real | |
1386 | infinity \*(L"overshadows\*(R" it, so the number might as well just be infinity. | |
1387 | However, the result is a complex number, and since BigInt/BigFloat can only | |
1388 | have real numbers as results, the result is NaN. | |
1389 | .IP "\fIexp()\fR, \fIcos()\fR, \fIsin()\fR, \fIatan2()\fR" 2 | |
1390 | .IX Item "exp(), cos(), sin(), atan2()" | |
1391 | These all might have problems handling infinity right. | |
1392 | .SH "INTERNALS" | |
1393 | .IX Header "INTERNALS" | |
1394 | The actual numbers are stored as unsigned big integers (with seperate sign). | |
1395 | .PP | |
1396 | You should neither care about nor depend on the internal representation; it | |
1397 | might change without notice. Use \fB\s-1ONLY\s0\fR method calls like \f(CW\*(C`$x\->sign();\*(C'\fR | |
1398 | instead relying on the internal representation. | |
1399 | .Sh "\s-1MATH\s0 \s-1LIBRARY\s0" | |
1400 | .IX Subsection "MATH LIBRARY" | |
1401 | Math with the numbers is done (by default) by a module called | |
1402 | \&\f(CW\*(C`Math::BigInt::Calc\*(C'\fR. This is equivalent to saying: | |
1403 | .PP | |
1404 | .Vb 1 | |
1405 | \& use Math::BigInt lib => 'Calc'; | |
1406 | .Ve | |
1407 | .PP | |
1408 | You can change this by using: | |
1409 | .PP | |
1410 | .Vb 1 | |
1411 | \& use Math::BigInt lib => 'BitVect'; | |
1412 | .Ve | |
1413 | .PP | |
1414 | The following would first try to find Math::BigInt::Foo, then | |
1415 | Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc: | |
1416 | .PP | |
1417 | .Vb 1 | |
1418 | \& use Math::BigInt lib => 'Foo,Math::BigInt::Bar'; | |
1419 | .Ve | |
1420 | .PP | |
1421 | Since Math::BigInt::GMP is in almost all cases faster than Calc (especially in | |
1422 | math involving really big numbers, where it is \fBmuch\fR faster), and there is | |
1423 | no penalty if Math::BigInt::GMP is not installed, it is a good idea to always | |
1424 | use the following: | |
1425 | .PP | |
1426 | .Vb 1 | |
1427 | \& use Math::BigInt lib => 'GMP'; | |
1428 | .Ve | |
1429 | .PP | |
1430 | Different low-level libraries use different formats to store the | |
1431 | numbers. You should \fB\s-1NOT\s0\fR depend on the number having a specific format | |
1432 | internally. | |
1433 | .PP | |
1434 | See the respective math library module documentation for further details. | |
1435 | .Sh "\s-1SIGN\s0" | |
1436 | .IX Subsection "SIGN" | |
1437 | The sign is either '+', '\-', 'NaN', '+inf' or '\-inf'. | |
1438 | .PP | |
1439 | A sign of 'NaN' is used to represent the result when input arguments are not | |
1440 | numbers or as a result of 0/0. '+inf' and '\-inf' represent plus respectively | |
1441 | minus infinity. You will get '+inf' when dividing a positive number by 0, and | |
1442 | \&'\-inf' when dividing any negative number by 0. | |
1443 | .Sh "\fImantissa()\fP, \fIexponent()\fP and \fIparts()\fP" | |
1444 | .IX Subsection "mantissa(), exponent() and parts()" | |
1445 | \&\f(CW\*(C`mantissa()\*(C'\fR and \f(CW\*(C`exponent()\*(C'\fR return the said parts of the BigInt such | |
1446 | that: | |
1447 | .PP | |
1448 | .Vb 4 | |
1449 | \& $m = $x->mantissa(); | |
1450 | \& $e = $x->exponent(); | |
1451 | \& $y = $m * ( 10 ** $e ); | |
1452 | \& print "ok\en" if $x == $y; | |
1453 | .Ve | |
1454 | .PP | |
1455 | \&\f(CW\*(C`($m,$e) = $x\->parts()\*(C'\fR is just a shortcut that gives you both of them | |
1456 | in one go. Both the returned mantissa and exponent have a sign. | |
1457 | .PP | |
1458 | Currently, for BigInts \f(CW$e\fR is always 0, except for NaN, +inf and \-inf, | |
1459 | where it is \f(CW\*(C`NaN\*(C'\fR; and for \f(CW\*(C`$x == 0\*(C'\fR, where it is \f(CW1\fR (to be compatible | |
1460 | with Math::BigFloat's internal representation of a zero as \f(CW0E1\fR). | |
1461 | .PP | |
1462 | \&\f(CW$m\fR is currently just a copy of the original number. The relation between | |
1463 | \&\f(CW$e\fR and \f(CW$m\fR will stay always the same, though their real values might | |
1464 | change. | |
1465 | .SH "EXAMPLES" | |
1466 | .IX Header "EXAMPLES" | |
1467 | .Vb 1 | |
1468 | \& use Math::BigInt; | |
1469 | .Ve | |
1470 | .PP | |
1471 | .Vb 1 | |
1472 | \& sub bint { Math::BigInt->new(shift); } | |
1473 | .Ve | |
1474 | .PP | |
1475 | .Vb 15 | |
1476 | \& $x = Math::BigInt->bstr("1234") # string "1234" | |
1477 | \& $x = "$x"; # same as bstr() | |
1478 | \& $x = Math::BigInt->bneg("1234"); # BigInt "-1234" | |
1479 | \& $x = Math::BigInt->babs("-12345"); # BigInt "12345" | |
1480 | \& $x = Math::BigInt->bnorm("-0 00"); # BigInt "0" | |
1481 | \& $x = bint(1) + bint(2); # BigInt "3" | |
1482 | \& $x = bint(1) + "2"; # ditto (auto-BigIntify of "2") | |
1483 | \& $x = bint(1); # BigInt "1" | |
1484 | \& $x = $x + 5 / 2; # BigInt "3" | |
1485 | \& $x = $x ** 3; # BigInt "27" | |
1486 | \& $x *= 2; # BigInt "54" | |
1487 | \& $x = Math::BigInt->new(0); # BigInt "0" | |
1488 | \& $x--; # BigInt "-1" | |
1489 | \& $x = Math::BigInt->badd(4,5) # BigInt "9" | |
1490 | \& print $x->bsstr(); # 9e+0 | |
1491 | .Ve | |
1492 | .PP | |
1493 | Examples for rounding: | |
1494 | .PP | |
1495 | .Vb 2 | |
1496 | \& use Math::BigFloat; | |
1497 | \& use Test; | |
1498 | .Ve | |
1499 | .PP | |
1500 | .Vb 3 | |
1501 | \& $x = Math::BigFloat->new(123.4567); | |
1502 | \& $y = Math::BigFloat->new(123.456789); | |
1503 | \& Math::BigFloat->accuracy(4); # no more A than 4 | |
1504 | .Ve | |
1505 | .PP | |
1506 | .Vb 9 | |
1507 | \& ok ($x->copy()->fround(),123.4); # even rounding | |
1508 | \& print $x->copy()->fround(),"\en"; # 123.4 | |
1509 | \& Math::BigFloat->round_mode('odd'); # round to odd | |
1510 | \& print $x->copy()->fround(),"\en"; # 123.5 | |
1511 | \& Math::BigFloat->accuracy(5); # no more A than 5 | |
1512 | \& Math::BigFloat->round_mode('odd'); # round to odd | |
1513 | \& print $x->copy()->fround(),"\en"; # 123.46 | |
1514 | \& $y = $x->copy()->fround(4),"\en"; # A = 4: 123.4 | |
1515 | \& print "$y, ",$y->accuracy(),"\en"; # 123.4, 4 | |
1516 | .Ve | |
1517 | .PP | |
1518 | .Vb 4 | |
1519 | \& Math::BigFloat->accuracy(undef); # A not important now | |
1520 | \& Math::BigFloat->precision(2); # P important | |
1521 | \& print $x->copy()->bnorm(),"\en"; # 123.46 | |
1522 | \& print $x->copy()->fround(),"\en"; # 123.46 | |
1523 | .Ve | |
1524 | .PP | |
1525 | Examples for converting: | |
1526 | .PP | |
1527 | .Vb 2 | |
1528 | \& my $x = Math::BigInt->new('0b1'.'01' x 123); | |
1529 | \& print "bin: ",$x->as_bin()," hex:",$x->as_hex()," dec: ",$x,"\en"; | |
1530 | .Ve | |
1531 | .SH "Autocreating constants" | |
1532 | .IX Header "Autocreating constants" | |
1533 | After \f(CW\*(C`use Math::BigInt ':constant'\*(C'\fR all the \fBinteger\fR decimal, hexadecimal | |
1534 | and binary constants in the given scope are converted to \f(CW\*(C`Math::BigInt\*(C'\fR. | |
1535 | This conversion happens at compile time. | |
1536 | .PP | |
1537 | In particular, | |
1538 | .PP | |
1539 | .Vb 1 | |
1540 | \& perl -MMath::BigInt=:constant -e 'print 2**100,"\en"' | |
1541 | .Ve | |
1542 | .PP | |
1543 | prints the integer value of \f(CW\*(C`2**100\*(C'\fR. Note that without conversion of | |
1544 | constants the expression 2**100 will be calculated as perl scalar. | |
1545 | .PP | |
1546 | Please note that strings and floating point constants are not affected, | |
1547 | so that | |
1548 | .PP | |
1549 | .Vb 1 | |
1550 | \& use Math::BigInt qw/:constant/; | |
1551 | .Ve | |
1552 | .PP | |
1553 | .Vb 4 | |
1554 | \& $x = 1234567890123456789012345678901234567890 | |
1555 | \& + 123456789123456789; | |
1556 | \& $y = '1234567890123456789012345678901234567890' | |
1557 | \& + '123456789123456789'; | |
1558 | .Ve | |
1559 | .PP | |
1560 | do not work. You need an explicit Math::BigInt\->\fInew()\fR around one of the | |
1561 | operands. You should also quote large constants to protect loss of precision: | |
1562 | .PP | |
1563 | .Vb 1 | |
1564 | \& use Math::BigInt; | |
1565 | .Ve | |
1566 | .PP | |
1567 | .Vb 1 | |
1568 | \& $x = Math::BigInt->new('1234567889123456789123456789123456789'); | |
1569 | .Ve | |
1570 | .PP | |
1571 | Without the quotes Perl would convert the large number to a floating point | |
1572 | constant at compile time and then hand the result to BigInt, which results in | |
1573 | an truncated result or a NaN. | |
1574 | .PP | |
1575 | This also applies to integers that look like floating point constants: | |
1576 | .PP | |
1577 | .Vb 1 | |
1578 | \& use Math::BigInt ':constant'; | |
1579 | .Ve | |
1580 | .PP | |
1581 | .Vb 2 | |
1582 | \& print ref(123e2),"\en"; | |
1583 | \& print ref(123.2e2),"\en"; | |
1584 | .Ve | |
1585 | .PP | |
1586 | will print nothing but newlines. Use either bignum or Math::BigFloat | |
1587 | to get this to work. | |
1588 | .SH "PERFORMANCE" | |
1589 | .IX Header "PERFORMANCE" | |
1590 | 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 | |
1591 | must be made in the second case. For long numbers, the copy can eat up to 20% | |
1592 | of the work (in the case of addition/subtraction, less for | |
1593 | multiplication/division). If \f(CW$y\fR is very small compared to \f(CW$x\fR, the form | |
1594 | \&\f(CW$x\fR += \f(CW$y\fR is \s-1MUCH\s0 faster than \f(CW$x\fR = \f(CW$x\fR + \f(CW$y\fR since making the copy of \f(CW$x\fR takes | |
1595 | more time then the actual addition. | |
1596 | .PP | |
1597 | With a technique called copy\-on\-write, the cost of copying with overload could | |
1598 | be minimized or even completely avoided. A test implementation of \s-1COW\s0 did show | |
1599 | performance gains for overloaded math, but introduced a performance loss due | |
1600 | to a constant overhead for all other operatons. So Math::BigInt does currently | |
1601 | not \s-1COW\s0. | |
1602 | .PP | |
1603 | The rewritten version of this module (vs. v0.01) is slower on certain | |
1604 | operations, like \f(CW\*(C`new()\*(C'\fR, \f(CW\*(C`bstr()\*(C'\fR and \f(CW\*(C`numify()\*(C'\fR. The reason are that it | |
1605 | does now more work and handles much more cases. The time spent in these | |
1606 | operations is usually gained in the other math operations so that code on | |
1607 | the average should get (much) faster. If they don't, please contact the author. | |
1608 | .PP | |
1609 | Some operations may be slower for small numbers, but are significantly faster | |
1610 | for big numbers. Other operations are now constant (O(1), like \f(CW\*(C`bneg()\*(C'\fR, | |
1611 | \&\f(CW\*(C`babs()\*(C'\fR etc), instead of O(N) and thus nearly always take much less time. | |
1612 | These optimizations were done on purpose. | |
1613 | .PP | |
1614 | If you find the Calc module to slow, try to install any of the replacement | |
1615 | modules and see if they help you. | |
1616 | .Sh "Alternative math libraries" | |
1617 | .IX Subsection "Alternative math libraries" | |
1618 | You can use an alternative library to drive Math::BigInt via: | |
1619 | .PP | |
1620 | .Vb 1 | |
1621 | \& use Math::BigInt lib => 'Module'; | |
1622 | .Ve | |
1623 | .PP | |
1624 | See \*(L"\s-1MATH\s0 \s-1LIBRARY\s0\*(R" for more information. | |
1625 | .PP | |
1626 | For more benchmark results see <http://bloodgate.com/perl/benchmarks.html>. | |
1627 | .Sh "\s-1SUBCLASSING\s0" | |
1628 | .IX Subsection "SUBCLASSING" | |
1629 | .SH "Subclassing Math::BigInt" | |
1630 | .IX Header "Subclassing Math::BigInt" | |
1631 | The basic design of Math::BigInt allows simple subclasses with very little | |
1632 | work, as long as a few simple rules are followed: | |
1633 | .IP "\(bu" 2 | |
1634 | The public \s-1API\s0 must remain consistent, i.e. if a sub-class is overloading | |
1635 | addition, the sub-class must use the same name, in this case \fIbadd()\fR. The | |
1636 | reason for this is that Math::BigInt is optimized to call the object methods | |
1637 | directly. | |
1638 | .IP "\(bu" 2 | |
1639 | The private object hash keys like \f(CW\*(C`$x\-\*(C'\fR{sign}> may not be changed, but | |
1640 | additional keys can be added, like \f(CW\*(C`$x\-\*(C'\fR{_custom}>. | |
1641 | .IP "\(bu" 2 | |
1642 | Accessor functions are available for all existing object hash keys and should | |
1643 | be used instead of directly accessing the internal hash keys. The reason for | |
1644 | this is that Math::BigInt itself has a pluggable interface which permits it | |
1645 | to support different storage methods. | |
1646 | .PP | |
1647 | More complex sub-classes may have to replicate more of the logic internal of | |
1648 | Math::BigInt if they need to change more basic behaviors. A subclass that | |
1649 | needs to merely change the output only needs to overload \f(CW\*(C`bstr()\*(C'\fR. | |
1650 | .PP | |
1651 | All other object methods and overloaded functions can be directly inherited | |
1652 | from the parent class. | |
1653 | .PP | |
1654 | At the very minimum, any subclass will need to provide it's own \f(CW\*(C`new()\*(C'\fR and can | |
1655 | store additional hash keys in the object. There are also some package globals | |
1656 | that must be defined, e.g.: | |
1657 | .PP | |
1658 | .Vb 5 | |
1659 | \& # Globals | |
1660 | \& $accuracy = undef; | |
1661 | \& $precision = -2; # round to 2 decimal places | |
1662 | \& $round_mode = 'even'; | |
1663 | \& $div_scale = 40; | |
1664 | .Ve | |
1665 | .PP | |
1666 | Additionally, you might want to provide the following two globals to allow | |
1667 | auto-upgrading and auto-downgrading to work correctly: | |
1668 | .PP | |
1669 | .Vb 2 | |
1670 | \& $upgrade = undef; | |
1671 | \& $downgrade = undef; | |
1672 | .Ve | |
1673 | .PP | |
1674 | This allows Math::BigInt to correctly retrieve package globals from the | |
1675 | subclass, like \f(CW$SubClass::precision\fR. See t/Math/BigInt/Subclass.pm or | |
1676 | t/Math/BigFloat/SubClass.pm completely functional subclass examples. | |
1677 | .PP | |
1678 | Don't forget to | |
1679 | .PP | |
1680 | .Vb 1 | |
1681 | \& use overload; | |
1682 | .Ve | |
1683 | .PP | |
1684 | in your subclass to automatically inherit the overloading from the parent. If | |
1685 | you like, you can change part of the overloading, look at Math::String for an | |
1686 | example. | |
1687 | .SH "UPGRADING" | |
1688 | .IX Header "UPGRADING" | |
1689 | When used like this: | |
1690 | .PP | |
1691 | .Vb 1 | |
1692 | \& use Math::BigInt upgrade => 'Foo::Bar'; | |
1693 | .Ve | |
1694 | .PP | |
1695 | certain operations will 'upgrade' their calculation and thus the result to | |
1696 | the class Foo::Bar. Usually this is used in conjunction with Math::BigFloat: | |
1697 | .PP | |
1698 | .Vb 1 | |
1699 | \& use Math::BigInt upgrade => 'Math::BigFloat'; | |
1700 | .Ve | |
1701 | .PP | |
1702 | As a shortcut, you can use the module \f(CW\*(C`bignum\*(C'\fR: | |
1703 | .PP | |
1704 | .Vb 1 | |
1705 | \& use bignum; | |
1706 | .Ve | |
1707 | .PP | |
1708 | Also good for oneliners: | |
1709 | .PP | |
1710 | .Vb 1 | |
1711 | \& perl -Mbignum -le 'print 2 ** 255' | |
1712 | .Ve | |
1713 | .PP | |
1714 | This makes it possible to mix arguments of different classes (as in 2.5 + 2) | |
1715 | as well es preserve accuracy (as in \fIsqrt\fR\|(3)). | |
1716 | .PP | |
1717 | Beware: This feature is not fully implemented yet. | |
1718 | .Sh "Auto-upgrade" | |
1719 | .IX Subsection "Auto-upgrade" | |
1720 | The following methods upgrade themselves unconditionally; that is if upgrade | |
1721 | is in effect, they will always hand up their work: | |
1722 | .IP "\fIbsqrt()\fR" 2 | |
1723 | .IX Item "bsqrt()" | |
1724 | .PD 0 | |
1725 | .IP "\fIdiv()\fR" 2 | |
1726 | .IX Item "div()" | |
1727 | .IP "\fIblog()\fR" 2 | |
1728 | .IX Item "blog()" | |
1729 | .PD | |
1730 | .PP | |
1731 | Beware: This list is not complete. | |
1732 | .PP | |
1733 | All other methods upgrade themselves only when one (or all) of their | |
1734 | arguments are of the class mentioned in \f(CW$upgrade\fR (This might change in later | |
1735 | versions to a more sophisticated scheme): | |
1736 | .SH "BUGS" | |
1737 | .IX Header "BUGS" | |
1738 | .IP "\fIbroot()\fR does not work" 2 | |
1739 | .IX Item "broot() does not work" | |
1740 | The \fIbroot()\fR function in BigInt may only work for small values. This will be | |
1741 | fixed in a later version. | |
1742 | .IP "Out of Memory!" 2 | |
1743 | .IX Item "Out of Memory!" | |
1744 | Under Perl prior to 5.6.0 having an \f(CW\*(C`use Math::BigInt ':constant';\*(C'\fR and | |
1745 | \&\f(CW\*(C`eval()\*(C'\fR in your code will crash with \*(L"Out of memory\*(R". This is probably an | |
1746 | overload/exporter bug. You can workaround by not having \f(CW\*(C`eval()\*(C'\fR | |
1747 | and ':constant' at the same time or upgrade your Perl to a newer version. | |
1748 | .IP "Fails to load Calc on Perl prior 5.6.0" 2 | |
1749 | .IX Item "Fails to load Calc on Perl prior 5.6.0" | |
1750 | Since eval(' use ...') can not be used in conjunction with ':constant', BigInt | |
1751 | will fall back to eval { require ... } when loading the math lib on Perls | |
1752 | prior to 5.6.0. This simple replaces '::' with '/' and thus might fail on | |
1753 | filesystems using a different seperator. | |
1754 | .SH "CAVEATS" | |
1755 | .IX Header "CAVEATS" | |
1756 | Some things might not work as you expect them. Below is documented what is | |
1757 | known to be troublesome: | |
1758 | .IP "\fIbstr()\fR, \fIbsstr()\fR and 'cmp'" 1 | |
1759 | .IX Item "bstr(), bsstr() and 'cmp'" | |
1760 | Both \f(CW\*(C`bstr()\*(C'\fR and \f(CW\*(C`bsstr()\*(C'\fR as well as automated stringify via overload now | |
1761 | drop the leading '+'. The old code would return '+3', the new returns '3'. | |
1762 | This is to be consistent with Perl and to make \f(CW\*(C`cmp\*(C'\fR (especially with | |
1763 | overloading) to work as you expect. It also solves problems with \f(CW\*(C`Test.pm\*(C'\fR, | |
1764 | because it's \f(CW\*(C`ok()\*(C'\fR uses 'eq' internally. | |
1765 | .Sp | |
1766 | Mark Biggar said, when asked about to drop the '+' altogether, or make only | |
1767 | \&\f(CW\*(C`cmp\*(C'\fR work: | |
1768 | .Sp | |
1769 | .Vb 4 | |
1770 | \& I agree (with the first alternative), don't add the '+' on positive | |
1771 | \& numbers. It's not as important anymore with the new internal | |
1772 | \& form for numbers. It made doing things like abs and neg easier, | |
1773 | \& but those have to be done differently now anyway. | |
1774 | .Ve | |
1775 | .Sp | |
1776 | So, the following examples will now work all as expected: | |
1777 | .Sp | |
1778 | .Vb 3 | |
1779 | \& use Test; | |
1780 | \& BEGIN { plan tests => 1 } | |
1781 | \& use Math::BigInt; | |
1782 | .Ve | |
1783 | .Sp | |
1784 | .Vb 2 | |
1785 | \& my $x = new Math::BigInt 3*3; | |
1786 | \& my $y = new Math::BigInt 3*3; | |
1787 | .Ve | |
1788 | .Sp | |
1789 | .Vb 4 | |
1790 | \& ok ($x,3*3); | |
1791 | \& print "$x eq 9" if $x eq $y; | |
1792 | \& print "$x eq 9" if $x eq '9'; | |
1793 | \& print "$x eq 9" if $x eq 3*3; | |
1794 | .Ve | |
1795 | .Sp | |
1796 | Additionally, the following still works: | |
1797 | .Sp | |
1798 | .Vb 3 | |
1799 | \& print "$x == 9" if $x == $y; | |
1800 | \& print "$x == 9" if $x == 9; | |
1801 | \& print "$x == 9" if $x == 3*3; | |
1802 | .Ve | |
1803 | .Sp | |
1804 | There is now a \f(CW\*(C`bsstr()\*(C'\fR method to get the string in scientific notation aka | |
1805 | \&\f(CW1e+2\fR instead of \f(CW100\fR. Be advised that overloaded 'eq' always uses \fIbstr()\fR | |
1806 | for comparisation, but Perl will represent some numbers as 100 and others | |
1807 | as 1e+308. If in doubt, convert both arguments to Math::BigInt before | |
1808 | comparing them as strings: | |
1809 | .Sp | |
1810 | .Vb 3 | |
1811 | \& use Test; | |
1812 | \& BEGIN { plan tests => 3 } | |
1813 | \& use Math::BigInt; | |
1814 | .Ve | |
1815 | .Sp | |
1816 | .Vb 5 | |
1817 | \& $x = Math::BigInt->new('1e56'); $y = 1e56; | |
1818 | \& ok ($x,$y); # will fail | |
1819 | \& ok ($x->bsstr(),$y); # okay | |
1820 | \& $y = Math::BigInt->new($y); | |
1821 | \& ok ($x,$y); # okay | |
1822 | .Ve | |
1823 | .Sp | |
1824 | Alternatively, simple use \f(CW\*(C`<=>\*(C'\fR for comparisations, this will get it | |
1825 | always right. There is not yet a way to get a number automatically represented | |
1826 | as a string that matches exactly the way Perl represents it. | |
1827 | .Sp | |
1828 | See also the section about \*(L"Infinity and Not a Number\*(R" for problems in | |
1829 | comparing NaNs. | |
1830 | .IP "\fIint()\fR" 1 | |
1831 | .IX Item "int()" | |
1832 | \&\f(CW\*(C`int()\*(C'\fR will return (at least for Perl v5.7.1 and up) another BigInt, not a | |
1833 | Perl scalar: | |
1834 | .Sp | |
1835 | .Vb 4 | |
1836 | \& $x = Math::BigInt->new(123); | |
1837 | \& $y = int($x); # BigInt 123 | |
1838 | \& $x = Math::BigFloat->new(123.45); | |
1839 | \& $y = int($x); # BigInt 123 | |
1840 | .Ve | |
1841 | .Sp | |
1842 | In all Perl versions you can use \f(CW\*(C`as_number()\*(C'\fR or \f(CW\*(C`as_int\*(C'\fR for the same | |
1843 | effect: | |
1844 | .Sp | |
1845 | .Vb 3 | |
1846 | \& $x = Math::BigFloat->new(123.45); | |
1847 | \& $y = $x->as_number(); # BigInt 123 | |
1848 | \& $y = $x->as_int(); # ditto | |
1849 | .Ve | |
1850 | .Sp | |
1851 | This also works for other subclasses, like Math::String. | |
1852 | .Sp | |
1853 | It is yet unlcear whether overloaded \fIint()\fR should return a scalar or a BigInt. | |
1854 | .Sp | |
1855 | If you want a real Perl scalar, use \f(CW\*(C`numify()\*(C'\fR: | |
1856 | .Sp | |
1857 | .Vb 1 | |
1858 | \& $y = $x->numify(); # 123 as scalar | |
1859 | .Ve | |
1860 | .Sp | |
1861 | This is seldom necessary, though, because this is done automatically, like | |
1862 | when you access an array: | |
1863 | .Sp | |
1864 | .Vb 1 | |
1865 | \& $z = $array[$x]; # does work automatically | |
1866 | .Ve | |
1867 | .IP "length" 1 | |
1868 | .IX Item "length" | |
1869 | The following will probably not do what you expect: | |
1870 | .Sp | |
1871 | .Vb 2 | |
1872 | \& $c = Math::BigInt->new(123); | |
1873 | \& print $c->length(),"\en"; # prints 30 | |
1874 | .Ve | |
1875 | .Sp | |
1876 | It prints both the number of digits in the number and in the fraction part | |
1877 | since print calls \f(CW\*(C`length()\*(C'\fR in list context. Use something like: | |
1878 | .Sp | |
1879 | .Vb 1 | |
1880 | \& print scalar $c->length(),"\en"; # prints 3 | |
1881 | .Ve | |
1882 | .IP "bdiv" 1 | |
1883 | .IX Item "bdiv" | |
1884 | The following will probably not do what you expect: | |
1885 | .Sp | |
1886 | .Vb 1 | |
1887 | \& print $c->bdiv(10000),"\en"; | |
1888 | .Ve | |
1889 | .Sp | |
1890 | It prints both quotient and remainder since print calls \f(CW\*(C`bdiv()\*(C'\fR in list | |
1891 | context. Also, \f(CW\*(C`bdiv()\*(C'\fR will modify \f(CW$c\fR, so be carefull. You probably want | |
1892 | to use | |
1893 | .Sp | |
1894 | .Vb 2 | |
1895 | \& print $c / 10000,"\en"; | |
1896 | \& print scalar $c->bdiv(10000),"\en"; # or if you want to modify $c | |
1897 | .Ve | |
1898 | .Sp | |
1899 | instead. | |
1900 | .Sp | |
1901 | The quotient is always the greatest integer less than or equal to the | |
1902 | real-valued quotient of the two operands, and the remainder (when it is | |
1903 | nonzero) always has the same sign as the second operand; so, for | |
1904 | example, | |
1905 | .Sp | |
1906 | .Vb 6 | |
1907 | \& 1 / 4 => ( 0, 1) | |
1908 | \& 1 / -4 => (-1,-3) | |
1909 | \& -3 / 4 => (-1, 1) | |
1910 | \& -3 / -4 => ( 0,-3) | |
1911 | \& -11 / 2 => (-5,1) | |
1912 | \& 11 /-2 => (-5,-1) | |
1913 | .Ve | |
1914 | .Sp | |
1915 | As a consequence, the behavior of the operator % agrees with the | |
1916 | behavior of Perl's built-in % operator (as documented in the perlop | |
1917 | manpage), and the equation | |
1918 | .Sp | |
1919 | .Vb 1 | |
1920 | \& $x == ($x / $y) * $y + ($x % $y) | |
1921 | .Ve | |
1922 | .Sp | |
1923 | holds true for any \f(CW$x\fR and \f(CW$y\fR, which justifies calling the two return | |
1924 | values of \fIbdiv()\fR the quotient and remainder. The only exception to this rule | |
1925 | are when \f(CW$y\fR == 0 and \f(CW$x\fR is negative, then the remainder will also be | |
1926 | negative. See below under \*(L"infinity handling\*(R" for the reasoning behing this. | |
1927 | .Sp | |
1928 | Perl's 'use integer;' changes the behaviour of % and / for scalars, but will | |
1929 | not change BigInt's way to do things. This is because under 'use integer' Perl | |
1930 | will do what the underlying C thinks is right and this is different for each | |
1931 | system. If you need BigInt's behaving exactly like Perl's 'use integer', bug | |
1932 | the author to implement it ;) | |
1933 | .IP "infinity handling" 1 | |
1934 | .IX Item "infinity handling" | |
1935 | Here are some examples that explain the reasons why certain results occur while | |
1936 | handling infinity: | |
1937 | .Sp | |
1938 | The following table shows the result of the division and the remainder, so that | |
1939 | the equation above holds true. Some \*(L"ordinary\*(R" cases are strewn in to show more | |
1940 | clearly the reasoning: | |
1941 | .Sp | |
1942 | .Vb 23 | |
1943 | \& A / B = C, R so that C * B + R = A | |
1944 | \& ========================================================= | |
1945 | \& 5 / 8 = 0, 5 0 * 8 + 5 = 5 | |
1946 | \& 0 / 8 = 0, 0 0 * 8 + 0 = 0 | |
1947 | \& 0 / inf = 0, 0 0 * inf + 0 = 0 | |
1948 | \& 0 /-inf = 0, 0 0 * -inf + 0 = 0 | |
1949 | \& 5 / inf = 0, 5 0 * inf + 5 = 5 | |
1950 | \& 5 /-inf = 0, 5 0 * -inf + 5 = 5 | |
1951 | \& -5/ inf = 0, -5 0 * inf + -5 = -5 | |
1952 | \& -5/-inf = 0, -5 0 * -inf + -5 = -5 | |
1953 | \& inf/ 5 = inf, 0 inf * 5 + 0 = inf | |
1954 | \& -inf/ 5 = -inf, 0 -inf * 5 + 0 = -inf | |
1955 | \& inf/ -5 = -inf, 0 -inf * -5 + 0 = inf | |
1956 | \& -inf/ -5 = inf, 0 inf * -5 + 0 = -inf | |
1957 | \& 5/ 5 = 1, 0 1 * 5 + 0 = 5 | |
1958 | \& -5/ -5 = 1, 0 1 * -5 + 0 = -5 | |
1959 | \& inf/ inf = 1, 0 1 * inf + 0 = inf | |
1960 | \& -inf/-inf = 1, 0 1 * -inf + 0 = -inf | |
1961 | \& inf/-inf = -1, 0 -1 * -inf + 0 = inf | |
1962 | \& -inf/ inf = -1, 0 1 * -inf + 0 = -inf | |
1963 | \& 8/ 0 = inf, 8 inf * 0 + 8 = 8 | |
1964 | \& inf/ 0 = inf, inf inf * 0 + inf = inf | |
1965 | \& 0/ 0 = NaN | |
1966 | .Ve | |
1967 | .Sp | |
1968 | These cases below violate the \*(L"remainder has the sign of the second of the two | |
1969 | arguments\*(R", since they wouldn't match up otherwise. | |
1970 | .Sp | |
1971 | .Vb 4 | |
1972 | \& A / B = C, R so that C * B + R = A | |
1973 | \& ======================================================== | |
1974 | \& -inf/ 0 = -inf, -inf -inf * 0 + inf = -inf | |
1975 | \& -8/ 0 = -inf, -8 -inf * 0 + 8 = -8 | |
1976 | .Ve | |
1977 | .IP "Modifying and =" 1 | |
1978 | .IX Item "Modifying and =" | |
1979 | Beware of: | |
1980 | .Sp | |
1981 | .Vb 2 | |
1982 | \& $x = Math::BigFloat->new(5); | |
1983 | \& $y = $x; | |
1984 | .Ve | |
1985 | .Sp | |
1986 | It will not do what you think, e.g. making a copy of \f(CW$x\fR. Instead it just makes | |
1987 | a second reference to the \fBsame\fR object and stores it in \f(CW$y\fR. Thus anything | |
1988 | that modifies \f(CW$x\fR (except overloaded operators) will modify \f(CW$y\fR, and vice versa. | |
1989 | Or in other words, \f(CW\*(C`=\*(C'\fR is only safe if you modify your BigInts only via | |
1990 | overloaded math. As soon as you use a method call it breaks: | |
1991 | .Sp | |
1992 | .Vb 2 | |
1993 | \& $x->bmul(2); | |
1994 | \& print "$x, $y\en"; # prints '10, 10' | |
1995 | .Ve | |
1996 | .Sp | |
1997 | If you want a true copy of \f(CW$x\fR, use: | |
1998 | .Sp | |
1999 | .Vb 1 | |
2000 | \& $y = $x->copy(); | |
2001 | .Ve | |
2002 | .Sp | |
2003 | You can also chain the calls like this, this will make first a copy and then | |
2004 | multiply it by 2: | |
2005 | .Sp | |
2006 | .Vb 1 | |
2007 | \& $y = $x->copy()->bmul(2); | |
2008 | .Ve | |
2009 | .Sp | |
2010 | See also the documentation for overload.pm regarding \f(CW\*(C`=\*(C'\fR. | |
2011 | .IP "bpow" 1 | |
2012 | .IX Item "bpow" | |
2013 | \&\f(CW\*(C`bpow()\*(C'\fR (and the rounding functions) now modifies the first argument and | |
2014 | returns it, unlike the old code which left it alone and only returned the | |
2015 | result. This is to be consistent with \f(CW\*(C`badd()\*(C'\fR etc. The first three will | |
2016 | modify \f(CW$x\fR, the last one won't: | |
2017 | .Sp | |
2018 | .Vb 4 | |
2019 | \& print bpow($x,$i),"\en"; # modify $x | |
2020 | \& print $x->bpow($i),"\en"; # ditto | |
2021 | \& print $x **= $i,"\en"; # the same | |
2022 | \& print $x ** $i,"\en"; # leave $x alone | |
2023 | .Ve | |
2024 | .Sp | |
2025 | The form \f(CW\*(C`$x **= $y\*(C'\fR is faster than \f(CW\*(C`$x = $x ** $y;\*(C'\fR, though. | |
2026 | .IP "Overloading \-$x" 1 | |
2027 | .IX Item "Overloading -$x" | |
2028 | The following: | |
2029 | .Sp | |
2030 | .Vb 1 | |
2031 | \& $x = -$x; | |
2032 | .Ve | |
2033 | .Sp | |
2034 | is slower than | |
2035 | .Sp | |
2036 | .Vb 1 | |
2037 | \& $x->bneg(); | |
2038 | .Ve | |
2039 | .Sp | |
2040 | since overload calls \f(CW\*(C`sub($x,0,1);\*(C'\fR instead of \f(CW\*(C`neg($x)\*(C'\fR. The first variant | |
2041 | needs to preserve \f(CW$x\fR since it does not know that it later will get overwritten. | |
2042 | This makes a copy of \f(CW$x\fR and takes O(N), but \f(CW$x\fR\->\fIbneg()\fR is O(1). | |
2043 | .IP "Mixing different object types" 1 | |
2044 | .IX Item "Mixing different object types" | |
2045 | In Perl you will get a floating point value if you do one of the following: | |
2046 | .Sp | |
2047 | .Vb 3 | |
2048 | \& $float = 5.0 + 2; | |
2049 | \& $float = 2 + 5.0; | |
2050 | \& $float = 5 / 2; | |
2051 | .Ve | |
2052 | .Sp | |
2053 | With overloaded math, only the first two variants will result in a BigFloat: | |
2054 | .Sp | |
2055 | .Vb 2 | |
2056 | \& use Math::BigInt; | |
2057 | \& use Math::BigFloat; | |
2058 | .Ve | |
2059 | .Sp | |
2060 | .Vb 3 | |
2061 | \& $mbf = Math::BigFloat->new(5); | |
2062 | \& $mbi2 = Math::BigInteger->new(5); | |
2063 | \& $mbi = Math::BigInteger->new(2); | |
2064 | .Ve | |
2065 | .Sp | |
2066 | .Vb 6 | |
2067 | \& # what actually gets called: | |
2068 | \& $float = $mbf + $mbi; # $mbf->badd() | |
2069 | \& $float = $mbf / $mbi; # $mbf->bdiv() | |
2070 | \& $integer = $mbi + $mbf; # $mbi->badd() | |
2071 | \& $integer = $mbi2 / $mbi; # $mbi2->bdiv() | |
2072 | \& $integer = $mbi2 / $mbf; # $mbi2->bdiv() | |
2073 | .Ve | |
2074 | .Sp | |
2075 | This is because math with overloaded operators follows the first (dominating) | |
2076 | operand, and the operation of that is called and returns thus the result. So, | |
2077 | \&\fIMath::BigInt::bdiv()\fR will always return a Math::BigInt, regardless whether | |
2078 | the result should be a Math::BigFloat or the second operant is one. | |
2079 | .Sp | |
2080 | To get a Math::BigFloat you either need to call the operation manually, | |
2081 | make sure the operands are already of the proper type or casted to that type | |
2082 | via Math::BigFloat\->\fInew()\fR: | |
2083 | .Sp | |
2084 | .Vb 1 | |
2085 | \& $float = Math::BigFloat->new($mbi2) / $mbi; # = 2.5 | |
2086 | .Ve | |
2087 | .Sp | |
2088 | Beware of simple \*(L"casting\*(R" the entire expression, this would only convert | |
2089 | the already computed result: | |
2090 | .Sp | |
2091 | .Vb 1 | |
2092 | \& $float = Math::BigFloat->new($mbi2 / $mbi); # = 2.0 thus wrong! | |
2093 | .Ve | |
2094 | .Sp | |
2095 | Beware also of the order of more complicated expressions like: | |
2096 | .Sp | |
2097 | .Vb 2 | |
2098 | \& $integer = ($mbi2 + $mbi) / $mbf; # int / float => int | |
2099 | \& $integer = $mbi2 / Math::BigFloat->new($mbi); # ditto | |
2100 | .Ve | |
2101 | .Sp | |
2102 | If in doubt, break the expression into simpler terms, or cast all operands | |
2103 | to the desired resulting type. | |
2104 | .Sp | |
2105 | Scalar values are a bit different, since: | |
2106 | .Sp | |
2107 | .Vb 2 | |
2108 | \& $float = 2 + $mbf; | |
2109 | \& $float = $mbf + 2; | |
2110 | .Ve | |
2111 | .Sp | |
2112 | will both result in the proper type due to the way the overloaded math works. | |
2113 | .Sp | |
2114 | This section also applies to other overloaded math packages, like Math::String. | |
2115 | .Sp | |
2116 | One solution to you problem might be autoupgrading|upgrading. See the | |
2117 | pragmas bignum, bigint and bigrat for an easy way to do this. | |
2118 | .IP "\fIbsqrt()\fR" 1 | |
2119 | .IX Item "bsqrt()" | |
2120 | \&\f(CW\*(C`bsqrt()\*(C'\fR works only good if the result is a big integer, e.g. the square | |
2121 | root of 144 is 12, but from 12 the square root is 3, regardless of rounding | |
2122 | mode. The reason is that the result is always truncated to an integer. | |
2123 | .Sp | |
2124 | If you want a better approximation of the square root, then use: | |
2125 | .Sp | |
2126 | .Vb 4 | |
2127 | \& $x = Math::BigFloat->new(12); | |
2128 | \& Math::BigFloat->precision(0); | |
2129 | \& Math::BigFloat->round_mode('even'); | |
2130 | \& print $x->copy->bsqrt(),"\en"; # 4 | |
2131 | .Ve | |
2132 | .Sp | |
2133 | .Vb 3 | |
2134 | \& Math::BigFloat->precision(2); | |
2135 | \& print $x->bsqrt(),"\en"; # 3.46 | |
2136 | \& print $x->bsqrt(3),"\en"; # 3.464 | |
2137 | .Ve | |
2138 | .IP "\fIbrsft()\fR" 1 | |
2139 | .IX Item "brsft()" | |
2140 | For negative numbers in base see also brsft. | |
2141 | .SH "LICENSE" | |
2142 | .IX Header "LICENSE" | |
2143 | This program is free software; you may redistribute it and/or modify it under | |
2144 | the same terms as Perl itself. | |
2145 | .SH "SEE ALSO" | |
2146 | .IX Header "SEE ALSO" | |
2147 | Math::BigFloat, Math::BigRat and Math::Big as well as | |
2148 | Math::BigInt::BitVect, Math::BigInt::Pari and Math::BigInt::GMP. | |
2149 | .PP | |
2150 | The pragmas bignum, bigint and bigrat also might be of interest | |
2151 | because they solve the autoupgrading/downgrading issue, at least partly. | |
2152 | .PP | |
2153 | The package at | |
2154 | <http://search.cpan.org/search?mode=module&query=Math%3A%3ABigInt> contains | |
2155 | more documentation including a full version history, testcases, empty | |
2156 | subclass files and benchmarks. | |
2157 | .SH "AUTHORS" | |
2158 | .IX Header "AUTHORS" | |
2159 | Original code by Mark Biggar, overloaded interface by Ilya Zakharevich. | |
2160 | Completely rewritten by Tels http://bloodgate.com in late 2000, 2001 \- 2004 | |
2161 | and still at it in 2005. | |
2162 | .PP | |
2163 | Many people contributed in one or more ways to the final beast, see the file | |
2164 | \&\s-1CREDITS\s0 for an (uncomplete) list. If you miss your name, please drop me a | |
2165 | mail. Thank you! |