Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
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 "2002-06-01" "perl v5.8.0" "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_positive(); # true if >= 0 | |
161 | \& $x->is_negative(); # 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 1 | |
174 | \& # The following all modify their first argument: | |
175 | .Ve | |
176 | .PP | |
177 | .Vb 7 | |
178 | \& # set | |
179 | \& $x->bzero(); # set $i to 0 | |
180 | \& $x->bnan(); # set $i to NaN | |
181 | \& $x->bone(); # set $x to +1 | |
182 | \& $x->bone('-'); # set $x to -1 | |
183 | \& $x->binf(); # set $x to inf | |
184 | \& $x->binf('-'); # set $x to -inf | |
185 | .Ve | |
186 | .PP | |
187 | .Vb 6 | |
188 | \& $x->bneg(); # negation | |
189 | \& $x->babs(); # absolute value | |
190 | \& $x->bnorm(); # normalize (no-op) | |
191 | \& $x->bnot(); # two's complement (bit wise not) | |
192 | \& $x->binc(); # increment x by 1 | |
193 | \& $x->bdec(); # decrement x by 1 | |
194 | .Ve | |
195 | .PP | |
196 | .Vb 5 | |
197 | \& $x->badd($y); # addition (add $y to $x) | |
198 | \& $x->bsub($y); # subtraction (subtract $y from $x) | |
199 | \& $x->bmul($y); # multiplication (multiply $x by $y) | |
200 | \& $x->bdiv($y); # divide, set $i to quotient | |
201 | \& # return (quo,rem) or quo if scalar | |
202 | .Ve | |
203 | .PP | |
204 | .Vb 5 | |
205 | \& $x->bmod($y); # modulus | |
206 | \& $x->bpow($y); # power of arguments (a**b) | |
207 | \& $x->blsft($y); # left shift | |
208 | \& $x->brsft($y); # right shift | |
209 | \& # return (quo,rem) or quo if scalar | |
210 | .Ve | |
211 | .PP | |
212 | .Vb 2 | |
213 | \& $x->blog($base); # logarithm of $x, base defaults to e | |
214 | \& # (other bases than e not supported yet) | |
215 | .Ve | |
216 | .PP | |
217 | .Vb 4 | |
218 | \& $x->band($y); # bit-wise and | |
219 | \& $x->bior($y); # bit-wise inclusive or | |
220 | \& $x->bxor($y); # bit-wise exclusive or | |
221 | \& $x->bnot(); # bit-wise not (two's complement) | |
222 | .Ve | |
223 | .PP | |
224 | .Vb 2 | |
225 | \& $x->bsqrt(); # calculate square-root | |
226 | \& $x->bfac(); # factorial of $x (1*2*3*4*..$x) | |
227 | .Ve | |
228 | .PP | |
229 | .Vb 2 | |
230 | \& $x->bround($N); # accuracy: preserver $N digits | |
231 | \& $x->bfround($N); # precision: round to the $Nth digit | |
232 | .Ve | |
233 | .PP | |
234 | .Vb 3 | |
235 | \& # The following do not modify their arguments: | |
236 | \& bgcd(@values); # greatest common divisor | |
237 | \& blcm(@values); # lowest common multiplicator | |
238 | .Ve | |
239 | .PP | |
240 | .Vb 2 | |
241 | \& $x->bstr(); # return string | |
242 | \& $x->bsstr(); # return string in scientific notation | |
243 | .Ve | |
244 | .PP | |
245 | .Vb 2 | |
246 | \& $x->bfloor(); # return integer less or equal than $x | |
247 | \& $x->bceil(); # return integer greater or equal than $x | |
248 | .Ve | |
249 | .PP | |
250 | .Vb 3 | |
251 | \& $x->exponent(); # return exponent as BigInt | |
252 | \& $x->mantissa(); # return mantissa as BigInt | |
253 | \& $x->parts(); # return (mantissa,exponent) as BigInt | |
254 | .Ve | |
255 | .PP | |
256 | .Vb 2 | |
257 | \& $x->length(); # number of digits (w/o sign and '.') | |
258 | \& ($l,$f) = $x->length(); # number of digits, and length of fraction | |
259 | .Ve | |
260 | .PP | |
261 | .Vb 4 | |
262 | \& $x->precision(); # return P of $x (or global, if P of $x undef) | |
263 | \& $x->precision($n); # set P of $x to $n | |
264 | \& $x->accuracy(); # return A of $x (or global, if A of $x undef) | |
265 | \& $x->accuracy($n); # set A $x to $n | |
266 | .Ve | |
267 | .PP | |
268 | .Vb 2 | |
269 | \& Math::BigFloat->precision(); # get/set global P for all BigFloat objects | |
270 | \& Math::BigFloat->accuracy(); # get/set global A for all BigFloat objects | |
271 | .Ve | |
272 | .SH "DESCRIPTION" | |
273 | .IX Header "DESCRIPTION" | |
274 | All operators (inlcuding basic math operations) are overloaded if you | |
275 | declare your big floating point numbers as | |
276 | .PP | |
277 | .Vb 1 | |
278 | \& $i = new Math::BigFloat '12_3.456_789_123_456_789E-2'; | |
279 | .Ve | |
280 | .PP | |
281 | Operations with overloaded operators preserve the arguments, which is | |
282 | exactly what you expect. | |
283 | .Sh "Canonical notation" | |
284 | .IX Subsection "Canonical notation" | |
285 | Input to these routines are either BigFloat objects, or strings of the | |
286 | following four forms: | |
287 | .IP "\(bu" 2 | |
288 | \&\f(CW\*(C`/^[+\-]\ed+$/\*(C'\fR | |
289 | .IP "\(bu" 2 | |
290 | \&\f(CW\*(C`/^[+\-]\ed+\e.\ed*$/\*(C'\fR | |
291 | .IP "\(bu" 2 | |
292 | \&\f(CW\*(C`/^[+\-]\ed+E[+\-]?\ed+$/\*(C'\fR | |
293 | .IP "\(bu" 2 | |
294 | \&\f(CW\*(C`/^[+\-]\ed*\e.\ed+E[+\-]?\ed+$/\*(C'\fR | |
295 | .PP | |
296 | all with optional leading and trailing zeros and/or spaces. Additonally, | |
297 | numbers are allowed to have an underscore between any two digits. | |
298 | .PP | |
299 | Empty strings as well as other illegal numbers results in 'NaN'. | |
300 | .PP | |
301 | \&\fIbnorm()\fR on a BigFloat object is now effectively a no\-op, since the numbers | |
302 | are always stored in normalized form. On a string, it creates a BigFloat | |
303 | object. | |
304 | .Sh "Output" | |
305 | .IX Subsection "Output" | |
306 | Output values are BigFloat objects (normalized), except for \fIbstr()\fR and \fIbsstr()\fR. | |
307 | .PP | |
308 | The string output will always have leading and trailing zeros stripped and drop | |
309 | a plus sign. \f(CW\*(C`bstr()\*(C'\fR will give you always the form with a decimal point, | |
310 | while \f(CW\*(C`bsstr()\*(C'\fR (for scientific) gives you the scientific notation. | |
311 | .PP | |
312 | .Vb 6 | |
313 | \& Input bstr() bsstr() | |
314 | \& '-0' '0' '0E1' | |
315 | \& ' -123 123 123' '-123123123' '-123123123E0' | |
316 | \& '00.0123' '0.0123' '123E-4' | |
317 | \& '123.45E-2' '1.2345' '12345E-4' | |
318 | \& '10E+3' '10000' '1E4' | |
319 | .Ve | |
320 | .PP | |
321 | 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, | |
322 | \&\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) | |
323 | return either undef, <0, 0 or >0 and are suited for sort. | |
324 | .PP | |
325 | Actual math is done by using BigInts to represent the mantissa and exponent. | |
326 | The sign \f(CW\*(C`/^[+\-]$/\*(C'\fR is stored separately. The string 'NaN' is used to | |
327 | represent the result when input arguments are not numbers, as well as | |
328 | the result of dividing by zero. | |
329 | .ie n .Sh """mantissa()""\fP, \f(CW""exponent()""\fP and \f(CW""parts()""" | |
330 | .el .Sh "\f(CWmantissa()\fP, \f(CWexponent()\fP and \f(CWparts()\fP" | |
331 | .IX Subsection "mantissa(), exponent() and parts()" | |
332 | \&\f(CW\*(C`mantissa()\*(C'\fR and \f(CW\*(C`exponent()\*(C'\fR return the said parts of the BigFloat | |
333 | as BigInts such that: | |
334 | .PP | |
335 | .Vb 4 | |
336 | \& $m = $x->mantissa(); | |
337 | \& $e = $x->exponent(); | |
338 | \& $y = $m * ( 10 ** $e ); | |
339 | \& print "ok\en" if $x == $y; | |
340 | .Ve | |
341 | .PP | |
342 | \&\f(CW\*(C`($m,$e) = $x\->parts();\*(C'\fR is just a shortcut giving you both of them. | |
343 | .PP | |
344 | A zero is represented and returned as \f(CW0E1\fR, \fBnot\fR \f(CW0E0\fR (after Knuth). | |
345 | .PP | |
346 | Currently the mantissa is reduced as much as possible, favouring higher | |
347 | exponents over lower ones (e.g. returning 1e7 instead of 10e6 or 10000000e0). | |
348 | This might change in the future, so do not depend on it. | |
349 | .Sh "Accuracy vs. Precision" | |
350 | .IX Subsection "Accuracy vs. Precision" | |
351 | See also: Rounding. | |
352 | .PP | |
353 | Math::BigFloat supports both precision and accuracy. For a full documentation, | |
354 | examples and tips on these topics please see the large section in | |
355 | Math::BigInt. | |
356 | .PP | |
357 | Since things like \fIsqrt\fR\|(2) or 1/3 must presented with a limited precision lest | |
358 | a operation consumes all resources, each operation produces no more than | |
359 | \&\f(CW\*(C`Math::BigFloat::precision()\*(C'\fR digits. | |
360 | .PP | |
361 | In case the result of one operation has more precision than specified, | |
362 | it is rounded. The rounding mode taken is either the default mode, or the one | |
363 | supplied to the operation after the \fIscale\fR: | |
364 | .PP | |
365 | .Vb 7 | |
366 | \& $x = Math::BigFloat->new(2); | |
367 | \& Math::BigFloat::precision(5); # 5 digits max | |
368 | \& $y = $x->copy()->bdiv(3); # will give 0.66666 | |
369 | \& $y = $x->copy()->bdiv(3,6); # will give 0.666666 | |
370 | \& $y = $x->copy()->bdiv(3,6,'odd'); # will give 0.666667 | |
371 | \& Math::BigFloat::round_mode('zero'); | |
372 | \& $y = $x->copy()->bdiv(3,6); # will give 0.666666 | |
373 | .Ve | |
374 | .Sh "Rounding" | |
375 | .IX Subsection "Rounding" | |
376 | .IP "ffround ( +$scale )" 2 | |
377 | .IX Item "ffround ( +$scale )" | |
378 | Rounds to the \f(CW$scale\fR'th place left from the '.', counting from the dot. | |
379 | The first digit is numbered 1. | |
380 | .IP "ffround ( \-$scale )" 2 | |
381 | .IX Item "ffround ( -$scale )" | |
382 | Rounds to the \f(CW$scale\fR'th place right from the '.', counting from the dot. | |
383 | .IP "ffround ( 0 )" 2 | |
384 | .IX Item "ffround ( 0 )" | |
385 | Rounds to an integer. | |
386 | .IP "fround ( +$scale )" 2 | |
387 | .IX Item "fround ( +$scale )" | |
388 | Preserves accuracy to \f(CW$scale\fR digits from the left (aka significant digits) | |
389 | and pads the rest with zeros. If the number is between 1 and \-1, the | |
390 | significant digits count from the first non-zero after the '.' | |
391 | .IP "fround ( \-$scale ) and fround ( 0 )" 2 | |
392 | .IX Item "fround ( -$scale ) and fround ( 0 )" | |
393 | These are effetively no\-ops. | |
394 | .PP | |
395 | All rounding functions take as a second parameter a rounding mode from one of | |
396 | the following: 'even', 'odd', '+inf', '\-inf', 'zero' or 'trunc'. | |
397 | .PP | |
398 | The default rounding mode is 'even'. By using | |
399 | \&\f(CW\*(C`Math::BigFloat::round_mode($round_mode);\*(C'\fR you can get and set the default | |
400 | mode for subsequent rounding. The usage of \f(CW\*(C`$Math::BigFloat::$round_mode\*(C'\fR is | |
401 | no longer supported. | |
402 | The second parameter to the round functions then overrides the default | |
403 | temporarily. | |
404 | .PP | |
405 | The \f(CW\*(C`as_number()\*(C'\fR function returns a BigInt from a Math::BigFloat. It uses | |
406 | \&'trunc' as rounding mode to make it equivalent to: | |
407 | .PP | |
408 | .Vb 2 | |
409 | \& $x = 2.5; | |
410 | \& $y = int($x) + 2; | |
411 | .Ve | |
412 | .PP | |
413 | You can override this by passing the desired rounding mode as parameter to | |
414 | \&\f(CW\*(C`as_number()\*(C'\fR: | |
415 | .PP | |
416 | .Vb 2 | |
417 | \& $x = Math::BigFloat->new(2.5); | |
418 | \& $y = $x->as_number('odd'); # $y = 3 | |
419 | .Ve | |
420 | .SH "EXAMPLES" | |
421 | .IX Header "EXAMPLES" | |
422 | .Vb 1 | |
423 | \& # not ready yet | |
424 | .Ve | |
425 | .SH "Autocreating constants" | |
426 | .IX Header "Autocreating constants" | |
427 | After \f(CW\*(C`use Math::BigFloat ':constant'\*(C'\fR all the floating point constants | |
428 | in the given scope are converted to \f(CW\*(C`Math::BigFloat\*(C'\fR. This conversion | |
429 | happens at compile time. | |
430 | .PP | |
431 | In particular | |
432 | .PP | |
433 | .Vb 1 | |
434 | \& perl -MMath::BigFloat=:constant -e 'print 2E-100,"\en"' | |
435 | .Ve | |
436 | .PP | |
437 | prints the value of \f(CW\*(C`2E\-100\*(C'\fR. Note that without conversion of | |
438 | constants the expression 2E\-100 will be calculated as normal floating point | |
439 | number. | |
440 | .PP | |
441 | Please note that ':constant' does not affect integer constants, nor binary | |
442 | nor hexadecimal constants. Use bignum or Math::BigInt to get this to | |
443 | work. | |
444 | .Sh "Math library" | |
445 | .IX Subsection "Math library" | |
446 | Math with the numbers is done (by default) by a module called | |
447 | Math::BigInt::Calc. This is equivalent to saying: | |
448 | .PP | |
449 | .Vb 1 | |
450 | \& use Math::BigFloat lib => 'Calc'; | |
451 | .Ve | |
452 | .PP | |
453 | You can change this by using: | |
454 | .PP | |
455 | .Vb 1 | |
456 | \& use Math::BigFloat lib => 'BitVect'; | |
457 | .Ve | |
458 | .PP | |
459 | The following would first try to find Math::BigInt::Foo, then | |
460 | Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc: | |
461 | .PP | |
462 | .Vb 1 | |
463 | \& use Math::BigFloat lib => 'Foo,Math::BigInt::Bar'; | |
464 | .Ve | |
465 | .PP | |
466 | Calc.pm uses as internal format an array of elements of some decimal base | |
467 | (usually 1e7, but this might be differen for some systems) with the least | |
468 | significant digit first, while BitVect.pm uses a bit vector of base 2, most | |
469 | significant bit first. Other modules might use even different means of | |
470 | representing the numbers. See the respective module documentation for further | |
471 | details. | |
472 | .PP | |
473 | Please note that Math::BigFloat does \fBnot\fR use the denoted library itself, | |
474 | but it merely passes the lib argument to Math::BigInt. So, instead of the need | |
475 | to do: | |
476 | .PP | |
477 | .Vb 2 | |
478 | \& use Math::BigInt lib => 'GMP'; | |
479 | \& use Math::BigFloat; | |
480 | .Ve | |
481 | .PP | |
482 | you can roll it all into one line: | |
483 | .PP | |
484 | .Vb 1 | |
485 | \& use Math::BigFloat lib => 'GMP'; | |
486 | .Ve | |
487 | .PP | |
488 | Use the lib, Luke! And see \*(L"Using Math::BigInt::Lite\*(R" for more details. | |
489 | .Sh "Using Math::BigInt::Lite" | |
490 | .IX Subsection "Using Math::BigInt::Lite" | |
491 | It is possible to use Math::BigInt::Lite with Math::BigFloat: | |
492 | .PP | |
493 | .Vb 2 | |
494 | \& # 1 | |
495 | \& use Math::BigFloat with => 'Math::BigInt::Lite'; | |
496 | .Ve | |
497 | .PP | |
498 | There is no need to \*(L"use Math::BigInt\*(R" or \*(L"use Math::BigInt::Lite\*(R", but you | |
499 | can combine these if you want. For instance, you may want to use | |
500 | Math::BigInt objects in your main script, too. | |
501 | .PP | |
502 | .Vb 3 | |
503 | \& # 2 | |
504 | \& use Math::BigInt; | |
505 | \& use Math::BigFloat with => 'Math::BigInt::Lite'; | |
506 | .Ve | |
507 | .PP | |
508 | Of course, you can combine this with the \f(CW\*(C`lib\*(C'\fR parameter. | |
509 | .PP | |
510 | .Vb 2 | |
511 | \& # 3 | |
512 | \& use Math::BigFloat with => 'Math::BigInt::Lite', lib => 'GMP,Pari'; | |
513 | .Ve | |
514 | .PP | |
515 | If you want to use Math::BigInt's, too, simple add a Math::BigInt \fBbefore\fR: | |
516 | .PP | |
517 | .Vb 3 | |
518 | \& # 4 | |
519 | \& use Math::BigInt; | |
520 | \& use Math::BigFloat with => 'Math::BigInt::Lite', lib => 'GMP,Pari'; | |
521 | .Ve | |
522 | .PP | |
523 | Notice that the module with the last \f(CW\*(C`lib\*(C'\fR will \*(L"win\*(R" and thus | |
524 | it's lib will be used if the lib is available: | |
525 | .PP | |
526 | .Vb 3 | |
527 | \& # 5 | |
528 | \& use Math::BigInt lib => 'Bar,Baz'; | |
529 | \& use Math::BigFloat with => 'Math::BigInt::Lite', lib => 'Foo'; | |
530 | .Ve | |
531 | .PP | |
532 | That would try to load Foo, Bar, Baz and Calc (in that order). Or in other | |
533 | words, Math::BigFloat will try to retain previously loaded libs when you | |
534 | don't specify it one. | |
535 | .PP | |
536 | Actually, the lib loading order would be \*(L"Bar,Baz,Calc\*(R", and then | |
537 | \&\*(L"Foo,Bar,Baz,Calc\*(R", but independend of which lib exists, the result is the | |
538 | same as trying the latter load alone, except for the fact that Bar or Baz | |
539 | might be loaded needlessly in an intermidiate step | |
540 | .PP | |
541 | The old way still works though: | |
542 | .PP | |
543 | .Vb 3 | |
544 | \& # 6 | |
545 | \& use Math::BigInt lib => 'Bar,Baz'; | |
546 | \& use Math::BigFloat; | |
547 | .Ve | |
548 | .PP | |
549 | But \fBexamples #3 and #4 are recommended\fR for usage. | |
550 | .SH "BUGS" | |
551 | .IX Header "BUGS" | |
552 | .IP "\(bu" 2 | |
553 | The following does not work yet: | |
554 | .Sp | |
555 | .Vb 4 | |
556 | \& $m = $x->mantissa(); | |
557 | \& $e = $x->exponent(); | |
558 | \& $y = $m * ( 10 ** $e ); | |
559 | \& print "ok\en" if $x == $y; | |
560 | .Ve | |
561 | .IP "\(bu" 2 | |
562 | There is no \fIfmod()\fR function yet. | |
563 | .SH "CAVEAT" | |
564 | .IX Header "CAVEAT" | |
565 | .IP "stringify, \fIbstr()\fR" 1 | |
566 | .IX Item "stringify, bstr()" | |
567 | Both stringify and \fIbstr()\fR now drop the leading '+'. The old code would return | |
568 | \&'+1.23', the new returns '1.23'. See the documentation in Math::BigInt for | |
569 | reasoning and details. | |
570 | .IP "bdiv" 1 | |
571 | .IX Item "bdiv" | |
572 | The following will probably not do what you expect: | |
573 | .Sp | |
574 | .Vb 1 | |
575 | \& print $c->bdiv(123.456),"\en"; | |
576 | .Ve | |
577 | .Sp | |
578 | It prints both quotient and reminder since print works in list context. Also, | |
579 | \&\fIbdiv()\fR will modify \f(CW$c\fR, so be carefull. You probably want to use | |
580 | .Sp | |
581 | .Vb 2 | |
582 | \& print $c / 123.456,"\en"; | |
583 | \& print scalar $c->bdiv(123.456),"\en"; # or if you want to modify $c | |
584 | .Ve | |
585 | .Sp | |
586 | instead. | |
587 | .IP "Modifying and =" 1 | |
588 | .IX Item "Modifying and =" | |
589 | Beware of: | |
590 | .Sp | |
591 | .Vb 2 | |
592 | \& $x = Math::BigFloat->new(5); | |
593 | \& $y = $x; | |
594 | .Ve | |
595 | .Sp | |
596 | It will not do what you think, e.g. making a copy of \f(CW$x\fR. Instead it just makes | |
597 | a second reference to the \fBsame\fR object and stores it in \f(CW$y\fR. Thus anything | |
598 | that modifies \f(CW$x\fR will modify \f(CW$y\fR, and vice versa. | |
599 | .Sp | |
600 | .Vb 2 | |
601 | \& $x->bmul(2); | |
602 | \& print "$x, $y\en"; # prints '10, 10' | |
603 | .Ve | |
604 | .Sp | |
605 | If you want a true copy of \f(CW$x\fR, use: | |
606 | .Sp | |
607 | .Vb 1 | |
608 | \& $y = $x->copy(); | |
609 | .Ve | |
610 | .Sp | |
611 | See also the documentation in overload regarding \f(CW\*(C`=\*(C'\fR. | |
612 | .IP "bpow" 1 | |
613 | .IX Item "bpow" | |
614 | \&\f(CW\*(C`bpow()\*(C'\fR now modifies the first argument, unlike the old code which left | |
615 | it alone and only returned the result. This is to be consistent with | |
616 | \&\f(CW\*(C`badd()\*(C'\fR etc. The first will modify \f(CW$x\fR, the second one won't: | |
617 | .Sp | |
618 | .Vb 3 | |
619 | \& print bpow($x,$i),"\en"; # modify $x | |
620 | \& print $x->bpow($i),"\en"; # ditto | |
621 | \& print $x ** $i,"\en"; # leave $x alone | |
622 | .Ve | |
623 | .SH "LICENSE" | |
624 | .IX Header "LICENSE" | |
625 | This program is free software; you may redistribute it and/or modify it under | |
626 | the same terms as Perl itself. | |
627 | .SH "AUTHORS" | |
628 | .IX Header "AUTHORS" | |
629 | Mark Biggar, overloaded interface by Ilya Zakharevich. | |
630 | Completely rewritten by Tels http://bloodgate.com in 2001. |