1bigint(3pm) Perl Programmers Reference Guide bigint(3pm)
2
3
4
6 bigint - Transparent BigInteger support for Perl
7
9 use bigint;
10
11 $x = 2 + 4.5,"\n"; # BigInt 6
12 print 2 ** 512,"\n"; # really is what you think it is
13 print inf + 42,"\n"; # inf
14 print NaN * 7,"\n"; # NaN
15
17 All operators (including basic math operations) are overloaded. Integer
18 constants are created as proper BigInts.
19
20 Floating point constants are truncated to integer. All results are also
21 truncated.
22
23 Options
24
25 bigint recognizes some options that can be passed while loading it via
26 use. The options can (currently) be either a single letter form, or
27 the long form. The following options exist:
28
29 a or accuracy
30 This sets the accuracy for all math operations. The argument must be
31 greater than or equal to zero. See Math::BigInt's bround() function
32 for details.
33
34 perl -Mbigint=a,2 -le 'print 12345+1'
35
36 p or precision
37 This sets the precision for all math operations. The argument can be
38 any integer. Negative values mean a fixed number of digits after the
39 dot, and are <B>ignored</B> since all operations happen in integer
40 space. A positive value rounds to this digit left from the dot. 0 or
41 1 mean round to integer and are ignore like negative values.
42
43 See Math::BigInt's bfround() function for details.
44
45 perl -Mbignum=p,5 -le 'print 123456789+123'
46
47 t or trace
48 This enables a trace mode and is primarily for debugging bigint or
49 Math::BigInt.
50
51 l or lib
52 Load a different math lib, see "MATH LIBRARY".
53
54 perl -Mbigint=l,GMP -e 'print 2 ** 512'
55
56 Currently there is no way to specify more than one library on the
57 command line. This will be hopefully fixed soon ;)
58
59 v or version
60 This prints out the name and version of all modules used and then
61 exits.
62
63 perl -Mbigint=v
64
65 Math Library
66
67 Math with the numbers is done (by default) by a module called
68 Math::BigInt::Calc. This is equivalent to saying:
69
70 use bigint lib => 'Calc';
71
72 You can change this by using:
73
74 use bigint lib => 'BitVect';
75
76 The following would first try to find Math::BigInt::Foo, then
77 Math::BigInt::Bar, and when this also fails, revert to Math::Big‐
78 Int::Calc:
79
80 use bigint lib => 'Foo,Math::BigInt::Bar';
81
82 Please see respective module documentation for further details.
83
84 Internal Format
85
86 The numbers are stored as objects, and their internals might change at
87 anytime, especially between math operations. The objects also might
88 belong to different classes, like Math::BigInt, or Math::BigInt::Lite.
89 Mixing them together, even with normal scalars is not extraordinary,
90 but normal and expected.
91
92 You should not depend on the internal format, all accesses must go
93 through accessor methods. E.g. looking at $x->{sign} is not a good idea
94 since there is no guaranty that the object in question has such a hash
95 key, nor is a hash underneath at all.
96
97 Sign
98
99 The sign is either '+', '-', 'NaN', '+inf' or '-inf'. You can access
100 it with the sign() method.
101
102 A sign of 'NaN' is used to represent the result when input arguments
103 are not numbers or as a result of 0/0. '+inf' and '-inf' represent plus
104 respectively minus infinity. You will get '+inf' when dividing a posi‐
105 tive number by 0, and '-inf' when dividing any negative number by 0.
106
107 Methods
108
109 Since all numbers are now objects, you can use all functions that are
110 part of the BigInt API. You can only use the bxxx() notation, and not
111 the fxxx() notation, though.
112
113 Caveat
114
115 But a warning is in order. When using the following to make a copy of a
116 number, only a shallow copy will be made.
117
118 $x = 9; $y = $x;
119 $x = $y = 7;
120
121 Using the copy or the original with overloaded math is okay, e.g. the
122 following work:
123
124 $x = 9; $y = $x;
125 print $x + 1, " ", $y,"\n"; # prints 10 9
126
127 but calling any method that modifies the number directly will result in
128 both the original and the copy beeing destroyed:
129
130 $x = 9; $y = $x;
131 print $x->badd(1), " ", $y,"\n"; # prints 10 10
132
133 $x = 9; $y = $x;
134 print $x->binc(1), " ", $y,"\n"; # prints 10 10
135
136 $x = 9; $y = $x;
137 print $x->bmul(2), " ", $y,"\n"; # prints 18 18
138
139 Using methods that do not modify, but testthe contents works:
140
141 $x = 9; $y = $x;
142 $z = 9 if $x->is_zero(); # works fine
143
144 See the documentation about the copy constructor and "=" in overload,
145 as well as the documentation in BigInt for further details.
146
148 "bigint" is just a thin wrapper around various modules of the
149 Math::BigInt family. Think of it as the head of the family, who runs
150 the shop, and orders the others to do the work.
151
152 The following modules are currently used by bigint:
153
154 Math::BigInt::Lite (for speed, and only if it is loadable)
155 Math::BigInt
156
158 Some cool command line examples to impress the Python crowd ;) You
159 might want to compare them to the results under -Mbignum or -Mbigrat:
160
161 perl -Mbigint -le 'print sqrt(33)'
162 perl -Mbigint -le 'print 2*255'
163 perl -Mbigint -le 'print 4.5+2*255'
164 perl -Mbigint -le 'print 3/7 + 5/7 + 8/3'
165 perl -Mbigint -le 'print 123->is_odd()'
166 perl -Mbigint -le 'print log(2)'
167 perl -Mbigint -le 'print 2 ** 0.5'
168 perl -Mbigint=a,65 -le 'print 2 ** 0.2'
169
171 This program is free software; you may redistribute it and/or modify it
172 under the same terms as Perl itself.
173
175 Especially bigrat as in "perl -Mbigrat -le 'print 1/3+1/4'" and bignum
176 as in "perl -Mbignum -le 'print sqrt(2)'".
177
178 Math::BigInt, Math::BigRat and Math::Big as well as Math::Big‐
179 Int::BitVect, Math::BigInt::Pari and Math::BigInt::GMP.
180
182 (C) by Tels <http://bloodgate.com/> in early 2002 - 2005.
183
184
185
186perl v5.8.8 2001-09-21 bigint(3pm)