1bignum(3pm) Perl Programmers Reference Guide bignum(3pm)
2
3
4
6 bignum - Transparent BigNumber support for Perl
7
9 use bignum;
10
11 $x = 2 + 4.5,"\n"; # BigFloat 6.5
12 print 2 ** 512 * 0.1,"\n"; # really is what you think it is
13 print inf * inf,"\n"; # prints inf
14 print NaN * 3,"\n"; # prints NaN
15
16 {
17 no bignum;
18 print 2 ** 256,"\n"; # a normal Perl scalar now
19 }
20
21 # for older Perls, note that this will be global:
22 use bignum qw/hex oct/;
23 print hex("0x1234567890123490"),"\n";
24 print oct("01234567890123490"),"\n";
25
27 All operators (including basic math operations) are overloaded. Integer
28 and floating-point constants are created as proper BigInts or
29 BigFloats, respectively.
30
31 If you do
32
33 use bignum;
34
35 at the top of your script, Math::BigFloat and Math::BigInt will be
36 loaded and any constant number will be converted to an object
37 (Math::BigFloat for floats like 3.1415 and Math::BigInt for integers
38 like 1234).
39
40 So, the following line:
41
42 $x = 1234;
43
44 creates actually a Math::BigInt and stores a reference to in $x. This
45 happens transparently and behind your back, so to speak.
46
47 You can see this with the following:
48
49 perl -Mbignum -le 'print ref(1234)'
50
51 Don't worry if it says Math::BigInt::Lite, bignum and friends will use
52 Lite if it is installed since it is faster for some operations. It will
53 be automatically upgraded to BigInt whenever necessary:
54
55 perl -Mbignum -le 'print ref(2**255)'
56
57 This also means it is a bad idea to check for some specific package,
58 since the actual contents of $x might be something unexpected. Due to
59 the transparent way of bignum "ref()" should not be necessary, anyway.
60
61 Since Math::BigInt and BigFloat also overload the normal math
62 operations, the following line will still work:
63
64 perl -Mbignum -le 'print ref(1234+1234)'
65
66 Since numbers are actually objects, you can call all the usual methods
67 from BigInt/BigFloat on them. This even works to some extent on
68 expressions:
69
70 perl -Mbignum -le '$x = 1234; print $x->bdec()'
71 perl -Mbignum -le 'print 1234->copy()->binc();'
72 perl -Mbignum -le 'print 1234->copy()->binc->badd(6);'
73 perl -Mbignum -le 'print +(1234)->copy()->binc()'
74
75 (Note that print doesn't do what you expect if the expression starts
76 with '(' hence the "+")
77
78 You can even chain the operations together as usual:
79
80 perl -Mbignum -le 'print 1234->copy()->binc->badd(6);'
81 1241
82
83 Under bignum (or bigint or bigrat), Perl will "upgrade" the numbers
84 appropriately. This means that:
85
86 perl -Mbignum -le 'print 1234+4.5'
87 1238.5
88
89 will work correctly. These mixed cases don't do always work when using
90 Math::BigInt or Math::BigFloat alone, or at least not in the way normal
91 Perl scalars work.
92
93 If you do want to work with large integers like under "use integer;",
94 try "use bigint;":
95
96 perl -Mbigint -le 'print 1234.5+4.5'
97 1238
98
99 There is also "use bigrat;" which gives you big rationals:
100
101 perl -Mbigrat -le 'print 1234+4.1'
102 12381/10
103
104 The entire upgrading/downgrading is still experimental and might not
105 work as you expect or may even have bugs. You might get errors like
106 this:
107
108 Can't use an undefined value as an ARRAY reference at
109 /usr/local/lib/perl5/5.8.0/Math/BigInt/Calc.pm line 864
110
111 This means somewhere a routine got a BigFloat/Lite but expected a
112 BigInt (or vice versa) and the upgrade/downgrad path was missing. This
113 is a bug, please report it so that we can fix it.
114
115 You might consider using just Math::BigInt or Math::BigFloat, since
116 they allow you finer control over what get's done in which
117 module/space. For instance, simple loop counters will be Math::BigInts
118 under "use bignum;" and this is slower than keeping them as Perl
119 scalars:
120
121 perl -Mbignum -le 'for ($i = 0; $i < 10; $i++) { print ref($i); }'
122
123 Please note the following does not work as expected (prints nothing),
124 since overloading of '..' is not yet possible in Perl (as of v5.8.0):
125
126 perl -Mbignum -le 'for (1..2) { print ref($_); }'
127
128 Options
129 bignum recognizes some options that can be passed while loading it via
130 use. The options can (currently) be either a single letter form, or
131 the long form. The following options exist:
132
133 a or accuracy
134 This sets the accuracy for all math operations. The argument must be
135 greater than or equal to zero. See Math::BigInt's bround() function
136 for details.
137
138 perl -Mbignum=a,50 -le 'print sqrt(20)'
139
140 Note that setting precision and accuracy at the same time is not
141 possible.
142
143 p or precision
144 This sets the precision for all math operations. The argument can be
145 any integer. Negative values mean a fixed number of digits after the
146 dot, while a positive value rounds to this digit left from the dot. 0
147 or 1 mean round to integer. See Math::BigInt's bfround() function for
148 details.
149
150 perl -Mbignum=p,-50 -le 'print sqrt(20)'
151
152 Note that setting precision and accuracy at the same time is not
153 possible.
154
155 t or trace
156 This enables a trace mode and is primarily for debugging bignum or
157 Math::BigInt/Math::BigFloat.
158
159 l or lib
160 Load a different math lib, see "Math Library".
161
162 perl -Mbignum=l,GMP -e 'print 2 ** 512'
163
164 Currently there is no way to specify more than one library on the
165 command line. This means the following does not work:
166
167 perl -Mbignum=l,GMP,Pari -e 'print 2 ** 512'
168
169 This will be hopefully fixed soon ;)
170
171 hex
172 Override the built-in hex() method with a version that can handle big
173 integers. Note that under Perl older than v5.9.4, this will be global
174 and cannot be disabled with "no bigint;".
175
176 oct
177 Override the built-in oct() method with a version that can handle big
178 integers. Note that under Perl older than v5.9.4, this will be global
179 and cannot be disabled with "no bigint;".
180
181 v or version
182 This prints out the name and version of all modules used and then
183 exits.
184
185 perl -Mbignum=v
186
187 Methods
188 Beside import() and AUTOLOAD() there are only a few other methods.
189
190 Since all numbers are now objects, you can use all functions that are
191 part of the BigInt or BigFloat API. It is wise to use only the bxxx()
192 notation, and not the fxxx() notation, though. This makes it possible
193 that the underlying object might morph into a different class than
194 BigFloat.
195
196 Caveats
197 But a warning is in order. When using the following to make a copy of a
198 number, only a shallow copy will be made.
199
200 $x = 9; $y = $x;
201 $x = $y = 7;
202
203 If you want to make a real copy, use the following:
204
205 $y = $x->copy();
206
207 Using the copy or the original with overloaded math is okay, e.g. the
208 following work:
209
210 $x = 9; $y = $x;
211 print $x + 1, " ", $y,"\n"; # prints 10 9
212
213 but calling any method that modifies the number directly will result in
214 both the original and the copy being destroyed:
215
216 $x = 9; $y = $x;
217 print $x->badd(1), " ", $y,"\n"; # prints 10 10
218
219 $x = 9; $y = $x;
220 print $x->binc(1), " ", $y,"\n"; # prints 10 10
221
222 $x = 9; $y = $x;
223 print $x->bmul(2), " ", $y,"\n"; # prints 18 18
224
225 Using methods that do not modify, but test the contents works:
226
227 $x = 9; $y = $x;
228 $z = 9 if $x->is_zero(); # works fine
229
230 See the documentation about the copy constructor and "=" in overload,
231 as well as the documentation in BigInt for further details.
232
233 inf()
234 A shortcut to return Math::BigInt->binf(). Useful because Perl does
235 not always handle bareword "inf" properly.
236
237 NaN()
238 A shortcut to return Math::BigInt->bnan(). Useful because Perl does
239 not always handle bareword "NaN" properly.
240
241 e
242 # perl -Mbignum=e -wle 'print e'
243
244 Returns Euler's number "e", aka exp(1).
245
246 PI()
247 # perl -Mbignum=PI -wle 'print PI'
248
249 Returns PI.
250
251 bexp()
252 bexp($power,$accuracy);
253
254 Returns Euler's number "e" raised to the appropriate power, to the
255 wanted accuracy.
256
257 Example:
258
259 # perl -Mbignum=bexp -wle 'print bexp(1,80)'
260
261 bpi()
262 bpi($accuracy);
263
264 Returns PI to the wanted accuracy.
265
266 Example:
267
268 # perl -Mbignum=bpi -wle 'print bpi(80)'
269
270 upgrade()
271 Return the class that numbers are upgraded to, is in fact returning
272 $Math::BigInt::upgrade.
273
274 in_effect()
275 use bignum;
276
277 print "in effect\n" if bignum::in_effect; # true
278 {
279 no bignum;
280 print "in effect\n" if bignum::in_effect; # false
281 }
282
283 Returns true or false if "bignum" is in effect in the current scope.
284
285 This method only works on Perl v5.9.4 or later.
286
287 Math Library
288 Math with the numbers is done (by default) by a module called
289 Math::BigInt::Calc. This is equivalent to saying:
290
291 use bignum lib => 'Calc';
292
293 You can change this by using:
294
295 use bignum lib => 'GMP';
296
297 The following would first try to find Math::BigInt::Foo, then
298 Math::BigInt::Bar, and when this also fails, revert to
299 Math::BigInt::Calc:
300
301 use bignum lib => 'Foo,Math::BigInt::Bar';
302
303 Please see respective module documentation for further details.
304
305 Using "lib" warns if none of the specified libraries can be found and
306 Math::BigInt did fall back to one of the default libraries. To
307 suppress this warning, use "try" instead:
308
309 use bignum try => 'GMP';
310
311 If you want the code to die instead of falling back, use "only"
312 instead:
313
314 use bignum only => 'GMP';
315
316 INTERNAL FORMAT
317 The numbers are stored as objects, and their internals might change at
318 anytime, especially between math operations. The objects also might
319 belong to different classes, like Math::BigInt, or Math::BigFLoat.
320 Mixing them together, even with normal scalars is not extraordinary,
321 but normal and expected.
322
323 You should not depend on the internal format, all accesses must go
324 through accessor methods. E.g. looking at $x->{sign} is not a bright
325 idea since there is no guaranty that the object in question has such a
326 hashkey, nor is a hash underneath at all.
327
328 SIGN
329 The sign is either '+', '-', 'NaN', '+inf' or '-inf' and stored
330 separately. You can access it with the sign() method.
331
332 A sign of 'NaN' is used to represent the result when input arguments
333 are not numbers or as a result of 0/0. '+inf' and '-inf' represent plus
334 respectively minus infinity. You will get '+inf' when dividing a
335 positive number by 0, and '-inf' when dividing any negative number by
336 0.
337
339 in_effect()
340 This method only works on Perl v5.9.4 or later.
341
342 hex()/oct()
343 "bigint" overrides these routines with versions that can also handle
344 big integer values. Under Perl prior to version v5.9.4, however, this
345 will not happen unless you specifically ask for it with the two
346 import tags "hex" and "oct" - and then it will be global and cannot
347 be disabled inside a scope with "no bigint":
348
349 use bigint qw/hex oct/;
350
351 print hex("0x1234567890123456");
352 {
353 no bigint;
354 print hex("0x1234567890123456");
355 }
356
357 The second call to hex() will warn about a non-portable constant.
358
359 Compare this to:
360
361 use bigint;
362
363 # will warn only under older than v5.9.4
364 print hex("0x1234567890123456");
365
367 "bignum" is just a thin wrapper around various modules of the
368 Math::BigInt family. Think of it as the head of the family, who runs
369 the shop, and orders the others to do the work.
370
371 The following modules are currently used by bignum:
372
373 Math::BigInt::Lite (for speed, and only if it is loadable)
374 Math::BigInt
375 Math::BigFloat
376
378 Some cool command line examples to impress the Python crowd ;)
379
380 perl -Mbignum -le 'print sqrt(33)'
381 perl -Mbignum -le 'print 2*255'
382 perl -Mbignum -le 'print 4.5+2*255'
383 perl -Mbignum -le 'print 3/7 + 5/7 + 8/3'
384 perl -Mbignum -le 'print 123->is_odd()'
385 perl -Mbignum -le 'print log(2)'
386 perl -Mbignum -le 'print exp(1)'
387 perl -Mbignum -le 'print 2 ** 0.5'
388 perl -Mbignum=a,65 -le 'print 2 ** 0.2'
389 perl -Mbignum=a,65,l,GMP -le 'print 7 ** 7777'
390
392 This program is free software; you may redistribute it and/or modify it
393 under the same terms as Perl itself.
394
396 Especially bigrat as in "perl -Mbigrat -le 'print 1/3+1/4'".
397
398 Math::BigFloat, Math::BigInt, Math::BigRat and Math::Big as well as
399 Math::BigInt::BitVect, Math::BigInt::Pari and Math::BigInt::GMP.
400
402 (C) by Tels <http://bloodgate.com/> in early 2002 - 2007.
403
404
405
406perl v5.16.3 2013-03-04 bignum(3pm)