1bigrat(3pm) Perl Programmers Reference Guide bigrat(3pm)
2
3
4
6 bigrat - Transparent BigNumber/BigRational support for Perl
7
9 use bigrat;
10
11 print 2 + 4.5,"\n"; # BigFloat 6.5
12 print 1/3 + 1/4,"\n"; # produces 7/12
13
14 {
15 no bigrat;
16 print 1/3,"\n"; # 0.33333...
17 }
18
19 # Note that this will make hex() and oct() be globally overridden:
20 use bigrat qw/hex oct/;
21 print hex("0x1234567890123490"),"\n";
22 print oct("01234567890123490"),"\n";
23
25 All operators (including basic math operations) are overloaded. Integer
26 and floating-point constants are created as proper BigInts or
27 BigFloats, respectively.
28
29 Other than bignum, this module upgrades to Math::BigRat, meaning that
30 instead of 2.5 you will get 2+1/2 as output.
31
32 Modules Used
33 "bigrat" is just a thin wrapper around various modules of the
34 Math::BigInt family. Think of it as the head of the family, who runs
35 the shop, and orders the others to do the work.
36
37 The following modules are currently used by bignum:
38
39 Math::BigInt::Lite (for speed, and only if it is loadable)
40 Math::BigInt
41 Math::BigFloat
42 Math::BigRat
43
44 Math Library
45 Math with the numbers is done (by default) by a module called
46 Math::BigInt::Calc. This is equivalent to saying:
47
48 use bigrat lib => 'Calc';
49
50 You can change this by using:
51
52 use bignum lib => 'GMP';
53
54 The following would first try to find Math::BigInt::Foo, then
55 Math::BigInt::Bar, and when this also fails, revert to
56 Math::BigInt::Calc:
57
58 use bigrat lib => 'Foo,Math::BigInt::Bar';
59
60 Using "lib" warns if none of the specified libraries can be found and
61 Math::BigInt did fall back to one of the default libraries. To
62 suppress this warning, use "try" instead:
63
64 use bignum try => 'GMP';
65
66 If you want the code to die instead of falling back, use "only"
67 instead:
68
69 use bignum only => 'GMP';
70
71 Please see respective module documentation for further details.
72
73 Sign
74 The sign is either '+', '-', 'NaN', '+inf' or '-inf'.
75
76 A sign of 'NaN' is used to represent the result when input arguments
77 are not numbers or as a result of 0/0. '+inf' and '-inf' represent plus
78 respectively minus infinity. You will get '+inf' when dividing a
79 positive number by 0, and '-inf' when dividing any negative number by
80 0.
81
82 Methods
83 Since all numbers are not objects, you can use all functions that are
84 part of the BigInt or BigFloat API. It is wise to use only the bxxx()
85 notation, and not the fxxx() notation, though. This makes you
86 independent on the fact that the underlying object might morph into a
87 different class than BigFloat.
88
89 inf()
90 A shortcut to return Math::BigInt->binf(). Useful because Perl does
91 not always handle bareword "inf" properly.
92
93 NaN()
94 A shortcut to return Math::BigInt->bnan(). Useful because Perl does
95 not always handle bareword "NaN" properly.
96
97 e
98 # perl -Mbigrat=e -wle 'print e'
99
100 Returns Euler's number "e", aka exp(1).
101
102 PI
103 # perl -Mbigrat=PI -wle 'print PI'
104
105 Returns PI.
106
107 bexp()
108 bexp($power,$accuracy);
109
110 Returns Euler's number "e" raised to the appropriate power, to the
111 wanted accuracy.
112
113 Example:
114
115 # perl -Mbigrat=bexp -wle 'print bexp(1,80)'
116
117 bpi()
118 bpi($accuracy);
119
120 Returns PI to the wanted accuracy.
121
122 Example:
123
124 # perl -Mbigrat=bpi -wle 'print bpi(80)'
125
126 upgrade()
127 Return the class that numbers are upgraded to, is in fact returning
128 $Math::BigInt::upgrade.
129
130 in_effect()
131 use bigrat;
132
133 print "in effect\n" if bigrat::in_effect; # true
134 {
135 no bigrat;
136 print "in effect\n" if bigrat::in_effect; # false
137 }
138
139 Returns true or false if "bigrat" is in effect in the current scope.
140
141 This method only works on Perl v5.9.4 or later.
142
143 MATH LIBRARY
144 Math with the numbers is done (by default) by a module called
145
146 Caveat
147 But a warning is in order. When using the following to make a copy of a
148 number, only a shallow copy will be made.
149
150 $x = 9; $y = $x;
151 $x = $y = 7;
152
153 If you want to make a real copy, use the following:
154
155 $y = $x->copy();
156
157 Using the copy or the original with overloaded math is okay, e.g. the
158 following work:
159
160 $x = 9; $y = $x;
161 print $x + 1, " ", $y,"\n"; # prints 10 9
162
163 but calling any method that modifies the number directly will result in
164 both the original and the copy being destroyed:
165
166 $x = 9; $y = $x;
167 print $x->badd(1), " ", $y,"\n"; # prints 10 10
168
169 $x = 9; $y = $x;
170 print $x->binc(1), " ", $y,"\n"; # prints 10 10
171
172 $x = 9; $y = $x;
173 print $x->bmul(2), " ", $y,"\n"; # prints 18 18
174
175 Using methods that do not modify, but testthe contents works:
176
177 $x = 9; $y = $x;
178 $z = 9 if $x->is_zero(); # works fine
179
180 See the documentation about the copy constructor and "=" in overload,
181 as well as the documentation in BigInt for further details.
182
183 Options
184 bignum recognizes some options that can be passed while loading it via
185 use. The options can (currently) be either a single letter form, or
186 the long form. The following options exist:
187
188 a or accuracy
189 This sets the accuracy for all math operations. The argument must be
190 greater than or equal to zero. See Math::BigInt's bround() function
191 for details.
192
193 perl -Mbigrat=a,50 -le 'print sqrt(20)'
194
195 Note that setting precision and accuracy at the same time is not
196 possible.
197
198 p or precision
199 This sets the precision for all math operations. The argument can be
200 any integer. Negative values mean a fixed number of digits after the
201 dot, while a positive value rounds to this digit left from the dot. 0
202 or 1 mean round to integer. See Math::BigInt's bfround() function for
203 details.
204
205 perl -Mbigrat=p,-50 -le 'print sqrt(20)'
206
207 Note that setting precision and accuracy at the same time is not
208 possible.
209
210 t or trace
211 This enables a trace mode and is primarily for debugging bignum or
212 Math::BigInt/Math::BigFloat.
213
214 l or lib
215 Load a different math lib, see "MATH LIBRARY".
216
217 perl -Mbigrat=l,GMP -e 'print 2 ** 512'
218
219 Currently there is no way to specify more than one library on the
220 command line. This means the following does not work:
221
222 perl -Mbignum=l,GMP,Pari -e 'print 2 ** 512'
223
224 This will be hopefully fixed soon ;)
225
226 hex
227 Override the built-in hex() method with a version that can handle big
228 integers. Note that under Perl v5.9.4 or ealier, this will be global
229 and cannot be disabled with "no bigint;".
230
231 oct
232 Override the built-in oct() method with a version that can handle big
233 integers. Note that under Perl v5.9.4 or earlier, this will be global
234 and cannot be disabled with "no bigint;".
235
236 v or version
237 This prints out the name and version of all modules used and then
238 exits.
239
240 perl -Mbigrat=v
241
243 in_effect()
244 This method only works on Perl v5.9.4 or later.
245
246 hex()/oct()
247 "bigint" overrides these routines with versions that can also handle
248 big integer values. Under Perl prior to version v5.9.4, however, this
249 will not happen unless you specifically ask for it with the two
250 import tags "hex" and "oct" - and then it will be global and cannot
251 be disabled inside a scope with "no bigint":
252
253 use bigint qw/hex oct/;
254
255 print hex("0x1234567890123456");
256 {
257 no bigint;
258 print hex("0x1234567890123456");
259 }
260
261 The second call to hex() will warn about a non-portable constant.
262
263 Compare this to:
264
265 use bigint;
266
267 # will warn only under Perl older than v5.9.4
268 print hex("0x1234567890123456");
269
271 perl -Mbigrat -le 'print sqrt(33)'
272 perl -Mbigrat -le 'print 2*255'
273 perl -Mbigrat -le 'print 4.5+2*255'
274 perl -Mbigrat -le 'print 3/7 + 5/7 + 8/3'
275 perl -Mbigrat -le 'print 12->is_odd()';
276 perl -Mbignum=l,GMP -le 'print 7 ** 7777'
277
279 This program is free software; you may redistribute it and/or modify it
280 under the same terms as Perl itself.
281
283 Especially bignum.
284
285 Math::BigFloat, Math::BigInt, Math::BigRat and Math::Big as well as
286 Math::BigInt::BitVect, Math::BigInt::Pari and Math::BigInt::GMP.
287
289 (C) by Tels <http://bloodgate.com/> in early 2002 - 2007.
290
291
292
293perl v5.16.3 2013-03-04 bigrat(3pm)