Debian Codegraph @

upstream/debian-unstable/man3/bigrat.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "bigrat 3perl"
58 .TH bigrat 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 bigrat \- transparent big rational number support for Perl
65 .SH SYNOPSIS
66 .IX Header "SYNOPSIS"
67 .Vb 1
68 \& use bigrat;
69 \&
70 \& print 2 + 4.5; # Math::BigRat 13/2
71 \& print 1/3 + 1/4; # Math::BigRat 7/12
72 \& print inf + 42; # Math::BigRat inf
73 \& print NaN * 7; # Math::BigRat NaN
74 \& print hex("0x1234567890123490"); # Perl v5.10.0 or later
75 \&
76 \& {
77 \& no bigrat;
78 \& print 1/3; # 0.33333...
79 \& }
80 \&
81 \& # for older Perls, import into current package:
82 \& use bigrat qw/hex oct/;
83 \& print hex("0x1234567890123490");
84 \& print oct("01234567890123490");
85 .Ve
86 .SH DESCRIPTION
87 .IX Header "DESCRIPTION"
88 All numeric literal in the given scope are converted to Math::BigRat objects.
89 .PP
90 All operators (including basic math operations) except the range operator \f(CW\*(C`..\*(C'\fR
91 are overloaded.
92 .PP
93 So, the following:
94 .PP
95 .Vb 2
96 \& use bigrat;
97 \& $x = 1234;
98 .Ve
99 .PP
100 creates a Math::BigRat and stores a reference to in \f(CW$x\fR. This happens
101 transparently and behind your back, so to speak.
102 .PP
103 You can see this with the following:
104 .PP
105 .Vb 1
106 \& perl \-Mbigrat \-le \*(Aqprint ref(1234)\*(Aq
107 .Ve
108 .PP
109 Since numbers are actually objects, you can call all the usual methods from
110 Math::BigRat on them. This even works to some extent on expressions:
111 .PP
112 .Vb 4
113 \& perl \-Mbigrat \-le \*(Aq$x = 1234; print $x\->bdec()\*(Aq
114 \& perl \-Mbigrat \-le \*(Aqprint 1234\->copy()\->binc();\*(Aq
115 \& perl \-Mbigrat \-le \*(Aqprint 1234\->copy()\->binc\->badd(6);\*(Aq
116 \& perl \-Mbigrat \-le \*(Aqprint +(1234)\->copy()\->binc()\*(Aq
117 .Ve
118 .PP
119 (Note that print doesn't do what you expect if the expression starts with
120 \&'(' hence the \f(CW\*(C`+\*(C'\fR)
121 .PP
122 You can even chain the operations together as usual:
123 .PP
124 .Vb 2
125 \& perl \-Mbigrat \-le \*(Aqprint 1234\->copy()\->binc\->badd(6);\*(Aq
126 \& 1241
127 .Ve
128 .PP
129 Please note the following does not work as expected (prints nothing), since
130 overloading of '..' is not yet possible in Perl (as of v5.8.0):
131 .PP
132 .Vb 1
133 \& perl \-Mbigrat \-le \*(Aqfor (1..2) { print ref($_); }\*(Aq
134 .Ve
135 .SS Options
136 .IX Subsection "Options"
137 \&\f(CW\*(C`bigrat\*(C'\fR recognizes some options that can be passed while loading it via
138 \&\f(CW\*(C`use\*(C'\fR. The following options exist:
139 .IP "a or accuracy" 4
140 .IX Item "a or accuracy"
141 This sets the accuracy for all math operations. The argument must be greater
142 than or equal to zero. See Math::BigInt's \fBbround()\fR method for details.
143 .Sp
144 .Vb 1
145 \& perl \-Mbigrat=a,50 \-le \*(Aqprint sqrt(20)\*(Aq
146 .Ve
147 .Sp
148 Note that setting precision and accuracy at the same time is not possible.
149 .IP "p or precision" 4
150 .IX Item "p or precision"
151 This sets the precision for all math operations. The argument can be any
152 integer. Negative values mean a fixed number of digits after the dot, while a
153 positive value rounds to this digit left from the dot. 0 means round to integer.
154 See Math::BigInt's \fBbfround()\fR method for details.
155 .Sp
156 .Vb 1
157 \& perl \-Mbigrat=p,\-50 \-le \*(Aqprint sqrt(20)\*(Aq
158 .Ve
159 .Sp
160 Note that setting precision and accuracy at the same time is not possible.
161 .IP "t or trace" 4
162 .IX Item "t or trace"
163 This enables a trace mode and is primarily for debugging.
164 .IP "l, lib, try, or only" 4
165 .IX Item "l, lib, try, or only"
166 Load a different math lib, see "Math Library".
167 .Sp
168 .Vb 4
169 \& perl \-Mbigrat=l,GMP \-e \*(Aqprint 2 ** 512\*(Aq
170 \& perl \-Mbigrat=lib,GMP \-e \*(Aqprint 2 ** 512\*(Aq
171 \& perl \-Mbigrat=try,GMP \-e \*(Aqprint 2 ** 512\*(Aq
172 \& perl \-Mbigrat=only,GMP \-e \*(Aqprint 2 ** 512\*(Aq
173 .Ve
174 .IP hex 4
175 .IX Item "hex"
176 Override the built-in \fBhex()\fR method with a version that can handle big numbers.
177 This overrides it by exporting it to the current package. Under Perl v5.10.0 and
178 higher, this is not so necessary, as \fBhex()\fR is lexically overridden in the
179 current scope whenever the \f(CW\*(C`bigrat\*(C'\fR pragma is active.
180 .IP oct 4
181 .IX Item "oct"
182 Override the built-in \fBoct()\fR method with a version that can handle big numbers.
183 This overrides it by exporting it to the current package. Under Perl v5.10.0 and
184 higher, this is not so necessary, as \fBoct()\fR is lexically overridden in the
185 current scope whenever the \f(CW\*(C`bigrat\*(C'\fR pragma is active.
186 .IP "v or version" 4
187 .IX Item "v or version"
188 this prints out the name and version of the modules and then exits.
189 .Sp
190 .Vb 1
191 \& perl \-Mbigrat=v
192 .Ve
193 .SS "Math Library"
194 .IX Subsection "Math Library"
195 Math with the numbers is done (by default) by a backend library module called
196 Math::BigInt::Calc. The default is equivalent to saying:
197 .PP
198 .Vb 1
199 \& use bigrat lib => \*(AqCalc\*(Aq;
200 .Ve
201 .PP
202 you can change this by using:
203 .PP
204 .Vb 1
205 \& use bigrat lib => \*(AqGMP\*(Aq;
206 .Ve
207 .PP
208 The following would first try to find Math::BigInt::Foo, then Math::BigInt::Bar,
209 and if this also fails, revert to Math::BigInt::Calc:
210 .PP
211 .Vb 1
212 \& use bigrat lib => \*(AqFoo,Math::BigInt::Bar\*(Aq;
213 .Ve
214 .PP
215 Using c<lib> warns if none of the specified libraries can be found and
216 Math::BigInt fell back to one of the default libraries. To suppress this
217 warning, use c<try> instead:
218 .PP
219 .Vb 1
220 \& use bigrat try => \*(AqGMP\*(Aq;
221 .Ve
222 .PP
223 If you want the code to die instead of falling back, use \f(CW\*(C`only\*(C'\fR instead:
224 .PP
225 .Vb 1
226 \& use bigrat only => \*(AqGMP\*(Aq;
227 .Ve
228 .PP
229 Please see the respective module documentation for further details.
230 .SS "Method calls"
231 .IX Subsection "Method calls"
232 Since all numbers are now objects, you can use all methods that are part of the
233 Math::BigRat API.
234 .PP
235 But a warning is in order. When using the following to make a copy of a number,
236 only a shallow copy will be made.
237 .PP
238 .Vb 2
239 \& $x = 9; $y = $x;
240 \& $x = $y = 7;
241 .Ve
242 .PP
243 Using the copy or the original with overloaded math is okay, e.g., the following
244 work:
245 .PP
246 .Vb 2
247 \& $x = 9; $y = $x;
248 \& print $x + 1, " ", $y,"\en"; # prints 10 9
249 .Ve
250 .PP
251 but calling any method that modifies the number directly will result in \fBboth\fR
252 the original and the copy being destroyed:
253 .PP
254 .Vb 2
255 \& $x = 9; $y = $x;
256 \& print $x\->badd(1), " ", $y,"\en"; # prints 10 10
257 \&
258 \& $x = 9; $y = $x;
259 \& print $x\->binc(1), " ", $y,"\en"; # prints 10 10
260 \&
261 \& $x = 9; $y = $x;
262 \& print $x\->bmul(2), " ", $y,"\en"; # prints 18 18
263 .Ve
264 .PP
265 Using methods that do not modify, but test that the contents works:
266 .PP
267 .Vb 2
268 \& $x = 9; $y = $x;
269 \& $z = 9 if $x\->is_zero(); # works fine
270 .Ve
271 .PP
272 See the documentation about the copy constructor and \f(CW\*(C`=\*(C'\fR in overload, as well
273 as the documentation in Math::BigFloat for further details.
274 .SS Methods
275 .IX Subsection "Methods"
276 .IP \fBinf()\fR 4
277 .IX Item "inf()"
278 A shortcut to return Math::BigRat\->\fBbinf()\fR. Useful because Perl does not always
279 handle bareword \f(CW\*(C`inf\*(C'\fR properly.
280 .IP \fBNaN()\fR 4
281 .IX Item "NaN()"
282 A shortcut to return Math::BigRat\->\fBbnan()\fR. Useful because Perl does not always
283 handle bareword \f(CW\*(C`NaN\*(C'\fR properly.
284 .IP e 4
285 .IX Item "e"
286 .Vb 1
287 \& # perl \-Mbigrat=e \-wle \*(Aqprint e\*(Aq
288 .Ve
289 .Sp
290 Returns Euler's number \f(CW\*(C`e\*(C'\fR, aka \fBexp\fR\|(1).
291 .IP PI 4
292 .IX Item "PI"
293 .Vb 1
294 \& # perl \-Mbigrat=PI \-wle \*(Aqprint PI\*(Aq
295 .Ve
296 .Sp
297 Returns PI.
298 .IP \fBbexp()\fR 4
299 .IX Item "bexp()"
300 .Vb 1
301 \& bexp($power, $accuracy);
302 .Ve
303 .Sp
304 Returns Euler's number \f(CW\*(C`e\*(C'\fR raised to the appropriate power, to the wanted
305 accuracy.
306 .Sp
307 Example:
308 .Sp
309 .Vb 1
310 \& # perl \-Mbigrat=bexp \-wle \*(Aqprint bexp(1,80)\*(Aq
311 .Ve
312 .IP \fBbpi()\fR 4
313 .IX Item "bpi()"
314 .Vb 1
315 \& bpi($accuracy);
316 .Ve
317 .Sp
318 Returns PI to the wanted accuracy.
319 .Sp
320 Example:
321 .Sp
322 .Vb 1
323 \& # perl \-Mbigrat=bpi \-wle \*(Aqprint bpi(80)\*(Aq
324 .Ve
325 .IP \fBaccuracy()\fR 4
326 .IX Item "accuracy()"
327 Set or get the accuracy.
328 .IP \fBprecision()\fR 4
329 .IX Item "precision()"
330 Set or get the precision.
331 .IP \fBround_mode()\fR 4
332 .IX Item "round_mode()"
333 Set or get the rounding mode.
334 .IP \fBdiv_scale()\fR 4
335 .IX Item "div_scale()"
336 Set or get the division scale.
337 .IP \fBin_effect()\fR 4
338 .IX Item "in_effect()"
339 .Vb 1
340 \& use bigrat;
341 \&
342 \& print "in effect\en" if bigrat::in_effect; # true
343 \& {
344 \& no bigrat;
345 \& print "in effect\en" if bigrat::in_effect; # false
346 \& }
347 .Ve
348 .Sp
349 Returns true or false if \f(CW\*(C`bigrat\*(C'\fR is in effect in the current scope.
350 .Sp
351 This method only works on Perl v5.9.4 or later.
352 .SH CAVEATS
353 .IX Header "CAVEATS"
354 .IP "Hexadecimal, octal, and binary floating point literals" 4
355 .IX Item "Hexadecimal, octal, and binary floating point literals"
356 Perl (and this module) accepts hexadecimal, octal, and binary floating point
357 literals, but use them with care with Perl versions before v5.32.0, because some
358 versions of Perl silently give the wrong result.
359 .IP "Operator vs literal overloading" 4
360 .IX Item "Operator vs literal overloading"
361 \&\f(CW\*(C`bigrat\*(C'\fR works by overloading handling of integer and floating point literals,
362 converting them to Math::BigRat objects.
363 .Sp
364 This means that arithmetic involving only string values or string literals are
365 performed using Perl's built-in operators.
366 .Sp
367 For example:
368 .Sp
369 .Vb 4
370 \& use bigrat;
371 \& my $x = "900000000000000009";
372 \& my $y = "900000000000000007";
373 \& print $x \- $y;
374 .Ve
375 .Sp
376 outputs \f(CW0\fR on default 32\-bit builds, since \f(CW\*(C`bigrat\*(C'\fR never sees the string
377 literals. To ensure the expression is all treated as \f(CW\*(C`Math::BigRat\*(C'\fR objects,
378 use a literal number in the expression:
379 .Sp
380 .Vb 1
381 \& print +(0+$x) \- $y;
382 .Ve
383 .IP Ranges 4
384 .IX Item "Ranges"
385 Perl does not allow overloading of ranges, so you can neither safely use ranges
386 with \f(CW\*(C`bigrat\*(C'\fR endpoints, nor is the iterator variable a \f(CW\*(C`Math::BigRat\*(C'\fR.
387 .Sp
388 .Vb 7
389 \& use 5.010;
390 \& for my $i (12..13) {
391 \& for my $j (20..21) {
392 \& say $i ** $j; # produces a floating\-point number,
393 \& # not an object
394 \& }
395 \& }
396 .Ve
397 .IP \fBin_effect()\fR 4
398 .IX Item "in_effect()"
399 This method only works on Perl v5.9.4 or later.
400 .IP \fBhex()\fR/\fBoct()\fR 4
401 .IX Item "hex()/oct()"
402 \&\f(CW\*(C`bigrat\*(C'\fR overrides these routines with versions that can also handle big
403 integer values. Under Perl prior to version v5.9.4, however, this will not
404 happen unless you specifically ask for it with the two import tags "hex" and
405 "oct" \- and then it will be global and cannot be disabled inside a scope with
406 \&\f(CW\*(C`no bigrat\*(C'\fR:
407 .Sp
408 .Vb 1
409 \& use bigrat qw/hex oct/;
410 \&
411 \& print hex("0x1234567890123456");
412 \& {
413 \& no bigrat;
414 \& print hex("0x1234567890123456");
415 \& }
416 .Ve
417 .Sp
418 The second call to \fBhex()\fR will warn about a non-portable constant.
419 .Sp
420 Compare this to:
421 .Sp
422 .Vb 1
423 \& use bigrat;
424 \&
425 \& # will warn only under Perl older than v5.9.4
426 \& print hex("0x1234567890123456");
427 .Ve
428 .SH EXAMPLES
429 .IX Header "EXAMPLES"
430 .Vb 6
431 \& perl \-Mbigrat \-le \*(Aqprint sqrt(33)\*(Aq
432 \& perl \-Mbigrat \-le \*(Aqprint 2**255\*(Aq
433 \& perl \-Mbigrat \-le \*(Aqprint 4.5+2**255\*(Aq
434 \& perl \-Mbigrat \-le \*(Aqprint 3/7 + 5/7 + 8/3\*(Aq
435 \& perl \-Mbigrat \-le \*(Aqprint 12\->is_odd()\*(Aq;
436 \& perl \-Mbigrat=l,GMP \-le \*(Aqprint 7 ** 7777\*(Aq
437 .Ve
438 .SH BUGS
439 .IX Header "BUGS"
440 Please report any bugs or feature requests to
441 \&\f(CW\*(C`bug\-bignum at rt.cpan.org\*(C'\fR, or through the web interface at
442 <https://rt.cpan.org/Ticket/Create.html?Queue=bignum> (requires login).
443 We will be notified, and then you'll automatically be notified of
444 progress on your bug as I make changes.
445 .SH SUPPORT
446 .IX Header "SUPPORT"
447 You can find documentation for this module with the perldoc command.
448 .PP
449 .Vb 1
450 \& perldoc bigrat
451 .Ve
452 .PP
453 You can also look for information at:
454 .IP \(bu 4
455 GitHub
456 .Sp
457 <https://github.com/pjacklam/p5\-bignum>
458 .IP \(bu 4
459 RT: CPAN's request tracker
460 .Sp
461 <https://rt.cpan.org/Dist/Display.html?Name=bignum>
462 .IP \(bu 4
463 MetaCPAN
464 .Sp
465 <https://metacpan.org/release/bignum>
466 .IP \(bu 4
467 CPAN Testers Matrix
468 .Sp
469 <http://matrix.cpantesters.org/?dist=bignum>
470 .SH LICENSE
471 .IX Header "LICENSE"
472 This program is free software; you may redistribute it and/or modify it under
473 the same terms as Perl itself.
474 .SH "SEE ALSO"
475 .IX Header "SEE ALSO"
476 bignum and bigint.
477 .PP
478 Math::BigInt, Math::BigFloat, Math::BigRat and Math::Big as well as
479 Math::BigInt::FastCalc, Math::BigInt::Pari and Math::BigInt::GMP.
480 .SH AUTHORS
481 .IX Header "AUTHORS"
482 .IP \(bu 4
483 (C) by Tels <http://bloodgate.com/> in early 2002 \- 2007.
484 .IP \(bu 4
485 Maintained by Peter John Acklam <[email protected]>, 2014\-.