1bigrat(3)             User Contributed Perl Documentation            bigrat(3)
2
3
4

NAME

6       bigrat - Transparent BigNumber/BigRational support for Perl
7

SYNOPSIS

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         # Import into current package:
20         use bigrat qw/hex oct/;
21         print hex("0x1234567890123490"),"\n";
22         print oct("01234567890123490"),"\n";
23

DESCRIPTION

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         numbers. This overrides it by exporting it to the current package.
229         Under Perl v5.10.0 and higher, this is not so necessary, as hex() is
230         lexically overridden in the current scope whenever the bigrat pragma
231         is active.
232
233       oct
234         Override the built-in oct() method with a version that can handle big
235         numbers. This overrides it by exporting it to the current package.
236         Under Perl v5.10.0 and higher, this is not so necessary, as oct() is
237         lexically overridden in the current scope whenever the bigrat pragma
238         is active.
239
240       v or version
241         This prints out the name and version of all modules used and then
242         exits.
243
244                 perl -Mbigrat=v
245

CAVEATS

247       Operator vs literal overloading
248         "bigrat" works by overloading handling of integer and floating point
249         literals, converting them to Math::BigInt or Math::BigRat objects.
250
251         This means that arithmetic involving only string values or string
252         literals will be performed using Perl's built-in operators.
253
254         For example:
255
256             use bigrat;
257             my $x = "900000000000000009";
258             my $y = "900000000000000007";
259             print $x - $y;
260
261         will output 0 on default 32-bit builds, since "bigrat" never sees the
262         string literals.  To ensure the expression is all treated as
263         "Math::BigInt" or "Math::BigRat" objects, use a literal number in the
264         expression:
265
266             print +(0+$x) - $y;
267
268       in_effect()
269         This method only works on Perl v5.9.4 or later.
270
271       hex()/oct()
272         "bigint" overrides these routines with versions that can also handle
273         big integer values. Under Perl prior to version v5.9.4, however, this
274         will not happen unless you specifically ask for it with the two
275         import tags "hex" and "oct" - and then it will be global and cannot
276         be disabled inside a scope with "no bigint":
277
278                 use bigint qw/hex oct/;
279
280                 print hex("0x1234567890123456");
281                 {
282                         no bigint;
283                         print hex("0x1234567890123456");
284                 }
285
286         The second call to hex() will warn about a non-portable constant.
287
288         Compare this to:
289
290                 use bigint;
291
292                 # will warn only under Perl older than v5.9.4
293                 print hex("0x1234567890123456");
294

EXAMPLES

296               perl -Mbigrat -le 'print sqrt(33)'
297               perl -Mbigrat -le 'print 2*255'
298               perl -Mbigrat -le 'print 4.5+2*255'
299               perl -Mbigrat -le 'print 3/7 + 5/7 + 8/3'
300               perl -Mbigrat -le 'print 12->is_odd()';
301               perl -Mbignum=l,GMP -le 'print 7 ** 7777'
302

BUGS

304       For information about bugs and how to report them, see the BUGS section
305       in the documentation available with the perldoc command.
306
307           perldoc bignum
308

SUPPORT

310       You can find documentation for this module with the perldoc command.
311
312           perldoc bigrat
313
314       For more information, see the SUPPORT section in the documentation
315       available with the perldoc command.
316
317           perldoc bignum
318

LICENSE

320       This program is free software; you may redistribute it and/or modify it
321       under the same terms as Perl itself.
322

SEE ALSO

324       bignum and bigint.
325
326       Math::BigInt, Math::BigFloat, Math::BigRat and Math::Big as well as
327       Math::BigInt::FastCalc, Math::BigInt::Pari and Math::BigInt::GMP.
328

AUTHORS

330       ·   (C) by Tels <http://bloodgate.com/> in early 2002 - 2007.
331
332       ·   Peter John Acklam <pjacklam@gmail.com<gt>, 2014-.
333
334
335
336perl v5.28.1                      2018-10-09                         bigrat(3)
Impressum