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::BigFloat 3" | |
132 | .TH Math::BigFloat 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | Math::BigFloat \- Arbitrary size floating point math package | |
135 | .SH "SYNOPSIS" | |
136 | .IX Header "SYNOPSIS" | |
137 | .Vb 1 | |
138 | \& use Math::BigFloat; | |
139 | .Ve | |
140 | .PP | |
141 | .Vb 8 | |
142 | \& # Number creation | |
143 | \& $x = Math::BigFloat->new($str); # defaults to 0 | |
144 | \& $nan = Math::BigFloat->bnan(); # create a NotANumber | |
145 | \& $zero = Math::BigFloat->bzero(); # create a +0 | |
146 | \& $inf = Math::BigFloat->binf(); # create a +inf | |
147 | \& $inf = Math::BigFloat->binf('-'); # create a -inf | |
148 | \& $one = Math::BigFloat->bone(); # create a +1 | |
149 | \& $one = Math::BigFloat->bone('-'); # create a -1 | |
150 | .Ve | |
151 | .PP | |
152 | .Vb 10 | |
153 | \& # Testing | |
154 | \& $x->is_zero(); # true if arg is +0 | |
155 | \& $x->is_nan(); # true if arg is NaN | |
156 | \& $x->is_one(); # true if arg is +1 | |
157 | \& $x->is_one('-'); # true if arg is -1 | |
158 | \& $x->is_odd(); # true if odd, false for even | |
159 | \& $x->is_even(); # true if even, false for odd | |
160 | \& $x->is_pos(); # true if >= 0 | |
161 | \& $x->is_neg(); # true if < 0 | |
162 | \& $x->is_inf(sign); # true if +inf, or -inf (default is '+') | |
163 | .Ve | |
164 | .PP | |
165 | .Vb 5 | |
166 | \& $x->bcmp($y); # compare numbers (undef,<0,=0,>0) | |
167 | \& $x->bacmp($y); # compare absolutely (undef,<0,=0,>0) | |
168 | \& $x->sign(); # return the sign, either +,- or NaN | |
169 | \& $x->digit($n); # return the nth digit, counting from right | |
170 | \& $x->digit(-$n); # return the nth digit, counting from left | |
171 | .Ve | |
172 | .PP | |
173 | .Vb 3 | |
174 | \& # The following all modify their first argument. If you want to preserve | |
175 | \& # $x, use $z = $x->copy()->bXXX($y); See under L<CAVEATS> for why this is | |
176 | \& # neccessary when mixing $a = $b assigments with non-overloaded math. | |
177 | .Ve | |
178 | .PP | |
179 | .Vb 7 | |
180 | \& # set | |
181 | \& $x->bzero(); # set $i to 0 | |
182 | \& $x->bnan(); # set $i to NaN | |
183 | \& $x->bone(); # set $x to +1 | |
184 | \& $x->bone('-'); # set $x to -1 | |
185 | \& $x->binf(); # set $x to inf | |
186 | \& $x->binf('-'); # set $x to -inf | |
187 | .Ve | |
188 | .PP | |
189 | .Vb 6 | |
190 | \& $x->bneg(); # negation | |
191 | \& $x->babs(); # absolute value | |
192 | \& $x->bnorm(); # normalize (no-op) | |
193 | \& $x->bnot(); # two's complement (bit wise not) | |
194 | \& $x->binc(); # increment x by 1 | |
195 | \& $x->bdec(); # decrement x by 1 | |
196 | .Ve | |
197 | .PP | |
198 | .Vb 5 | |
199 | \& $x->badd($y); # addition (add $y to $x) | |
200 | \& $x->bsub($y); # subtraction (subtract $y from $x) | |
201 | \& $x->bmul($y); # multiplication (multiply $x by $y) | |
202 | \& $x->bdiv($y); # divide, set $x to quotient | |
203 | \& # return (quo,rem) or quo if scalar | |
204 | .Ve | |
205 | .PP | |
206 | .Vb 5 | |
207 | \& $x->bmod($y); # modulus ($x % $y) | |
208 | \& $x->bpow($y); # power of arguments ($x ** $y) | |
209 | \& $x->blsft($y); # left shift | |
210 | \& $x->brsft($y); # right shift | |
211 | \& # return (quo,rem) or quo if scalar | |
212 | .Ve | |
213 | .PP | |
214 | .Vb 2 | |
215 | \& $x->blog(); # logarithm of $x to base e (Euler's number) | |
216 | \& $x->blog($base); # logarithm of $x to base $base (f.i. 2) | |
217 | .Ve | |
218 | .PP | |
219 | .Vb 4 | |
220 | \& $x->band($y); # bit-wise and | |
221 | \& $x->bior($y); # bit-wise inclusive or | |
222 | \& $x->bxor($y); # bit-wise exclusive or | |
223 | \& $x->bnot(); # bit-wise not (two's complement) | |
224 | .Ve | |
225 | .PP | |
226 | .Vb 3 | |
227 | \& $x->bsqrt(); # calculate square-root | |
228 | \& $x->broot($y); # $y'th root of $x (e.g. $y == 3 => cubic root) | |
229 | \& $x->bfac(); # factorial of $x (1*2*3*4*..$x) | |
230 | .Ve | |
231 | .PP | |
232 | .Vb 2 | |
233 | \& $x->bround($N); # accuracy: preserve $N digits | |
234 | \& $x->bfround($N); # precision: round to the $Nth digit | |
235 | .Ve | |
236 | .PP | |
237 | .Vb 2 | |
238 | \& $x->bfloor(); # return integer less or equal than $x | |
239 | \& $x->bceil(); # return integer greater or equal than $x | |
240 | .Ve | |
241 | .PP | |
242 | .Vb 1 | |
243 | \& # The following do not modify their arguments: | |
244 | .Ve | |
245 | .PP | |
246 | .Vb 2 | |
247 | \& bgcd(@values); # greatest common divisor | |
248 | \& blcm(@values); # lowest common multiplicator | |
249 | .Ve | |
250 | .PP | |
251 | .Vb 2 | |
252 | \& $x->bstr(); # return string | |
253 | \& $x->bsstr(); # return string in scientific notation | |
254 | .Ve | |
255 | .PP | |
256 | .Vb 4 | |
257 | \& $x->as_int(); # return $x as BigInt | |
258 | \& $x->exponent(); # return exponent as BigInt | |
259 | \& $x->mantissa(); # return mantissa as BigInt | |
260 | \& $x->parts(); # return (mantissa,exponent) as BigInt | |
261 | .Ve | |
262 | .PP | |
263 | .Vb 2 | |
264 | \& $x->length(); # number of digits (w/o sign and '.') | |
265 | \& ($l,$f) = $x->length(); # number of digits, and length of fraction | |
266 | .Ve | |
267 | .PP | |
268 | .Vb 4 | |
269 | \& $x->precision(); # return P of $x (or global, if P of $x undef) | |
270 | \& $x->precision($n); # set P of $x to $n | |
271 | \& $x->accuracy(); # return A of $x (or global, if A of $x undef) | |
272 | \& $x->accuracy($n); # set A $x to $n | |
273 | .Ve | |
274 | .PP | |
275 | .Vb 4 | |
276 | \& # these get/set the appropriate global value for all BigFloat objects | |
277 | \& Math::BigFloat->precision(); # Precision | |
278 | \& Math::BigFloat->accuracy(); # Accuracy | |
279 | \& Math::BigFloat->round_mode(); # rounding mode | |
280 | .Ve | |
281 | .SH "DESCRIPTION" | |
282 | .IX Header "DESCRIPTION" | |
283 | All operators (inlcuding basic math operations) are overloaded if you | |
284 | declare your big floating point numbers as | |
285 | .PP | |
286 | .Vb 1 | |
287 | \& $i = new Math::BigFloat '12_3.456_789_123_456_789E-2'; | |
288 | .Ve | |
289 | .PP | |
290 | Operations with overloaded operators preserve the arguments, which is | |
291 | exactly what you expect. | |
292 | .Sh "Canonical notation" | |
293 | .IX Subsection "Canonical notation" | |
294 | Input to these routines are either BigFloat objects, or strings of the | |
295 | following four forms: | |
296 | .IP "\(bu" 2 | |
297 | \&\f(CW\*(C`/^[+\-]\ed+$/\*(C'\fR | |
298 | .IP "\(bu" 2 | |
299 | \&\f(CW\*(C`/^[+\-]\ed+\e.\ed*$/\*(C'\fR | |
300 | .IP "\(bu" 2 | |
301 | \&\f(CW\*(C`/^[+\-]\ed+E[+\-]?\ed+$/\*(C'\fR | |
302 | .IP "\(bu" 2 | |
303 | \&\f(CW\*(C`/^[+\-]\ed*\e.\ed+E[+\-]?\ed+$/\*(C'\fR | |
304 | .PP | |
305 | all with optional leading and trailing zeros and/or spaces. Additonally, | |
306 | numbers are allowed to have an underscore between any two digits. | |
307 | .PP | |
308 | Empty strings as well as other illegal numbers results in 'NaN'. | |
309 | .PP | |
310 | \&\fIbnorm()\fR on a BigFloat object is now effectively a no\-op, since the numbers | |
311 | are always stored in normalized form. On a string, it creates a BigFloat | |
312 | object. | |
313 | .Sh "Output" | |
314 | .IX Subsection "Output" | |
315 | Output values are BigFloat objects (normalized), except for \fIbstr()\fR and \fIbsstr()\fR. | |
316 | .PP | |
317 | The string output will always have leading and trailing zeros stripped and drop | |
318 | a plus sign. \f(CW\*(C`bstr()\*(C'\fR will give you always the form with a decimal point, | |
319 | while \f(CW\*(C`bsstr()\*(C'\fR (s for scientific) gives you the scientific notation. | |
320 | .PP | |
321 | .Vb 6 | |
322 | \& Input bstr() bsstr() | |
323 | \& '-0' '0' '0E1' | |
324 | \& ' -123 123 123' '-123123123' '-123123123E0' | |
325 | \& '00.0123' '0.0123' '123E-4' | |
326 | \& '123.45E-2' '1.2345' '12345E-4' | |
327 | \& '10E+3' '10000' '1E4' | |
328 | .Ve | |
329 | .PP | |
330 | 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, | |
331 | \&\f(CW\*(C`is_nan()\*(C'\fR) return true or false, while others (\f(CW\*(C`bcmp()\*(C'\fR, \f(CW\*(C`bacmp()\*(C'\fR) | |
332 | return either undef, <0, 0 or >0 and are suited for sort. | |
333 | .PP | |
334 | Actual math is done by using the class defined with \f(CW\*(C`with =\*(C'\fR Class;> (which | |
335 | defaults to BigInts) to represent the mantissa and exponent. | |
336 | .PP | |
337 | The sign \f(CW\*(C`/^[+\-]$/\*(C'\fR is stored separately. The string 'NaN' is used to | |
338 | represent the result when input arguments are not numbers, as well as | |
339 | the result of dividing by zero. | |
340 | .ie n .Sh """mantissa()""\fP, \f(CW""exponent()""\fP and \f(CW""parts()""" | |
341 | .el .Sh "\f(CWmantissa()\fP, \f(CWexponent()\fP and \f(CWparts()\fP" | |
342 | .IX Subsection "mantissa(), exponent() and parts()" | |
343 | \&\f(CW\*(C`mantissa()\*(C'\fR and \f(CW\*(C`exponent()\*(C'\fR return the said parts of the BigFloat | |
344 | as BigInts such that: | |
345 | .PP | |
346 | .Vb 4 | |
347 | \& $m = $x->mantissa(); | |
348 | \& $e = $x->exponent(); | |
349 | \& $y = $m * ( 10 ** $e ); | |
350 | \& print "ok\en" if $x == $y; | |
351 | .Ve | |
352 | .PP | |
353 | \&\f(CW\*(C`($m,$e) = $x\->parts();\*(C'\fR is just a shortcut giving you both of them. | |
354 | .PP | |
355 | A zero is represented and returned as \f(CW0E1\fR, \fBnot\fR \f(CW0E0\fR (after Knuth). | |
356 | .PP | |
357 | Currently the mantissa is reduced as much as possible, favouring higher | |
358 | exponents over lower ones (e.g. returning 1e7 instead of 10e6 or 10000000e0). | |
359 | This might change in the future, so do not depend on it. | |
360 | .Sh "Accuracy vs. Precision" | |
361 | .IX Subsection "Accuracy vs. Precision" | |
362 | See also: Rounding. | |
363 | .PP | |
364 | Math::BigFloat supports both precision (rounding to a certain place before or | |
365 | after the dot) and accuracy (rounding to a certain number of digits). For a | |
366 | full documentation, examples and tips on these topics please see the large | |
367 | section about rounding in Math::BigInt. | |
368 | .PP | |
369 | Since things like \f(CWsqrt(2)\fR or \f(CW\*(C`1 / 3\*(C'\fR must presented with a limited | |
370 | accuracy lest a operation consumes all resources, each operation produces | |
371 | no more than the requested number of digits. | |
372 | .PP | |
373 | If there is no gloabl precision or accuracy set, \fBand\fR the operation in | |
374 | question was not called with a requested precision or accuracy, \fBand\fR the | |
375 | input \f(CW$x\fR has no accuracy or precision set, then a fallback parameter will | |
376 | be used. For historical reasons, it is called \f(CW\*(C`div_scale\*(C'\fR and can be accessed | |
377 | via: | |
378 | .PP | |
379 | .Vb 2 | |
380 | \& $d = Math::BigFloat->div_scale(); # query | |
381 | \& Math::BigFloat->div_scale($n); # set to $n digits | |
382 | .Ve | |
383 | .PP | |
384 | The default value for \f(CW\*(C`div_scale\*(C'\fR is 40. | |
385 | .PP | |
386 | In case the result of one operation has more digits than specified, | |
387 | it is rounded. The rounding mode taken is either the default mode, or the one | |
388 | supplied to the operation after the \fIscale\fR: | |
389 | .PP | |
390 | .Vb 7 | |
391 | \& $x = Math::BigFloat->new(2); | |
392 | \& Math::BigFloat->accuracy(5); # 5 digits max | |
393 | \& $y = $x->copy()->bdiv(3); # will give 0.66667 | |
394 | \& $y = $x->copy()->bdiv(3,6); # will give 0.666667 | |
395 | \& $y = $x->copy()->bdiv(3,6,undef,'odd'); # will give 0.666667 | |
396 | \& Math::BigFloat->round_mode('zero'); | |
397 | \& $y = $x->copy()->bdiv(3,6); # will also give 0.666667 | |
398 | .Ve | |
399 | .PP | |
400 | Note that \f(CW\*(C`Math::BigFloat\->accuracy()\*(C'\fR and \f(CW\*(C`Math::BigFloat\->precision()\*(C'\fR | |
401 | set the global variables, and thus \fBany\fR newly created number will be subject | |
402 | to the global rounding \fBimmidiately\fR. This means that in the examples above, the | |
403 | \&\f(CW3\fR as argument to \f(CW\*(C`bdiv()\*(C'\fR will also get an accuracy of \fB5\fR. | |
404 | .PP | |
405 | It is less confusing to either calculate the result fully, and afterwards | |
406 | round it explicitely, or use the additional parameters to the math | |
407 | functions like so: | |
408 | .PP | |
409 | .Vb 4 | |
410 | \& use Math::BigFloat; | |
411 | \& $x = Math::BigFloat->new(2); | |
412 | \& $y = $x->copy()->bdiv(3); | |
413 | \& print $y->bround(5),"\en"; # will give 0.66667 | |
414 | .Ve | |
415 | .PP | |
416 | .Vb 1 | |
417 | \& or | |
418 | .Ve | |
419 | .PP | |
420 | .Vb 4 | |
421 | \& use Math::BigFloat; | |
422 | \& $x = Math::BigFloat->new(2); | |
423 | \& $y = $x->copy()->bdiv(3,5); # will give 0.66667 | |
424 | \& print "$y\en"; | |
425 | .Ve | |
426 | .Sh "Rounding" | |
427 | .IX Subsection "Rounding" | |
428 | .IP "ffround ( +$scale )" 2 | |
429 | .IX Item "ffround ( +$scale )" | |
430 | Rounds to the \f(CW$scale\fR'th place left from the '.', counting from the dot. | |
431 | The first digit is numbered 1. | |
432 | .IP "ffround ( \-$scale )" 2 | |
433 | .IX Item "ffround ( -$scale )" | |
434 | Rounds to the \f(CW$scale\fR'th place right from the '.', counting from the dot. | |
435 | .IP "ffround ( 0 )" 2 | |
436 | .IX Item "ffround ( 0 )" | |
437 | Rounds to an integer. | |
438 | .IP "fround ( +$scale )" 2 | |
439 | .IX Item "fround ( +$scale )" | |
440 | Preserves accuracy to \f(CW$scale\fR digits from the left (aka significant digits) | |
441 | and pads the rest with zeros. If the number is between 1 and \-1, the | |
442 | significant digits count from the first non-zero after the '.' | |
443 | .IP "fround ( \-$scale ) and fround ( 0 )" 2 | |
444 | .IX Item "fround ( -$scale ) and fround ( 0 )" | |
445 | These are effectively no\-ops. | |
446 | .PP | |
447 | All rounding functions take as a second parameter a rounding mode from one of | |
448 | the following: 'even', 'odd', '+inf', '\-inf', 'zero' or 'trunc'. | |
449 | .PP | |
450 | The default rounding mode is 'even'. By using | |
451 | \&\f(CW\*(C`Math::BigFloat\->round_mode($round_mode);\*(C'\fR you can get and set the default | |
452 | mode for subsequent rounding. The usage of \f(CW\*(C`$Math::BigFloat::$round_mode\*(C'\fR is | |
453 | no longer supported. | |
454 | The second parameter to the round functions then overrides the default | |
455 | temporarily. | |
456 | .PP | |
457 | The \f(CW\*(C`as_number()\*(C'\fR function returns a BigInt from a Math::BigFloat. It uses | |
458 | \&'trunc' as rounding mode to make it equivalent to: | |
459 | .PP | |
460 | .Vb 2 | |
461 | \& $x = 2.5; | |
462 | \& $y = int($x) + 2; | |
463 | .Ve | |
464 | .PP | |
465 | You can override this by passing the desired rounding mode as parameter to | |
466 | \&\f(CW\*(C`as_number()\*(C'\fR: | |
467 | .PP | |
468 | .Vb 2 | |
469 | \& $x = Math::BigFloat->new(2.5); | |
470 | \& $y = $x->as_number('odd'); # $y = 3 | |
471 | .Ve | |
472 | .SH "METHODS" | |
473 | .IX Header "METHODS" | |
474 | .Sh "accuracy" | |
475 | .IX Subsection "accuracy" | |
476 | .Vb 3 | |
477 | \& $x->accuracy(5); # local for $x | |
478 | \& CLASS->accuracy(5); # global for all members of CLASS | |
479 | \& # Note: This also applies to new()! | |
480 | .Ve | |
481 | .PP | |
482 | .Vb 2 | |
483 | \& $A = $x->accuracy(); # read out accuracy that affects $x | |
484 | \& $A = CLASS->accuracy(); # read out global accuracy | |
485 | .Ve | |
486 | .PP | |
487 | Set or get the global or local accuracy, aka how many significant digits the | |
488 | results have. If you set a global accuracy, then this also applies to \fInew()\fR! | |
489 | .PP | |
490 | Warning! The accuracy \fIsticks\fR, e.g. once you created a number under the | |
491 | influence of \f(CW\*(C`CLASS\->accuracy($A)\*(C'\fR, all results from math operations with | |
492 | that number will also be rounded. | |
493 | .PP | |
494 | In most cases, you should probably round the results explicitely using one of | |
495 | \&\fIround()\fR, \fIbround()\fR or \fIbfround()\fR or by passing the desired accuracy | |
496 | to the math operation as additional parameter: | |
497 | .PP | |
498 | .Vb 4 | |
499 | \& my $x = Math::BigInt->new(30000); | |
500 | \& my $y = Math::BigInt->new(7); | |
501 | \& print scalar $x->copy()->bdiv($y, 2); # print 4300 | |
502 | \& print scalar $x->copy()->bdiv($y)->bround(2); # print 4300 | |
503 | .Ve | |
504 | .Sh "\fIprecision()\fP" | |
505 | .IX Subsection "precision()" | |
506 | .Vb 2 | |
507 | \& $x->precision(-2); # local for $x, round at the second digit right of the dot | |
508 | \& $x->precision(2); # ditto, round at the second digit left of the dot | |
509 | .Ve | |
510 | .PP | |
511 | .Vb 3 | |
512 | \& CLASS->precision(5); # Global for all members of CLASS | |
513 | \& # This also applies to new()! | |
514 | \& CLASS->precision(-5); # ditto | |
515 | .Ve | |
516 | .PP | |
517 | .Vb 2 | |
518 | \& $P = CLASS->precision(); # read out global precision | |
519 | \& $P = $x->precision(); # read out precision that affects $x | |
520 | .Ve | |
521 | .PP | |
522 | Note: You probably want to use \fIaccuracy()\fR instead. With accuracy you | |
523 | set the number of digits each result should have, with precision you | |
524 | set the place where to round! | |
525 | .SH "Autocreating constants" | |
526 | .IX Header "Autocreating constants" | |
527 | After \f(CW\*(C`use Math::BigFloat ':constant'\*(C'\fR all the floating point constants | |
528 | in the given scope are converted to \f(CW\*(C`Math::BigFloat\*(C'\fR. This conversion | |
529 | happens at compile time. | |
530 | .PP | |
531 | In particular | |
532 | .PP | |
533 | .Vb 1 | |
534 | \& perl -MMath::BigFloat=:constant -e 'print 2E-100,"\en"' | |
535 | .Ve | |
536 | .PP | |
537 | prints the value of \f(CW\*(C`2E\-100\*(C'\fR. Note that without conversion of | |
538 | constants the expression 2E\-100 will be calculated as normal floating point | |
539 | number. | |
540 | .PP | |
541 | Please note that ':constant' does not affect integer constants, nor binary | |
542 | nor hexadecimal constants. Use bignum or Math::BigInt to get this to | |
543 | work. | |
544 | .Sh "Math library" | |
545 | .IX Subsection "Math library" | |
546 | Math with the numbers is done (by default) by a module called | |
547 | Math::BigInt::Calc. This is equivalent to saying: | |
548 | .PP | |
549 | .Vb 1 | |
550 | \& use Math::BigFloat lib => 'Calc'; | |
551 | .Ve | |
552 | .PP | |
553 | You can change this by using: | |
554 | .PP | |
555 | .Vb 1 | |
556 | \& use Math::BigFloat lib => 'BitVect'; | |
557 | .Ve | |
558 | .PP | |
559 | The following would first try to find Math::BigInt::Foo, then | |
560 | Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc: | |
561 | .PP | |
562 | .Vb 1 | |
563 | \& use Math::BigFloat lib => 'Foo,Math::BigInt::Bar'; | |
564 | .Ve | |
565 | .PP | |
566 | Calc.pm uses as internal format an array of elements of some decimal base | |
567 | (usually 1e7, but this might be differen for some systems) with the least | |
568 | significant digit first, while BitVect.pm uses a bit vector of base 2, most | |
569 | significant bit first. Other modules might use even different means of | |
570 | representing the numbers. See the respective module documentation for further | |
571 | details. | |
572 | .PP | |
573 | Please note that Math::BigFloat does \fBnot\fR use the denoted library itself, | |
574 | but it merely passes the lib argument to Math::BigInt. So, instead of the need | |
575 | to do: | |
576 | .PP | |
577 | .Vb 2 | |
578 | \& use Math::BigInt lib => 'GMP'; | |
579 | \& use Math::BigFloat; | |
580 | .Ve | |
581 | .PP | |
582 | you can roll it all into one line: | |
583 | .PP | |
584 | .Vb 1 | |
585 | \& use Math::BigFloat lib => 'GMP'; | |
586 | .Ve | |
587 | .PP | |
588 | It is also possible to just require Math::BigFloat: | |
589 | .PP | |
590 | .Vb 1 | |
591 | \& require Math::BigFloat; | |
592 | .Ve | |
593 | .PP | |
594 | This will load the neccessary things (like BigInt) when they are needed, and | |
595 | automatically. | |
596 | .PP | |
597 | Use the lib, Luke! And see \*(L"Using Math::BigInt::Lite\*(R" for more details than | |
598 | you ever wanted to know about loading a different library. | |
599 | .Sh "Using Math::BigInt::Lite" | |
600 | .IX Subsection "Using Math::BigInt::Lite" | |
601 | It is possible to use Math::BigInt::Lite with Math::BigFloat: | |
602 | .PP | |
603 | .Vb 2 | |
604 | \& # 1 | |
605 | \& use Math::BigFloat with => 'Math::BigInt::Lite'; | |
606 | .Ve | |
607 | .PP | |
608 | There is no need to \*(L"use Math::BigInt\*(R" or \*(L"use Math::BigInt::Lite\*(R", but you | |
609 | can combine these if you want. For instance, you may want to use | |
610 | Math::BigInt objects in your main script, too. | |
611 | .PP | |
612 | .Vb 3 | |
613 | \& # 2 | |
614 | \& use Math::BigInt; | |
615 | \& use Math::BigFloat with => 'Math::BigInt::Lite'; | |
616 | .Ve | |
617 | .PP | |
618 | Of course, you can combine this with the \f(CW\*(C`lib\*(C'\fR parameter. | |
619 | .PP | |
620 | .Vb 2 | |
621 | \& # 3 | |
622 | \& use Math::BigFloat with => 'Math::BigInt::Lite', lib => 'GMP,Pari'; | |
623 | .Ve | |
624 | .PP | |
625 | There is no need for a \*(L"use Math::BigInt;\*(R" statement, even if you want to | |
626 | use Math::BigInt's, since Math::BigFloat will needs Math::BigInt and thus | |
627 | always loads it. But if you add it, add it \fBbefore\fR: | |
628 | .PP | |
629 | .Vb 3 | |
630 | \& # 4 | |
631 | \& use Math::BigInt; | |
632 | \& use Math::BigFloat with => 'Math::BigInt::Lite', lib => 'GMP,Pari'; | |
633 | .Ve | |
634 | .PP | |
635 | Notice that the module with the last \f(CW\*(C`lib\*(C'\fR will \*(L"win\*(R" and thus | |
636 | it's lib will be used if the lib is available: | |
637 | .PP | |
638 | .Vb 3 | |
639 | \& # 5 | |
640 | \& use Math::BigInt lib => 'Bar,Baz'; | |
641 | \& use Math::BigFloat with => 'Math::BigInt::Lite', lib => 'Foo'; | |
642 | .Ve | |
643 | .PP | |
644 | That would try to load Foo, Bar, Baz and Calc (in that order). Or in other | |
645 | words, Math::BigFloat will try to retain previously loaded libs when you | |
646 | don't specify it onem but if you specify one, it will try to load them. | |
647 | .PP | |
648 | Actually, the lib loading order would be \*(L"Bar,Baz,Calc\*(R", and then | |
649 | \&\*(L"Foo,Bar,Baz,Calc\*(R", but independend of which lib exists, the result is the | |
650 | same as trying the latter load alone, except for the fact that one of Bar or | |
651 | Baz might be loaded needlessly in an intermidiate step (and thus hang around | |
652 | and waste memory). If neither Bar nor Baz exist (or don't work/compile), they | |
653 | will still be tried to be loaded, but this is not as time/memory consuming as | |
654 | actually loading one of them. Still, this type of usage is not recommended due | |
655 | to these issues. | |
656 | .PP | |
657 | The old way (loading the lib only in BigInt) still works though: | |
658 | .PP | |
659 | .Vb 3 | |
660 | \& # 6 | |
661 | \& use Math::BigInt lib => 'Bar,Baz'; | |
662 | \& use Math::BigFloat; | |
663 | .Ve | |
664 | .PP | |
665 | You can even load Math::BigInt afterwards: | |
666 | .PP | |
667 | .Vb 3 | |
668 | \& # 7 | |
669 | \& use Math::BigFloat; | |
670 | \& use Math::BigInt lib => 'Bar,Baz'; | |
671 | .Ve | |
672 | .PP | |
673 | But this has the same problems like #5, it will first load Calc | |
674 | (Math::BigFloat needs Math::BigInt and thus loads it) and then later Bar or | |
675 | Baz, depending on which of them works and is usable/loadable. Since this | |
676 | loads Calc unnecc., it is not recommended. | |
677 | .PP | |
678 | Since it also possible to just require Math::BigFloat, this poses the question | |
679 | about what libary this will use: | |
680 | .PP | |
681 | .Vb 2 | |
682 | \& require Math::BigFloat; | |
683 | \& my $x = Math::BigFloat->new(123); $x += 123; | |
684 | .Ve | |
685 | .PP | |
686 | It will use Calc. Please note that the call to \fIimport()\fR is still done, but | |
687 | only when you use for the first time some Math::BigFloat math (it is triggered | |
688 | via any constructor, so the first time you create a Math::BigFloat, the load | |
689 | will happen in the background). This means: | |
690 | .PP | |
691 | .Vb 2 | |
692 | \& require Math::BigFloat; | |
693 | \& Math::BigFloat->import ( lib => 'Foo,Bar' ); | |
694 | .Ve | |
695 | .PP | |
696 | would be the same as: | |
697 | .PP | |
698 | .Vb 1 | |
699 | \& use Math::BigFloat lib => 'Foo, Bar'; | |
700 | .Ve | |
701 | .PP | |
702 | But don't try to be clever to insert some operations in between: | |
703 | .PP | |
704 | .Vb 4 | |
705 | \& require Math::BigFloat; | |
706 | \& my $x = Math::BigFloat->bone() + 4; # load BigInt and Calc | |
707 | \& Math::BigFloat->import( lib => 'Pari' ); # load Pari, too | |
708 | \& $x = Math::BigFloat->bone()+4; # now use Pari | |
709 | .Ve | |
710 | .PP | |
711 | While this works, it loads Calc needlessly. But maybe you just wanted that? | |
712 | .PP | |
713 | \&\fBExamples #3 is highly recommended\fR for daily usage. | |
714 | .SH "BUGS" | |
715 | .IX Header "BUGS" | |
716 | Please see the file \s-1BUGS\s0 in the \s-1CPAN\s0 distribution Math::BigInt for known bugs. | |
717 | .SH "CAVEATS" | |
718 | .IX Header "CAVEATS" | |
719 | .IP "stringify, \fIbstr()\fR" 1 | |
720 | .IX Item "stringify, bstr()" | |
721 | Both stringify and \fIbstr()\fR now drop the leading '+'. The old code would return | |
722 | \&'+1.23', the new returns '1.23'. See the documentation in Math::BigInt for | |
723 | reasoning and details. | |
724 | .IP "bdiv" 1 | |
725 | .IX Item "bdiv" | |
726 | The following will probably not do what you expect: | |
727 | .Sp | |
728 | .Vb 1 | |
729 | \& print $c->bdiv(123.456),"\en"; | |
730 | .Ve | |
731 | .Sp | |
732 | It prints both quotient and reminder since print works in list context. Also, | |
733 | \&\fIbdiv()\fR will modify \f(CW$c\fR, so be carefull. You probably want to use | |
734 | .Sp | |
735 | .Vb 2 | |
736 | \& print $c / 123.456,"\en"; | |
737 | \& print scalar $c->bdiv(123.456),"\en"; # or if you want to modify $c | |
738 | .Ve | |
739 | .Sp | |
740 | instead. | |
741 | .IP "Modifying and =" 1 | |
742 | .IX Item "Modifying and =" | |
743 | Beware of: | |
744 | .Sp | |
745 | .Vb 2 | |
746 | \& $x = Math::BigFloat->new(5); | |
747 | \& $y = $x; | |
748 | .Ve | |
749 | .Sp | |
750 | It will not do what you think, e.g. making a copy of \f(CW$x\fR. Instead it just makes | |
751 | a second reference to the \fBsame\fR object and stores it in \f(CW$y\fR. Thus anything | |
752 | that modifies \f(CW$x\fR will modify \f(CW$y\fR (except overloaded math operators), and vice | |
753 | versa. See Math::BigInt for details and how to avoid that. | |
754 | .IP "bpow" 1 | |
755 | .IX Item "bpow" | |
756 | \&\f(CW\*(C`bpow()\*(C'\fR now modifies the first argument, unlike the old code which left | |
757 | it alone and only returned the result. This is to be consistent with | |
758 | \&\f(CW\*(C`badd()\*(C'\fR etc. The first will modify \f(CW$x\fR, the second one won't: | |
759 | .Sp | |
760 | .Vb 3 | |
761 | \& print bpow($x,$i),"\en"; # modify $x | |
762 | \& print $x->bpow($i),"\en"; # ditto | |
763 | \& print $x ** $i,"\en"; # leave $x alone | |
764 | .Ve | |
765 | .IP "\fIprecision()\fR vs. \fIaccuracy()\fR" 1 | |
766 | .IX Item "precision() vs. accuracy()" | |
767 | A common pitfall is to use \fIprecision()\fR when you want to round a result to | |
768 | a certain number of digits: | |
769 | .Sp | |
770 | .Vb 1 | |
771 | \& use Math::BigFloat; | |
772 | .Ve | |
773 | .Sp | |
774 | .Vb 8 | |
775 | \& Math::BigFloat->precision(4); # does not do what you think it does | |
776 | \& my $x = Math::BigFloat->new(12345); # rounds $x to "12000"! | |
777 | \& print "$x\en"; # print "12000" | |
778 | \& my $y = Math::BigFloat->new(3); # rounds $y to "0"! | |
779 | \& print "$y\en"; # print "0" | |
780 | \& $z = $x / $y; # 12000 / 0 => NaN! | |
781 | \& print "$z\en"; | |
782 | \& print $z->precision(),"\en"; # 4 | |
783 | .Ve | |
784 | .Sp | |
785 | Replacing precision with accuracy is probably not what you want, either: | |
786 | .Sp | |
787 | .Vb 1 | |
788 | \& use Math::BigFloat; | |
789 | .Ve | |
790 | .Sp | |
791 | .Vb 7 | |
792 | \& Math::BigFloat->accuracy(4); # enables global rounding: | |
793 | \& my $x = Math::BigFloat->new(123456); # rounded immidiately to "12350" | |
794 | \& print "$x\en"; # print "123500" | |
795 | \& my $y = Math::BigFloat->new(3); # rounded to "3 | |
796 | \& print "$y\en"; # print "3" | |
797 | \& print $z = $x->copy()->bdiv($y),"\en"; # 41170 | |
798 | \& print $z->accuracy(),"\en"; # 4 | |
799 | .Ve | |
800 | .Sp | |
801 | What you want to use instead is: | |
802 | .Sp | |
803 | .Vb 1 | |
804 | \& use Math::BigFloat; | |
805 | .Ve | |
806 | .Sp | |
807 | .Vb 6 | |
808 | \& my $x = Math::BigFloat->new(123456); # no rounding | |
809 | \& print "$x\en"; # print "123456" | |
810 | \& my $y = Math::BigFloat->new(3); # no rounding | |
811 | \& print "$y\en"; # print "3" | |
812 | \& print $z = $x->copy()->bdiv($y,4),"\en"; # 41150 | |
813 | \& print $z->accuracy(),"\en"; # undef | |
814 | .Ve | |
815 | .Sp | |
816 | In addition to computing what you expected, the last example also does \fBnot\fR | |
817 | \&\*(L"taint\*(R" the result with an accuracy or precision setting, which would | |
818 | influence any further operation. | |
819 | .SH "SEE ALSO" | |
820 | .IX Header "SEE ALSO" | |
821 | Math::BigInt, Math::BigRat and Math::Big as well as | |
822 | Math::BigInt::BitVect, Math::BigInt::Pari and Math::BigInt::GMP. | |
823 | .PP | |
824 | The pragmas bignum, bigint and bigrat might also be of interest | |
825 | because they solve the autoupgrading/downgrading issue, at least partly. | |
826 | .PP | |
827 | The package at | |
828 | <http://search.cpan.org/search?mode=module&query=Math%3A%3ABigInt> contains | |
829 | more documentation including a full version history, testcases, empty | |
830 | subclass files and benchmarks. | |
831 | .SH "LICENSE" | |
832 | .IX Header "LICENSE" | |
833 | This program is free software; you may redistribute it and/or modify it under | |
834 | the same terms as Perl itself. | |
835 | .SH "AUTHORS" | |
836 | .IX Header "AUTHORS" | |
837 | Mark Biggar, overloaded interface by Ilya Zakharevich. | |
838 | Completely rewritten by Tels <http://bloodgate.com> in 2001 \- 2004, and still | |
839 | at it in 2005. |