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 $x = 2 + 4.5,"\n"; # BigFloat 6.5
12 print 1/3 + 1/4,"\n"; # produces 7/12
13
15 All operators (inlcuding basic math operations) are overloaded. Integer
16 and floating-point constants are created as proper BigInts or
17 BigFloats, respectively.
18
19 Other than bignum, this module upgrades to Math::BigRat, meaning that
20 instead of 2.5 you will get 2+1/2 as output.
21
22 Modules Used
23
24 "bigrat" is just a thin wrapper around various modules of the
25 Math::BigInt family. Think of it as the head of the family, who runs
26 the shop, and orders the others to do the work.
27
28 The following modules are currently used by bignum:
29
30 Math::BigInt::Lite (for speed, and only if it is loadable)
31 Math::BigInt
32 Math::BigFloat
33 Math::BigRat
34
35 Math Library
36
37 Math with the numbers is done (by default) by a module called
38 Math::BigInt::Calc. This is equivalent to saying:
39
40 use bigrat lib => 'Calc';
41
42 You can change this by using:
43
44 use bigrat lib => 'BitVect';
45
46 The following would first try to find Math::BigInt::Foo, then
47 Math::BigInt::Bar, and when this also fails, revert to Math::Big‐
48 Int::Calc:
49
50 use bigrat lib => 'Foo,Math::BigInt::Bar';
51
52 Please see respective module documentation for further details.
53
54 Sign
55
56 The sign is either '+', '-', 'NaN', '+inf' or '-inf'.
57
58 A sign of 'NaN' is used to represent the result when input arguments
59 are not numbers or as a result of 0/0. '+inf' and '-inf' represent plus
60 respectively minus infinity. You will get '+inf' when dividing a posi‐
61 tive number by 0, and '-inf' when dividing any negative number by 0.
62
63 Methods
64
65 Since all numbers are not objects, you can use all functions that are
66 part of the BigInt or BigFloat API. It is wise to use only the bxxx()
67 notation, and not the fxxx() notation, though. This makes you inde‐
68 pended on the fact that the underlying object might morph into a dif‐
69 ferent class than BigFloat.
70
71 Cavaet
72
73 But a warning is in order. When using the following to make a copy of a
74 number, only a shallow copy will be made.
75
76 $x = 9; $y = $x;
77 $x = $y = 7;
78
79 If you want to make a real copy, use the following:
80
81 $y = $x->copy();
82
83 Using the copy or the original with overloaded math is okay, e.g. the
84 following work:
85
86 $x = 9; $y = $x;
87 print $x + 1, " ", $y,"\n"; # prints 10 9
88
89 but calling any method that modifies the number directly will result in
90 both the original and the copy beeing destroyed:
91
92 $x = 9; $y = $x;
93 print $x->badd(1), " ", $y,"\n"; # prints 10 10
94
95 $x = 9; $y = $x;
96 print $x->binc(1), " ", $y,"\n"; # prints 10 10
97
98 $x = 9; $y = $x;
99 print $x->bmul(2), " ", $y,"\n"; # prints 18 18
100
101 Using methods that do not modify, but testthe contents works:
102
103 $x = 9; $y = $x;
104 $z = 9 if $x->is_zero(); # works fine
105
106 See the documentation about the copy constructor and "=" in overload,
107 as well as the documentation in BigInt for further details.
108
109 Options
110
111 bignum recognizes some options that can be passed while loading it via
112 use. The options can (currently) be either a single letter form, or
113 the long form. The following options exist:
114
115 a or accuracy
116 This sets the accuracy for all math operations. The argument must be
117 greater than or equal to zero. See Math::BigInt's bround() function
118 for details.
119
120 perl -Mbigrat=a,50 -le 'print sqrt(20)'
121
122 p or precision
123 This sets the precision for all math operations. The argument can be
124 any integer. Negative values mean a fixed number of digits after the
125 dot, while a positive value rounds to this digit left from the dot. 0
126 or 1 mean round to integer. See Math::BigInt's bfround() function for
127 details.
128
129 perl -Mbigrat=p,-50 -le 'print sqrt(20)'
130
131 t or trace
132 This enables a trace mode and is primarily for debugging bignum or
133 Math::BigInt/Math::BigFloat.
134
135 l or lib
136 Load a different math lib, see "MATH LIBRARY".
137
138 perl -Mbigrat=l,GMP -e 'print 2 ** 512'
139
140 Currently there is no way to specify more than one library on the
141 command line. This will be hopefully fixed soon ;)
142
143 v or version
144 This prints out the name and version of all modules used and then
145 exits.
146
147 perl -Mbigrat=v
148
150 perl -Mbigrat -le 'print sqrt(33)'
151 perl -Mbigrat -le 'print 2*255'
152 perl -Mbigrat -le 'print 4.5+2*255'
153 perl -Mbigrat -le 'print 3/7 + 5/7 + 8/3'
154 perl -Mbigrat -le 'print 12->is_odd()';
155
157 This program is free software; you may redistribute it and/or modify it
158 under the same terms as Perl itself.
159
161 Especially bignum.
162
163 Math::BigFloat, Math::BigInt, Math::BigRat and Math::Big as well as
164 Math::BigInt::BitVect, Math::BigInt::Pari and Math::BigInt::GMP.
165
167 (C) by Tels <http://bloodgate.com/> in early 2002 - 2005.
168
169
170
171perl v5.8.8 2001-09-21 bigrat(3pm)