1Math::BigRat(3pm) Perl Programmers Reference Guide Math::BigRat(3pm)
2
3
4
6 Math::BigRat - Arbitrary big rational numbers
7
9 use Math::BigRat;
10
11 my $x = Math::BigRat->new('3/7'); $x += '5/9';
12
13 print $x->bstr(),"\n";
14 print $x ** 2,"\n";
15
16 my $y = Math::BigRat->new('inf');
17 print "$y ", ($y->is_inf ? 'is' : 'is not') , " infinity\n";
18
19 my $z = Math::BigRat->new(144); $z->bsqrt();
20
22 Math::BigRat complements Math::BigInt and Math::BigFloat by providing
23 support for arbitrary big rational numbers.
24
25 MATH LIBRARY
26
27 Math with the numbers is done (by default) by a module called
28 Math::BigInt::Calc. This is equivalent to saying:
29
30 use Math::BigRat lib => 'Calc';
31
32 You can change this by using:
33
34 use Math::BigRat lib => 'BitVect';
35
36 The following would first try to find Math::BigInt::Foo, then
37 Math::BigInt::Bar, and when this also fails, revert to Math::Big‐
38 Int::Calc:
39
40 use Math::BigRat lib => 'Foo,Math::BigInt::Bar';
41
42 Calc.pm uses as internal format an array of elements of some decimal
43 base (usually 1e7, but this might be different for some systems) with
44 the least significant digit first, while BitVect.pm uses a bit vector
45 of base 2, most significant bit first. Other modules might use even
46 different means of representing the numbers. See the respective module
47 documentation for further details.
48
49 Currently the following replacement libraries exist, search for them at
50 CPAN:
51
52 Math::BigInt::BitVect
53 Math::BigInt::GMP
54 Math::BigInt::Pari
55 Math::BigInt::FastCalc
56
58 Any methods not listed here are dervied from Math::BigFloat (or
59 Math::BigInt), so make sure you check these two modules for further
60 information.
61
62 new()
63
64 $x = Math::BigRat->new('1/3');
65
66 Create a new Math::BigRat object. Input can come in various forms:
67
68 $x = Math::BigRat->new(123); # scalars
69 $x = Math::BigRat->new('inf'); # infinity
70 $x = Math::BigRat->new('123.3'); # float
71 $x = Math::BigRat->new('1/3'); # simple string
72 $x = Math::BigRat->new('1 / 3'); # spaced
73 $x = Math::BigRat->new('1 / 0.1'); # w/ floats
74 $x = Math::BigRat->new(Math::BigInt->new(3)); # BigInt
75 $x = Math::BigRat->new(Math::BigFloat->new('3.1')); # BigFloat
76 $x = Math::BigRat->new(Math::BigInt::Lite->new('2')); # BigLite
77
78 # You can also give D and N as different objects:
79 $x = Math::BigRat->new(
80 Math::BigInt->new(-123),
81 Math::BigInt->new(7),
82 ); # => -123/7
83
84 numerator()
85
86 $n = $x->numerator();
87
88 Returns a copy of the numerator (the part above the line) as signed
89 BigInt.
90
91 denominator()
92
93 $d = $x->denominator();
94
95 Returns a copy of the denominator (the part under the line) as positive
96 BigInt.
97
98 parts()
99
100 ($n,$d) = $x->parts();
101
102 Return a list consisting of (signed) numerator and (unsigned) denomina‐
103 tor as BigInts.
104
105 as_int()
106
107 $x = Math::BigRat->new('13/7');
108 print $x->as_int(),"\n"; # '1'
109
110 Returns a copy of the object as BigInt, truncated to an integer.
111
112 "as_number()" is an alias for "as_int()".
113
114 as_hex()
115
116 $x = Math::BigRat->new('13');
117 print $x->as_hex(),"\n"; # '0xd'
118
119 Returns the BigRat as hexadecimal string. Works only for integers.
120
121 as_bin()
122
123 $x = Math::BigRat->new('13');
124 print $x->as_bin(),"\n"; # '0x1101'
125
126 Returns the BigRat as binary string. Works only for integers.
127
128 bfac()
129
130 $x->bfac();
131
132 Calculates the factorial of $x. For instance:
133
134 print Math::BigRat->new('3/1')->bfac(),"\n"; # 1*2*3
135 print Math::BigRat->new('5/1')->bfac(),"\n"; # 1*2*3*4*5
136
137 Works currently only for integers.
138
139 blog()
140
141 Is not yet implemented.
142
143 bround()/round()/bfround()
144
145 Are not yet implemented.
146
147 bmod()
148
149 use Math::BigRat;
150 my $x = Math::BigRat->new('7/4');
151 my $y = Math::BigRat->new('4/3');
152 print $x->bmod($y);
153
154 Set $x to the remainder of the division of $x by $y.
155
156 is_one()
157
158 print "$x is 1\n" if $x->is_one();
159
160 Return true if $x is exactly one, otherwise false.
161
162 is_zero()
163
164 print "$x is 0\n" if $x->is_zero();
165
166 Return true if $x is exactly zero, otherwise false.
167
168 is_pos()
169
170 print "$x is >= 0\n" if $x->is_positive();
171
172 Return true if $x is positive (greater than or equal to zero), other‐
173 wise false. Please note that '+inf' is also positive, while 'NaN' and
174 '-inf' aren't.
175
176 "is_positive()" is an alias for "is_pos()".
177
178 is_neg()
179
180 print "$x is < 0\n" if $x->is_negative();
181
182 Return true if $x is negative (smaller than zero), otherwise false.
183 Please note that '-inf' is also negative, while 'NaN' and '+inf'
184 aren't.
185
186 "is_negative()" is an alias for "is_neg()".
187
188 is_int()
189
190 print "$x is an integer\n" if $x->is_int();
191
192 Return true if $x has a denominator of 1 (e.g. no fraction parts), oth‐
193 erwise false. Please note that '-inf', 'inf' and 'NaN' aren't integer.
194
195 is_odd()
196
197 print "$x is odd\n" if $x->is_odd();
198
199 Return true if $x is odd, otherwise false.
200
201 is_even()
202
203 print "$x is even\n" if $x->is_even();
204
205 Return true if $x is even, otherwise false.
206
207 bceil()
208
209 $x->bceil();
210
211 Set $x to the next bigger integer value (e.g. truncate the number to
212 integer and then increment it by one).
213
214 bfloor()
215
216 $x->bfloor();
217
218 Truncate $x to an integer value.
219
220 bsqrt()
221
222 $x->bsqrt();
223
224 Calculate the square root of $x.
225
226 config
227
228 use Data::Dumper;
229
230 print Dumper ( Math::BigRat->config() );
231 print Math::BigRat->config()->{lib},"\n";
232
233 Returns a hash containing the configuration, e.g. the version number,
234 lib loaded etc. The following hash keys are currently filled in with
235 the appropriate information.
236
237 key RO/RW Description
238 Example
239 ============================================================
240 lib RO Name of the Math library
241 Math::BigInt::Calc
242 lib_version RO Version of 'lib'
243 0.30
244 class RO The class of config you just called
245 Math::BigRat
246 version RO version number of the class you used
247 0.10
248 upgrade RW To which class numbers are upgraded
249 undef
250 downgrade RW To which class numbers are downgraded
251 undef
252 precision RW Global precision
253 undef
254 accuracy RW Global accuracy
255 undef
256 round_mode RW Global round mode
257 even
258 div_scale RW Fallback acccuracy for div
259 40
260 trap_nan RW Trap creation of NaN (undef = no)
261 undef
262 trap_inf RW Trap creation of +inf/-inf (undef = no)
263 undef
264
265 By passing a reference to a hash you may set the configuration values.
266 This works only for values that a marked with a "RW" above, anything
267 else is read-only.
268
270 Some things are not yet implemented, or only implemented half-way:
271
272 inf handling (partial)
273 NaN handling (partial)
274 rounding (not implemented except for bceil/bfloor)
275 $x ** $y where $y is not an integer
276 bmod(), blog(), bmodinv() and bmodpow() (partial)
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 Math::BigFloat and Math::Big as well as Math::BigInt::BitVect,
284 Math::BigInt::Pari and Math::BigInt::GMP.
285
286 See <http://search.cpan.org/search?dist=bignum> for a way to use
287 Math::BigRat.
288
289 The package at <http://search.cpan.org/search?dist=Math%3A%3ABigRat>
290 may contain more documentation and examples as well as testcases.
291
293 (C) by Tels <http://bloodgate.com/> 2001 - 2005.
294
295
296
297perl v5.8.8 2001-09-21 Math::BigRat(3pm)