Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / man / man3 / Math::BigFloat.3
CommitLineData
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"
134Math::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"
283All operators (inlcuding basic math operations) are overloaded if you
284declare 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
290Operations with overloaded operators preserve the arguments, which is
291exactly what you expect.
292.Sh "Canonical notation"
293.IX Subsection "Canonical notation"
294Input to these routines are either BigFloat objects, or strings of the
295following 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
305all with optional leading and trailing zeros and/or spaces. Additonally,
306numbers are allowed to have an underscore between any two digits.
307.PP
308Empty 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
311are always stored in normalized form. On a string, it creates a BigFloat
312object.
313.Sh "Output"
314.IX Subsection "Output"
315Output values are BigFloat objects (normalized), except for \fIbstr()\fR and \fIbsstr()\fR.
316.PP
317The string output will always have leading and trailing zeros stripped and drop
318a plus sign. \f(CW\*(C`bstr()\*(C'\fR will give you always the form with a decimal point,
319while \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
330Some 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)
332return either undef, <0, 0 or >0 and are suited for sort.
333.PP
334Actual math is done by using the class defined with \f(CW\*(C`with =\*(C'\fR Class;> (which
335defaults to BigInts) to represent the mantissa and exponent.
336.PP
337The sign \f(CW\*(C`/^[+\-]$/\*(C'\fR is stored separately. The string 'NaN' is used to
338represent the result when input arguments are not numbers, as well as
339the 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
344as 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
355A zero is represented and returned as \f(CW0E1\fR, \fBnot\fR \f(CW0E0\fR (after Knuth).
356.PP
357Currently the mantissa is reduced as much as possible, favouring higher
358exponents over lower ones (e.g. returning 1e7 instead of 10e6 or 10000000e0).
359This might change in the future, so do not depend on it.
360.Sh "Accuracy vs. Precision"
361.IX Subsection "Accuracy vs. Precision"
362See also: Rounding.
363.PP
364Math::BigFloat supports both precision (rounding to a certain place before or
365after the dot) and accuracy (rounding to a certain number of digits). For a
366full documentation, examples and tips on these topics please see the large
367section about rounding in Math::BigInt.
368.PP
369Since things like \f(CWsqrt(2)\fR or \f(CW\*(C`1 / 3\*(C'\fR must presented with a limited
370accuracy lest a operation consumes all resources, each operation produces
371no more than the requested number of digits.
372.PP
373If there is no gloabl precision or accuracy set, \fBand\fR the operation in
374question was not called with a requested precision or accuracy, \fBand\fR the
375input \f(CW$x\fR has no accuracy or precision set, then a fallback parameter will
376be used. For historical reasons, it is called \f(CW\*(C`div_scale\*(C'\fR and can be accessed
377via:
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
384The default value for \f(CW\*(C`div_scale\*(C'\fR is 40.
385.PP
386In case the result of one operation has more digits than specified,
387it is rounded. The rounding mode taken is either the default mode, or the one
388supplied 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
400Note that \f(CW\*(C`Math::BigFloat\->accuracy()\*(C'\fR and \f(CW\*(C`Math::BigFloat\->precision()\*(C'\fR
401set the global variables, and thus \fBany\fR newly created number will be subject
402to 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
405It is less confusing to either calculate the result fully, and afterwards
406round it explicitely, or use the additional parameters to the math
407functions 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 )"
430Rounds to the \f(CW$scale\fR'th place left from the '.', counting from the dot.
431The first digit is numbered 1.
432.IP "ffround ( \-$scale )" 2
433.IX Item "ffround ( -$scale )"
434Rounds 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 )"
437Rounds to an integer.
438.IP "fround ( +$scale )" 2
439.IX Item "fround ( +$scale )"
440Preserves accuracy to \f(CW$scale\fR digits from the left (aka significant digits)
441and pads the rest with zeros. If the number is between 1 and \-1, the
442significant 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 )"
445These are effectively no\-ops.
446.PP
447All rounding functions take as a second parameter a rounding mode from one of
448the following: 'even', 'odd', '+inf', '\-inf', 'zero' or 'trunc'.
449.PP
450The 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
452mode for subsequent rounding. The usage of \f(CW\*(C`$Math::BigFloat::$round_mode\*(C'\fR is
453no longer supported.
454The second parameter to the round functions then overrides the default
455temporarily.
456.PP
457The \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
465You 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
487Set or get the global or local accuracy, aka how many significant digits the
488results have. If you set a global accuracy, then this also applies to \fInew()\fR!
489.PP
490Warning! The accuracy \fIsticks\fR, e.g. once you created a number under the
491influence of \f(CW\*(C`CLASS\->accuracy($A)\*(C'\fR, all results from math operations with
492that number will also be rounded.
493.PP
494In 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
496to 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
522Note: You probably want to use \fIaccuracy()\fR instead. With accuracy you
523set the number of digits each result should have, with precision you
524set the place where to round!
525.SH "Autocreating constants"
526.IX Header "Autocreating constants"
527After \f(CW\*(C`use Math::BigFloat ':constant'\*(C'\fR all the floating point constants
528in the given scope are converted to \f(CW\*(C`Math::BigFloat\*(C'\fR. This conversion
529happens at compile time.
530.PP
531In particular
532.PP
533.Vb 1
534\& perl -MMath::BigFloat=:constant -e 'print 2E-100,"\en"'
535.Ve
536.PP
537prints the value of \f(CW\*(C`2E\-100\*(C'\fR. Note that without conversion of
538constants the expression 2E\-100 will be calculated as normal floating point
539number.
540.PP
541Please note that ':constant' does not affect integer constants, nor binary
542nor hexadecimal constants. Use bignum or Math::BigInt to get this to
543work.
544.Sh "Math library"
545.IX Subsection "Math library"
546Math with the numbers is done (by default) by a module called
547Math::BigInt::Calc. This is equivalent to saying:
548.PP
549.Vb 1
550\& use Math::BigFloat lib => 'Calc';
551.Ve
552.PP
553You can change this by using:
554.PP
555.Vb 1
556\& use Math::BigFloat lib => 'BitVect';
557.Ve
558.PP
559The following would first try to find Math::BigInt::Foo, then
560Math::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
566Calc.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
568significant digit first, while BitVect.pm uses a bit vector of base 2, most
569significant bit first. Other modules might use even different means of
570representing the numbers. See the respective module documentation for further
571details.
572.PP
573Please note that Math::BigFloat does \fBnot\fR use the denoted library itself,
574but it merely passes the lib argument to Math::BigInt. So, instead of the need
575to do:
576.PP
577.Vb 2
578\& use Math::BigInt lib => 'GMP';
579\& use Math::BigFloat;
580.Ve
581.PP
582you can roll it all into one line:
583.PP
584.Vb 1
585\& use Math::BigFloat lib => 'GMP';
586.Ve
587.PP
588It is also possible to just require Math::BigFloat:
589.PP
590.Vb 1
591\& require Math::BigFloat;
592.Ve
593.PP
594This will load the neccessary things (like BigInt) when they are needed, and
595automatically.
596.PP
597Use the lib, Luke! And see \*(L"Using Math::BigInt::Lite\*(R" for more details than
598you ever wanted to know about loading a different library.
599.Sh "Using Math::BigInt::Lite"
600.IX Subsection "Using Math::BigInt::Lite"
601It 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
608There is no need to \*(L"use Math::BigInt\*(R" or \*(L"use Math::BigInt::Lite\*(R", but you
609can combine these if you want. For instance, you may want to use
610Math::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
618Of 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
625There is no need for a \*(L"use Math::BigInt;\*(R" statement, even if you want to
626use Math::BigInt's, since Math::BigFloat will needs Math::BigInt and thus
627always 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
635Notice that the module with the last \f(CW\*(C`lib\*(C'\fR will \*(L"win\*(R" and thus
636it'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
644That would try to load Foo, Bar, Baz and Calc (in that order). Or in other
645words, Math::BigFloat will try to retain previously loaded libs when you
646don't specify it onem but if you specify one, it will try to load them.
647.PP
648Actually, 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
650same as trying the latter load alone, except for the fact that one of Bar or
651Baz might be loaded needlessly in an intermidiate step (and thus hang around
652and waste memory). If neither Bar nor Baz exist (or don't work/compile), they
653will still be tried to be loaded, but this is not as time/memory consuming as
654actually loading one of them. Still, this type of usage is not recommended due
655to these issues.
656.PP
657The 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
665You 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
673But 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
675Baz, depending on which of them works and is usable/loadable. Since this
676loads Calc unnecc., it is not recommended.
677.PP
678Since it also possible to just require Math::BigFloat, this poses the question
679about 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
686It will use Calc. Please note that the call to \fIimport()\fR is still done, but
687only when you use for the first time some Math::BigFloat math (it is triggered
688via any constructor, so the first time you create a Math::BigFloat, the load
689will 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
696would be the same as:
697.PP
698.Vb 1
699\& use Math::BigFloat lib => 'Foo, Bar';
700.Ve
701.PP
702But 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
711While 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"
716Please 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()"
721Both 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
723reasoning and details.
724.IP "bdiv" 1
725.IX Item "bdiv"
726The following will probably not do what you expect:
727.Sp
728.Vb 1
729\& print $c->bdiv(123.456),"\en";
730.Ve
731.Sp
732It 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
740instead.
741.IP "Modifying and =" 1
742.IX Item "Modifying and ="
743Beware of:
744.Sp
745.Vb 2
746\& $x = Math::BigFloat->new(5);
747\& $y = $x;
748.Ve
749.Sp
750It will not do what you think, e.g. making a copy of \f(CW$x\fR. Instead it just makes
751a second reference to the \fBsame\fR object and stores it in \f(CW$y\fR. Thus anything
752that modifies \f(CW$x\fR will modify \f(CW$y\fR (except overloaded math operators), and vice
753versa. 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
757it 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()"
767A common pitfall is to use \fIprecision()\fR when you want to round a result to
768a 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
785Replacing 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
801What 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
816In 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
818influence any further operation.
819.SH "SEE ALSO"
820.IX Header "SEE ALSO"
821Math::BigInt, Math::BigRat and Math::Big as well as
822Math::BigInt::BitVect, Math::BigInt::Pari and Math::BigInt::GMP.
823.PP
824The pragmas bignum, bigint and bigrat might also be of interest
825because they solve the autoupgrading/downgrading issue, at least partly.
826.PP
827The package at
828<http://search.cpan.org/search?mode=module&query=Math%3A%3ABigInt> contains
829more documentation including a full version history, testcases, empty
830subclass files and benchmarks.
831.SH "LICENSE"
832.IX Header "LICENSE"
833This program is free software; you may redistribute it and/or modify it under
834the same terms as Perl itself.
835.SH "AUTHORS"
836.IX Header "AUTHORS"
837Mark Biggar, overloaded interface by Ilya Zakharevich.
838Completely rewritten by Tels <http://bloodgate.com> in 2001 \- 2004, and still
839at it in 2005.