2 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns=
"http://www.w3.org/1999/xhtml">
5 <title>bignum - Transparent BigNumber support for Perl
</title>
6 <meta http-equiv=
"content-type" content=
"text/html; charset=utf-8" />
7 <link rev=
"made" href=
"mailto:" />
10 <body style=
"background-color: white">
11 <table border=
"0" width=
"100%" cellspacing=
"0" cellpadding=
"3">
12 <tr><td class=
"block" style=
"background-color: #cccccc" valign=
"middle">
13 <big><strong><span class=
"block"> bignum - Transparent BigNumber support for Perl
</span></strong></big>
17 <p><a name=
"__index__"></a></p>
22 <li><a href=
"#name">NAME
</a></li>
23 <li><a href=
"#synopsis">SYNOPSIS
</a></li>
24 <li><a href=
"#description">DESCRIPTION
</a></li>
27 <li><a href=
"#options">Options
</a></li>
28 <li><a href=
"#methods">Methods
</a></li>
29 <li><a href=
"#caveat">Caveat
</a></li>
30 <li><a href=
"#math_library">MATH LIBRARY
</a></li>
31 <li><a href=
"#internal_format">INTERNAL FORMAT
</a></li>
32 <li><a href=
"#sign">SIGN
</a></li>
35 <li><a href=
"#modules_used">MODULES USED
</a></li>
36 <li><a href=
"#examples">EXAMPLES
</a></li>
37 <li><a href=
"#license">LICENSE
</a></li>
38 <li><a href=
"#see_also">SEE ALSO
</a></li>
39 <li><a href=
"#authors">AUTHORS
</a></li>
46 <h1><a name=
"name">NAME
</a></h1>
47 <p>bignum - Transparent BigNumber support for Perl
</p>
51 <h1><a name=
"synopsis">SYNOPSIS
</a></h1>
55 $x =
2 +
4.5,
"\n
"; # BigFloat
6.5
56 print
2 **
512 *
0.1,
"\n
"; # really is what you think it is
57 print inf * inf,
"\n
"; # prints inf
58 print NaN *
3,
"\n
"; # prints NaN
</pre>
62 <h1><a name=
"description">DESCRIPTION
</a></h1>
63 <p>All operators (including basic math operations) are overloaded. Integer and
64 floating-point constants are created as proper BigInts or BigFloats,
69 <p>at the top of your script, Math::BigFloat and Math::BigInt will be loaded
70 and any constant number will be converted to an object (Math::BigFloat for
71 floats like
3.1415 and Math::BigInt for integers like
1234).
</p>
72 <p>So, the following line:
</p>
75 <p>creates actually a Math::BigInt and stores a reference to in $x.
76 This happens transparently and behind your back, so to speak.
</p>
77 <p>You can see this with the following:
</p>
79 perl -Mbignum -le 'print ref(
1234)'
</pre>
80 <p>Don't worry if it says Math::BigInt::Lite, bignum and friends will use Lite
81 if it is installed since it is faster for some operations. It will be
82 automatically upgraded to BigInt whenever neccessary:
</p>
84 perl -Mbignum -le 'print ref(
2**
255)'
</pre>
85 <p>This also means it is a bad idea to check for some specific package, since
86 the actual contents of $x might be something unexpected. Due to the
87 transparent way of bignum
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_ref"><code>ref()
</code></a> should not be neccessary, anyway.
</p>
88 <p>Since Math::BigInt and BigFloat also overload the normal math operations,
89 the following line will still work:
</p>
91 perl -Mbignum -le 'print ref(
1234+
1234)'
</pre>
92 <p>Since numbers are actually objects, you can call all the usual methods from
93 BigInt/BigFloat on them. This even works to some extent on expressions:
</p>
95 perl -Mbignum -le '$x =
1234; print $x-
>bdec()'
96 perl -Mbignum -le 'print
1234-
>binc();'
97 perl -Mbignum -le 'print
1234-
>binc-
>badd(
6);'
98 perl -Mbignum -le 'print +(
1234)-
>binc()'
</pre>
99 <p>(Note that print doesn't do what you expect if the expression starts with
100 '(' hence the
<code>+
</code>)
</p>
101 <p>You can even chain the operations together as usual:
</p>
103 perl -Mbignum -le 'print
1234-
>binc-
>badd(
6);'
105 <p>Under bignum (or bigint or bigrat), Perl will ``upgrade'' the numbers
106 appropriately. This means that:
</p>
108 perl -Mbignum -le 'print
1234+
4.5'
110 <p>will work correctly. These mixed cases don't do always work when using
111 Math::BigInt or Math::BigFloat alone, or at least not in the way normal Perl
113 <p>If you do want to work with large integers like under
<code>use integer;
</code>, try
114 <code>use bigint;
</code>:
</p>
116 perl -Mbigint -le 'print
1234.5+
4.5'
118 <p>There is also
<code>use bigrat;
</code> which gives you big rationals:
</p>
120 perl -Mbigrat -le 'print
1234+
4.1'
122 <p>The entire upgrading/downgrading is still experimental and might not work
123 as you expect or may even have bugs.
</p>
124 <p>You might get errors like this:
</p>
126 Can't use an undefined value as an ARRAY reference at
127 /usr/local/lib/perl5/
5.8.0/Math/BigInt/Calc.pm line
864</pre>
128 <p>This means somewhere a routine got a BigFloat/Lite but expected a BigInt (or
129 vice versa) and the upgrade/downgrad path was missing. This is a bug, please
130 report it so that we can fix it.
</p>
131 <p>You might consider using just Math::BigInt or Math::BigFloat, since they
132 allow you finer control over what get's done in which module/space. For
133 instance, simple loop counters will be Math::BigInts under
<code>use bignum;
</code> and
134 this is slower than keeping them as Perl scalars:
</p>
136 perl -Mbignum -le 'for ($i =
0; $i
< 10; $i++) { print ref($i); }'
</pre>
137 <p>Please note the following does not work as expected (prints nothing), since
138 overloading of '..' is not yet possible in Perl (as of v5.8
.0):
</p>
140 perl -Mbignum -le 'for (
1.
.2) { print ref($_); }'
</pre>
143 <h2><a name=
"options">Options
</a></h2>
144 <p>bignum recognizes some options that can be passed while loading it via use.
145 The options can (currently) be either a single letter form, or the long form.
146 The following options exist:
</p>
148 <dt><strong><a name=
"item_a_or_accuracy">a or accuracy
</a></strong>
151 <p>This sets the accuracy for all math operations. The argument must be greater
152 than or equal to zero. See Math::BigInt's
<code>bround()
</code> function for details.
</p>
156 perl -Mbignum=a,
50 -le 'print sqrt(
20)'
</pre>
159 <dt><strong><a name=
"item_p_or_precision">p or precision
</a></strong>
162 <p>This sets the precision for all math operations. The argument can be any
163 integer. Negative values mean a fixed number of digits after the dot, while
164 a positive value rounds to this digit left from the dot.
0 or
1 mean round to
165 integer. See Math::BigInt's
<code>bfround()
</code> function for details.
</p>
169 perl -Mbignum=p,-
50 -le 'print sqrt(
20)'
</pre>
172 <dt><strong><a name=
"item_t_or_trace">t or trace
</a></strong>
175 <p>This enables a trace mode and is primarily for debugging bignum or
176 Math::BigInt/Math::BigFloat.
</p>
179 <dt><strong><a name=
"item_l_or_lib">l or lib
</a></strong>
182 <p>Load a different math lib, see
<a href=
"#math_library">MATH LIBRARY
</a>.
</p>
186 perl -Mbignum=l,GMP -e 'print
2 **
512'
</pre>
189 <p>Currently there is no way to specify more than one library on the command
190 line. This will be hopefully fixed soon ;)
</p>
193 <dt><strong><a name=
"item_v_or_version">v or version
</a></strong>
196 <p>This prints out the name and version of all modules used and then exits.
</p>
200 perl -Mbignum=v
</pre>
206 <h2><a name=
"methods">Methods
</a></h2>
207 <p>Beside
<code>import()
</code> and
<code>AUTOLOAD()
</code> there are only a few other methods.
</p>
208 <p>Since all numbers are now objects, you can use all functions that are part of
209 the BigInt or BigFloat API. It is wise to use only the
<code>bxxx()
</code> notation, and not
210 the
<code>fxxx()
</code> notation, though. This makes it possible that the underlying object
211 might morph into a different class than BigFloat.
</p>
214 <h2><a name=
"caveat">Caveat
</a></h2>
215 <p>But a warning is in order. When using the following to make a copy of a number,
216 only a shallow copy will be made.
</p>
220 <p>If you want to make a real copy, use the following:
</p>
222 $y = $x-
>copy();
</pre>
223 <p>Using the copy or the original with overloaded math is okay, e.g. the
227 print $x +
1,
" ", $y,
"\n
"; # prints
10 9</pre>
228 <p>but calling any method that modifies the number directly will result in
229 <strong>both
</strong> the original and the copy beeing destroyed:
</p>
232 print $x-
>badd(
1),
" ", $y,
"\n
"; # prints
10 10</pre>
235 print $x-
>binc(
1),
" ", $y,
"\n
"; # prints
10 10</pre>
238 print $x-
>bmul(
2),
" ", $y,
"\n
"; # prints
18 18</pre>
239 <p>Using methods that do not modify, but testthe contents works:
</p>
242 $z =
9 if $x-
>is_zero(); # works fine
</pre>
243 <p>See the documentation about the copy constructor and
<code>=
</code> in overload, as
244 well as the documentation in BigInt for further details.
</p>
246 <dt><strong><a name=
"item_inf"><code>inf()
</code></a></strong>
249 <p>A shortcut to return Math::BigInt-
>binf(). Usefull because Perl does not always
250 handle bareword
<a href=
"#item_inf"><code>inf
</code></a> properly.
</p>
253 <dt><strong><a name=
"item_nan"><code>NaN()
</code></a></strong>
256 <p>A shortcut to return Math::BigInt-
>bnan(). Usefull because Perl does not always
257 handle bareword
<a href=
"#item_nan"><code>NaN
</code></a> properly.
</p>
260 <dt><strong><a name=
"item_upgrade"><code>upgrade()
</code></a></strong>
263 <p>Return the class that numbers are upgraded to, is in fact returning
264 <code>$Math::BigInt::upgrade
</code>.
</p>
270 <h2><a name=
"math_library">MATH LIBRARY
</a></h2>
271 <p>Math with the numbers is done (by default) by a module called
272 Math::BigInt::Calc. This is equivalent to saying:
</p>
274 use bignum lib =
> 'Calc';
</pre>
275 <p>You can change this by using:
</p>
277 use bignum lib =
> 'BitVect';
</pre>
278 <p>The following would first try to find Math::BigInt::Foo, then
279 Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:
</p>
281 use bignum lib =
> 'Foo,Math::BigInt::Bar';
</pre>
282 <p>Please see respective module documentation for further details.
</p>
285 <h2><a name=
"internal_format">INTERNAL FORMAT
</a></h2>
286 <p>The numbers are stored as objects, and their internals might change at anytime,
287 especially between math operations. The objects also might belong to different
288 classes, like Math::BigInt, or Math::BigFLoat. Mixing them together, even
289 with normal scalars is not extraordinary, but normal and expected.
</p>
290 <p>You should not depend on the internal format, all accesses must go through
291 accessor methods. E.g. looking at $x-
>{sign} is not a bright idea since there
292 is no guaranty that the object in question has such a hashkey, nor is a hash
293 underneath at all.
</p>
296 <h2><a name=
"sign">SIGN
</a></h2>
297 <p>The sign is either '+', '-', 'NaN', '+inf' or '-inf' and stored seperately.
298 You can access it with the
<code>sign()
</code> method.
</p>
299 <p>A sign of 'NaN' is used to represent the result when input arguments are not
300 numbers or as a result of
0/
0. '+inf' and '-inf' represent plus respectively
301 minus infinity. You will get '+inf' when dividing a positive number by
0, and
302 '-inf' when dividing any negative number by
0.
</p>
306 <h1><a name=
"modules_used">MODULES USED
</a></h1>
307 <p><code>bignum
</code> is just a thin wrapper around various modules of the Math::BigInt
308 family. Think of it as the head of the family, who runs the shop, and orders
309 the others to do the work.
</p>
310 <p>The following modules are currently used by bignum:
</p>
312 Math::BigInt::Lite (for speed, and only if it is loadable)
318 <h1><a name=
"examples">EXAMPLES
</a></h1>
319 <p>Some cool command line examples to impress the Python crowd ;)
323 perl -Mbignum -le 'print sqrt(
33)'
324 perl -Mbignum -le 'print
2*
255'
325 perl -Mbignum -le 'print
4.5+
2*
255'
326 perl -Mbignum -le 'print
3/
7 +
5/
7 +
8/
3'
327 perl -Mbignum -le 'print
123-
>is_odd()'
328 perl -Mbignum -le 'print log(
2)'
329 perl -Mbignum -le 'print
2 **
0.5'
330 perl -Mbignum=a,
65 -le 'print
2 **
0.2'
</pre>
334 <h1><a name=
"license">LICENSE
</a></h1>
335 <p>This program is free software; you may redistribute it and/or modify it under
336 the same terms as Perl itself.
</p>
340 <h1><a name=
"see_also">SEE ALSO
</a></h1>
341 <p>Especially
<a href=
"file://C|\msysgit\mingw\html/lib/bigrat.html">the bigrat manpage
</a> as in
<code>perl -Mbigrat -le 'print
1/
3+
1/
4'
</code>.
</p>
342 <p><a href=
"file://C|\msysgit\mingw\html/lib/Math/BigFloat.html">the Math::BigFloat manpage
</a>,
<a href=
"file://C|\msysgit\mingw\html/lib/Math/BigInt.html">the Math::BigInt manpage
</a>,
<a href=
"file://C|\msysgit\mingw\html/lib/Math/BigRat.html">the Math::BigRat manpage
</a> and
<a href=
"file://C|\msysgit\mingw\html/Math/Big.html">the Math::Big manpage
</a> as well
343 as
<a href=
"file://C|\msysgit\mingw\html/Math/BigInt/BitVect.html">the Math::BigInt::BitVect manpage
</a>,
<a href=
"file://C|\msysgit\mingw\html/Math/BigInt/Pari.html">the Math::BigInt::Pari manpage
</a> and
<a href=
"file://C|\msysgit\mingw\html/Math/BigInt/GMP.html">the Math::BigInt::GMP manpage
</a>.
</p>
347 <h1><a name=
"authors">AUTHORS
</a></h1>
348 <p>(C) by Tels
<a href=
"http://bloodgate.com/">http://bloodgate.com/
</a> in early
2002,
2003.
</p>
349 <table border=
"0" width=
"100%" cellspacing=
"0" cellpadding=
"3">
350 <tr><td class=
"block" style=
"background-color: #cccccc" valign=
"middle">
351 <big><strong><span class=
"block"> bignum - Transparent BigNumber support for Perl
</span></strong></big>