Debian Codegraph @

upstream/mageia-cauldron/man3pm/bigfloat.3pm

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