xref: /openbsd-src/gnu/usr.bin/perl/cpan/bignum/lib/bigfloat.pm (revision 5486feefcc8cb79b19e014ab332cc5dfd05b3b33)
1256a93a4Safresh1package bigfloat;
2256a93a4Safresh1
3256a93a4Safresh1use strict;
4256a93a4Safresh1use warnings;
5256a93a4Safresh1
6256a93a4Safresh1use Carp qw< carp croak >;
7256a93a4Safresh1
8*5486feefSafresh1our $VERSION = '0.67';
9256a93a4Safresh1
10256a93a4Safresh1use Exporter;
11256a93a4Safresh1our @ISA            = qw( Exporter );
12256a93a4Safresh1our @EXPORT_OK      = qw( PI e bpi bexp hex oct );
13256a93a4Safresh1our @EXPORT         = qw( inf NaN );
14256a93a4Safresh1
15256a93a4Safresh1use overload;
16256a93a4Safresh1
17256a93a4Safresh1my $obj_class = "Math::BigFloat";
18256a93a4Safresh1
19256a93a4Safresh1##############################################################################
20256a93a4Safresh1
21256a93a4Safresh1sub accuracy {
22256a93a4Safresh1    my $self = shift;
23256a93a4Safresh1    $obj_class -> accuracy(@_);
24256a93a4Safresh1}
25256a93a4Safresh1
26256a93a4Safresh1sub precision {
27256a93a4Safresh1    my $self = shift;
28256a93a4Safresh1    $obj_class -> precision(@_);
29256a93a4Safresh1}
30256a93a4Safresh1
31256a93a4Safresh1sub round_mode {
32256a93a4Safresh1    my $self = shift;
33256a93a4Safresh1    $obj_class -> round_mode(@_);
34256a93a4Safresh1}
35256a93a4Safresh1
36256a93a4Safresh1sub div_scale {
37256a93a4Safresh1    my $self = shift;
38256a93a4Safresh1    $obj_class -> div_scale(@_);
39256a93a4Safresh1}
40256a93a4Safresh1
41256a93a4Safresh1sub upgrade {
42256a93a4Safresh1    my $self = shift;
43256a93a4Safresh1    $obj_class -> upgrade(@_);
44256a93a4Safresh1}
45256a93a4Safresh1
46256a93a4Safresh1sub downgrade {
47256a93a4Safresh1    my $self = shift;
48256a93a4Safresh1    $obj_class -> downgrade(@_);
49256a93a4Safresh1}
50256a93a4Safresh1
51256a93a4Safresh1sub in_effect {
52256a93a4Safresh1    my $level = shift || 0;
53256a93a4Safresh1    my $hinthash = (caller($level))[10];
54256a93a4Safresh1    $hinthash->{bigfloat};
55256a93a4Safresh1}
56256a93a4Safresh1
57256a93a4Safresh1sub _float_constant {
58256a93a4Safresh1    my $str = shift;
59256a93a4Safresh1
60256a93a4Safresh1    # See if we can convert the input string to a string using a normalized form
61256a93a4Safresh1    # consisting of the significand as a signed integer, the character "e", and
62256a93a4Safresh1    # the exponent as a signed integer, e.g., "+0e+0", "+314e-2", and "-1e+3".
63256a93a4Safresh1
64256a93a4Safresh1    my $nstr;
65256a93a4Safresh1
66256a93a4Safresh1    if (
67256a93a4Safresh1        # See if it is an octal number. An octal number like '0377' is also
68256a93a4Safresh1        # accepted by the functions parsing decimal and hexadecimal numbers, so
69256a93a4Safresh1        # handle octal numbers before decimal and hexadecimal numbers.
70256a93a4Safresh1
71256a93a4Safresh1        $str =~ /^0(?:[Oo]|_*[0-7])/ and
72256a93a4Safresh1        $nstr = Math::BigInt -> oct_str_to_dec_flt_str($str)
73256a93a4Safresh1
74256a93a4Safresh1          or
75256a93a4Safresh1
76256a93a4Safresh1        # See if it is decimal number.
77256a93a4Safresh1
78256a93a4Safresh1        $nstr = Math::BigInt -> dec_str_to_dec_flt_str($str)
79256a93a4Safresh1
80256a93a4Safresh1          or
81256a93a4Safresh1
82256a93a4Safresh1        # See if it is a hexadecimal number. Every hexadecimal number has a
83256a93a4Safresh1        # prefix, but the functions parsing numbers don't require it, so check
84256a93a4Safresh1        # to see if it actually is a hexadecimal number.
85256a93a4Safresh1
86256a93a4Safresh1        $str =~ /^0[Xx]/ and
87256a93a4Safresh1        $nstr = Math::BigInt -> hex_str_to_dec_flt_str($str)
88256a93a4Safresh1
89256a93a4Safresh1          or
90256a93a4Safresh1
91256a93a4Safresh1        # See if it is a binary numbers. Every binary number has a prefix, but
92256a93a4Safresh1        # the functions parsing numbers don't require it, so check to see if it
93256a93a4Safresh1        # actually is a binary number.
94256a93a4Safresh1
95256a93a4Safresh1        $str =~ /^0[Bb]/ and
96256a93a4Safresh1        $nstr = Math::BigInt -> bin_str_to_dec_flt_str($str))
97256a93a4Safresh1    {
98256a93a4Safresh1        return $obj_class -> new($nstr);
99256a93a4Safresh1    }
100256a93a4Safresh1
101256a93a4Safresh1    # If we get here, there is a bug in the code above this point.
102256a93a4Safresh1
103256a93a4Safresh1    warn "Internal error: unable to handle literal constant '$str'.",
104256a93a4Safresh1      " This is a bug, so please report this to the module author.";
105256a93a4Safresh1    return $obj_class -> bnan();
106256a93a4Safresh1}
107256a93a4Safresh1
108256a93a4Safresh1#############################################################################
109256a93a4Safresh1# the following two routines are for "use bigfloat qw/hex oct/;":
110256a93a4Safresh1
111256a93a4Safresh1use constant LEXICAL => $] > 5.009004;
112256a93a4Safresh1
113256a93a4Safresh1# Internal function with the same semantics as CORE::hex(). This function is
114256a93a4Safresh1# not used directly, but rather by other front-end functions.
115256a93a4Safresh1
116256a93a4Safresh1sub _hex_core {
117256a93a4Safresh1    my $str = shift;
118256a93a4Safresh1
119256a93a4Safresh1    # Strip off, clean, and parse as much as we can from the beginning.
120256a93a4Safresh1
121256a93a4Safresh1    my $x;
122256a93a4Safresh1    if ($str =~ s/ ^ ( 0? [xX] )? ( [0-9a-fA-F]* ( _ [0-9a-fA-F]+ )* ) //x) {
123256a93a4Safresh1        my $chrs = $2;
124256a93a4Safresh1        $chrs =~ tr/_//d;
125256a93a4Safresh1        $chrs = '0' unless CORE::length $chrs;
126256a93a4Safresh1        $x = $obj_class -> from_hex($chrs);
127256a93a4Safresh1    } else {
128256a93a4Safresh1        $x = $obj_class -> bzero();
129256a93a4Safresh1    }
130256a93a4Safresh1
131256a93a4Safresh1    # Warn about trailing garbage.
132256a93a4Safresh1
133256a93a4Safresh1    if (CORE::length($str)) {
134256a93a4Safresh1        require Carp;
135256a93a4Safresh1        Carp::carp(sprintf("Illegal hexadecimal digit '%s' ignored",
136256a93a4Safresh1                           substr($str, 0, 1)));
137256a93a4Safresh1    }
138256a93a4Safresh1
139256a93a4Safresh1    return $x;
140256a93a4Safresh1}
141256a93a4Safresh1
142256a93a4Safresh1# Internal function with the same semantics as CORE::oct(). This function is
143256a93a4Safresh1# not used directly, but rather by other front-end functions.
144256a93a4Safresh1
145256a93a4Safresh1sub _oct_core {
146256a93a4Safresh1    my $str = shift;
147256a93a4Safresh1
148256a93a4Safresh1    $str =~ s/^\s*//;
149256a93a4Safresh1
150256a93a4Safresh1    # Hexadecimal input.
151256a93a4Safresh1
152256a93a4Safresh1    return _hex_core($str) if $str =~ /^0?[xX]/;
153256a93a4Safresh1
154256a93a4Safresh1    my $x;
155256a93a4Safresh1
156256a93a4Safresh1    # Binary input.
157256a93a4Safresh1
158256a93a4Safresh1    if ($str =~ /^0?[bB]/) {
159256a93a4Safresh1
160256a93a4Safresh1        # Strip off, clean, and parse as much as we can from the beginning.
161256a93a4Safresh1
162256a93a4Safresh1        if ($str =~ s/ ^ ( 0? [bB] )? ( [01]* ( _ [01]+ )* ) //x) {
163256a93a4Safresh1            my $chrs = $2;
164256a93a4Safresh1            $chrs =~ tr/_//d;
165256a93a4Safresh1            $chrs = '0' unless CORE::length $chrs;
166256a93a4Safresh1            $x = $obj_class -> from_bin($chrs);
167256a93a4Safresh1        }
168256a93a4Safresh1
169256a93a4Safresh1        # Warn about trailing garbage.
170256a93a4Safresh1
171256a93a4Safresh1        if (CORE::length($str)) {
172256a93a4Safresh1            require Carp;
173256a93a4Safresh1            Carp::carp(sprintf("Illegal binary digit '%s' ignored",
174256a93a4Safresh1                               substr($str, 0, 1)));
175256a93a4Safresh1        }
176256a93a4Safresh1
177256a93a4Safresh1        return $x;
178256a93a4Safresh1    }
179256a93a4Safresh1
180256a93a4Safresh1    # Octal input. Strip off, clean, and parse as much as we can from the
181256a93a4Safresh1    # beginning.
182256a93a4Safresh1
183256a93a4Safresh1    if ($str =~ s/ ^ ( 0? [oO] )? ( [0-7]* ( _ [0-7]+ )* ) //x) {
184256a93a4Safresh1        my $chrs = $2;
185256a93a4Safresh1        $chrs =~ tr/_//d;
186256a93a4Safresh1        $chrs = '0' unless CORE::length $chrs;
187256a93a4Safresh1        $x = $obj_class -> from_oct($chrs);
188256a93a4Safresh1    }
189256a93a4Safresh1
190256a93a4Safresh1    # Warn about trailing garbage. CORE::oct() only warns about 8 and 9, but it
191256a93a4Safresh1    # is more helpful to warn about all invalid digits.
192256a93a4Safresh1
193256a93a4Safresh1    if (CORE::length($str)) {
194256a93a4Safresh1        require Carp;
195256a93a4Safresh1        Carp::carp(sprintf("Illegal octal digit '%s' ignored",
196256a93a4Safresh1                           substr($str, 0, 1)));
197256a93a4Safresh1    }
198256a93a4Safresh1
199256a93a4Safresh1    return $x;
200256a93a4Safresh1}
201256a93a4Safresh1
202256a93a4Safresh1{
203256a93a4Safresh1    my $proto = LEXICAL ? '_' : ';$';
204256a93a4Safresh1    eval '
205256a93a4Safresh1sub hex(' . $proto . ') {' . <<'.';
206256a93a4Safresh1    my $str = @_ ? $_[0] : $_;
207256a93a4Safresh1    _hex_core($str);
208256a93a4Safresh1}
209256a93a4Safresh1.
210256a93a4Safresh1
211256a93a4Safresh1    eval '
212256a93a4Safresh1sub oct(' . $proto . ') {' . <<'.';
213256a93a4Safresh1    my $str = @_ ? $_[0] : $_;
214256a93a4Safresh1    _oct_core($str);
215256a93a4Safresh1}
216256a93a4Safresh1.
217256a93a4Safresh1}
218256a93a4Safresh1
219256a93a4Safresh1#############################################################################
220256a93a4Safresh1# the following two routines are for Perl 5.9.4 or later and are lexical
221256a93a4Safresh1
222256a93a4Safresh1my ($prev_oct, $prev_hex, $overridden);
223256a93a4Safresh1
224256a93a4Safresh1if (LEXICAL) { eval <<'.' }
225256a93a4Safresh1sub _hex(_) {
226256a93a4Safresh1    my $hh = (caller 0)[10];
227256a93a4Safresh1    return $$hh{bigfloat} ? bigfloat::_hex_core($_[0])
228256a93a4Safresh1         : $$hh{bigrat} ? bigrat::_hex_core($_[0])
229256a93a4Safresh1         : $$hh{bigint} ? bigint::_hex_core($_[0])
230256a93a4Safresh1         : $prev_hex    ? &$prev_hex($_[0])
231256a93a4Safresh1         : CORE::hex($_[0]);
232256a93a4Safresh1}
233256a93a4Safresh1
234256a93a4Safresh1sub _oct(_) {
235256a93a4Safresh1    my $hh = (caller 0)[10];
236256a93a4Safresh1    return $$hh{bigfloat} ? bigfloat::_oct_core($_[0])
237256a93a4Safresh1         : $$hh{bigrat} ? bigrat::_oct_core($_[0])
238256a93a4Safresh1         : $$hh{bigint} ? bigint::_oct_core($_[0])
239256a93a4Safresh1         : $prev_oct    ? &$prev_oct($_[0])
240256a93a4Safresh1         : CORE::oct($_[0]);
241256a93a4Safresh1}
242256a93a4Safresh1.
243256a93a4Safresh1
244256a93a4Safresh1sub _override {
245256a93a4Safresh1    return if $overridden;
246256a93a4Safresh1    $prev_oct = *CORE::GLOBAL::oct{CODE};
247256a93a4Safresh1    $prev_hex = *CORE::GLOBAL::hex{CODE};
248256a93a4Safresh1    no warnings 'redefine';
249256a93a4Safresh1    *CORE::GLOBAL::oct = \&_oct;
250256a93a4Safresh1    *CORE::GLOBAL::hex = \&_hex;
251256a93a4Safresh1    $overridden = 1;
252256a93a4Safresh1}
253256a93a4Safresh1
254256a93a4Safresh1sub unimport {
255*5486feefSafresh1    delete $^H{bigfloat};       # no longer in effect
256256a93a4Safresh1    overload::remove_constant('binary', '', 'float', '', 'integer');
257256a93a4Safresh1}
258256a93a4Safresh1
259256a93a4Safresh1sub import {
260256a93a4Safresh1    my $class = shift;
261256a93a4Safresh1
262256a93a4Safresh1    $^H{bigfloat} = 1;          # we are in effect
263*5486feefSafresh1    delete $^H{bigint};
264*5486feefSafresh1    delete $^H{bigrat};
265256a93a4Safresh1
266256a93a4Safresh1    # for newer Perls always override hex() and oct() with a lexical version:
267256a93a4Safresh1    if (LEXICAL) {
268256a93a4Safresh1        _override();
269256a93a4Safresh1    }
270256a93a4Safresh1
271256a93a4Safresh1    my @import = ();
272256a93a4Safresh1    my @a = ();                         # unrecognized arguments
273256a93a4Safresh1    my $ver;                            # version?
274256a93a4Safresh1
275256a93a4Safresh1    while (@_) {
276256a93a4Safresh1        my $param = shift;
277256a93a4Safresh1
278256a93a4Safresh1        # Accuracy.
279256a93a4Safresh1
280256a93a4Safresh1        if ($param =~ /^a(ccuracy)?$/) {
281256a93a4Safresh1            push @import, 'accuracy', shift();
282256a93a4Safresh1            next;
283256a93a4Safresh1        }
284256a93a4Safresh1
285256a93a4Safresh1        # Precision.
286256a93a4Safresh1
287256a93a4Safresh1        if ($param =~ /^p(recision)?$/) {
288256a93a4Safresh1            push @import, 'precision', shift();
289256a93a4Safresh1            next;
290256a93a4Safresh1        }
291256a93a4Safresh1
292256a93a4Safresh1        # Rounding mode.
293256a93a4Safresh1
294256a93a4Safresh1        if ($param eq 'round_mode') {
295256a93a4Safresh1            push @import, 'round_mode', shift();
296256a93a4Safresh1            next;
297256a93a4Safresh1        }
298256a93a4Safresh1
299256a93a4Safresh1        # Backend library.
300256a93a4Safresh1
301256a93a4Safresh1        if ($param =~ /^(l|lib|try|only)$/) {
302256a93a4Safresh1            push @import, $param eq 'l' ? 'lib' : $param;
303256a93a4Safresh1            push @import, shift() if @_;
304256a93a4Safresh1            next;
305256a93a4Safresh1        }
306256a93a4Safresh1
307256a93a4Safresh1        if ($param =~ /^(v|version)$/) {
308256a93a4Safresh1            $ver = 1;
309256a93a4Safresh1            next;
310256a93a4Safresh1        }
311256a93a4Safresh1
312256a93a4Safresh1        if ($param =~ /^(t|trace)$/) {
313256a93a4Safresh1            $obj_class .= "::Trace";
314256a93a4Safresh1            eval "require $obj_class";
315256a93a4Safresh1            die $@ if $@;
316256a93a4Safresh1            next;
317256a93a4Safresh1        }
318256a93a4Safresh1
319256a93a4Safresh1        if ($param =~ /^(PI|e|bexp|bpi|hex|oct)\z/) {
320256a93a4Safresh1            push @a, $param;
321256a93a4Safresh1            next;
322256a93a4Safresh1        }
323256a93a4Safresh1
324256a93a4Safresh1        croak("Unknown option '$param'");
325256a93a4Safresh1    }
326256a93a4Safresh1
327256a93a4Safresh1    eval "require $obj_class";
328256a93a4Safresh1    die $@ if $@;
329256a93a4Safresh1    $obj_class -> import(@import);
330256a93a4Safresh1
331256a93a4Safresh1    if ($ver) {
332256a93a4Safresh1        printf "%-31s v%s\n", $class, $class -> VERSION();
333256a93a4Safresh1        printf " lib => %-23s v%s\n",
334256a93a4Safresh1          $obj_class -> config("lib"), $obj_class -> config("lib_version");
335256a93a4Safresh1        printf "%-31s v%s\n", $obj_class, $obj_class -> VERSION();
336256a93a4Safresh1        exit;
337256a93a4Safresh1    }
338256a93a4Safresh1
339256a93a4Safresh1    $class -> export_to_level(1, $class, @a);   # export inf, NaN, etc.
340256a93a4Safresh1
341256a93a4Safresh1    overload::constant
342256a93a4Safresh1
343256a93a4Safresh1        # This takes care each number written as decimal integer and within the
344256a93a4Safresh1        # range of what perl can represent as an integer, e.g., "314", but not
345256a93a4Safresh1        # "3141592653589793238462643383279502884197169399375105820974944592307".
346256a93a4Safresh1
347256a93a4Safresh1        integer => sub {
348256a93a4Safresh1            #printf "Value '%s' handled by the 'integer' sub.\n", $_[0];
349256a93a4Safresh1            my $str = shift;
350256a93a4Safresh1            return $obj_class -> new($str);
351256a93a4Safresh1        },
352256a93a4Safresh1
353256a93a4Safresh1        # This takes care of each number written with a decimal point and/or
354256a93a4Safresh1        # using floating point notation, e.g., "3.", "3.0", "3.14e+2" (decimal),
355256a93a4Safresh1        # "0b1.101p+2" (binary), "03.14p+2" and "0o3.14p+2" (octal), and
356256a93a4Safresh1        # "0x3.14p+2" (hexadecimal).
357256a93a4Safresh1
358256a93a4Safresh1        float => sub {
359256a93a4Safresh1            #printf "# Value '%s' handled by the 'float' sub.\n", $_[0];
360256a93a4Safresh1            _float_constant(shift);
361256a93a4Safresh1        },
362256a93a4Safresh1
363256a93a4Safresh1        # Take care of each number written as an integer (no decimal point or
364256a93a4Safresh1        # exponent) using binary, octal, or hexadecimal notation, e.g., "0b101"
365256a93a4Safresh1        # (binary), "0314" and "0o314" (octal), and "0x314" (hexadecimal).
366256a93a4Safresh1
367256a93a4Safresh1        binary => sub {
368256a93a4Safresh1            #printf "# Value '%s' handled by the 'binary' sub.\n", $_[0];
369256a93a4Safresh1            my $str = shift;
370256a93a4Safresh1            return $obj_class -> new($str) if $str =~ /^0[XxBb]/;
371256a93a4Safresh1            $obj_class -> from_oct($str);
372256a93a4Safresh1        };
373256a93a4Safresh1}
374256a93a4Safresh1
375256a93a4Safresh1sub inf () { $obj_class -> binf(); }
376256a93a4Safresh1sub NaN () { $obj_class -> bnan(); }
377256a93a4Safresh1
378256a93a4Safresh1# This should depend on the current accuracy/precision. Fixme!
379256a93a4Safresh1sub PI  () { $obj_class -> new('3.141592653589793238462643383279502884197'); }
380256a93a4Safresh1sub e   () { $obj_class -> new('2.718281828459045235360287471352662497757'); }
381256a93a4Safresh1
382256a93a4Safresh1sub bpi ($) {
383256a93a4Safresh1    my $up = Math::BigFloat -> upgrade();   # get current upgrading, if any ...
384256a93a4Safresh1    Math::BigFloat -> upgrade(undef);       # ... and disable
385256a93a4Safresh1
386256a93a4Safresh1    my $x = Math::BigFloat -> bpi(@_);
387256a93a4Safresh1
388256a93a4Safresh1    Math::BigFloat -> upgrade($up);         # reset the upgrading
389256a93a4Safresh1
390256a93a4Safresh1    return $x;
391256a93a4Safresh1}
392256a93a4Safresh1
393256a93a4Safresh1sub bexp ($$) {
394256a93a4Safresh1    my $up = Math::BigFloat -> upgrade();   # get current upgrading, if any ...
395256a93a4Safresh1    Math::BigFloat -> upgrade(undef);       # ... and disable
396256a93a4Safresh1
397256a93a4Safresh1    my $x = Math::BigFloat -> new(shift);
398256a93a4Safresh1    $x -> bexp(@_);
399256a93a4Safresh1
400256a93a4Safresh1    Math::BigFloat -> upgrade($up);         # reset the upgrading
401256a93a4Safresh1
402256a93a4Safresh1    return $x;
403256a93a4Safresh1}
404256a93a4Safresh1
405256a93a4Safresh11;
406256a93a4Safresh1
407256a93a4Safresh1__END__
408256a93a4Safresh1
409256a93a4Safresh1=pod
410256a93a4Safresh1
411256a93a4Safresh1=head1 NAME
412256a93a4Safresh1
413256a93a4Safresh1bigfloat - transparent big floating point number support for Perl
414256a93a4Safresh1
415256a93a4Safresh1=head1 SYNOPSIS
416256a93a4Safresh1
417256a93a4Safresh1    use bigfloat;
418256a93a4Safresh1
419256a93a4Safresh1    $x = 2 + 4.5;                       # Math::BigFloat 6.5
420256a93a4Safresh1    print 2 ** 512 * 0.1;               # Math::BigFloat 134...09.6
421256a93a4Safresh1    print inf + 42;                     # Math::BigFloat inf
422256a93a4Safresh1    print NaN * 7;                      # Math::BigFloat NaN
423256a93a4Safresh1    print hex("0x1234567890123490");    # Perl v5.10.0 or later
424256a93a4Safresh1
425256a93a4Safresh1    {
426256a93a4Safresh1        no bigfloat;
427256a93a4Safresh1        print 2 ** 256;                 # a normal Perl scalar now
428256a93a4Safresh1    }
429256a93a4Safresh1
430256a93a4Safresh1    # for older Perls, import into current package:
431256a93a4Safresh1    use bigfloat qw/hex oct/;
432256a93a4Safresh1    print hex("0x1234567890123490");
433256a93a4Safresh1    print oct("01234567890123490");
434256a93a4Safresh1
435256a93a4Safresh1=head1 DESCRIPTION
436256a93a4Safresh1
437256a93a4Safresh1All numeric literals in the given scope are converted to Math::BigFloat objects.
438256a93a4Safresh1
439256a93a4Safresh1All operators (including basic math operations) except the range operator C<..>
440256a93a4Safresh1are overloaded.
441256a93a4Safresh1
442256a93a4Safresh1So, the following:
443256a93a4Safresh1
444256a93a4Safresh1    use bigfloat;
445256a93a4Safresh1    $x = 1234;
446256a93a4Safresh1
447256a93a4Safresh1creates a Math::BigFloat and stores a reference to in $x. This happens
448256a93a4Safresh1transparently and behind your back, so to speak.
449256a93a4Safresh1
450256a93a4Safresh1You can see this with the following:
451256a93a4Safresh1
452256a93a4Safresh1    perl -Mbigfloat -le 'print ref(1234)'
453256a93a4Safresh1
454256a93a4Safresh1Since numbers are actually objects, you can call all the usual methods from
455256a93a4Safresh1Math::BigFloat on them. This even works to some extent on expressions:
456256a93a4Safresh1
457256a93a4Safresh1    perl -Mbigfloat -le '$x = 1234; print $x->bdec()'
458256a93a4Safresh1    perl -Mbigfloat -le 'print 1234->copy()->binc();'
459256a93a4Safresh1    perl -Mbigfloat -le 'print 1234->copy()->binc->badd(6);'
460256a93a4Safresh1    perl -Mbigfloat -le 'print +(1234)->copy()->binc()'
461256a93a4Safresh1
462256a93a4Safresh1(Note that print doesn't do what you expect if the expression starts with
463256a93a4Safresh1'(' hence the C<+>)
464256a93a4Safresh1
465256a93a4Safresh1You can even chain the operations together as usual:
466256a93a4Safresh1
467256a93a4Safresh1    perl -Mbigfloat -le 'print 1234->copy()->binc->badd(6);'
468256a93a4Safresh1    1241
469256a93a4Safresh1
470256a93a4Safresh1Please note the following does not work as expected (prints nothing), since
471256a93a4Safresh1overloading of '..' is not yet possible in Perl (as of v5.8.0):
472256a93a4Safresh1
473256a93a4Safresh1    perl -Mbigfloat -le 'for (1..2) { print ref($_); }'
474256a93a4Safresh1
475256a93a4Safresh1=head2 Options
476256a93a4Safresh1
477256a93a4Safresh1C<bigfloat> recognizes some options that can be passed while loading it via via
478256a93a4Safresh1C<use>. The following options exist:
479256a93a4Safresh1
480256a93a4Safresh1=over 4
481256a93a4Safresh1
482256a93a4Safresh1=item a or accuracy
483256a93a4Safresh1
484256a93a4Safresh1This sets the accuracy for all math operations. The argument must be greater
485256a93a4Safresh1than or equal to zero. See Math::BigInt's bround() method for details.
486256a93a4Safresh1
487256a93a4Safresh1    perl -Mbigfloat=a,50 -le 'print sqrt(20)'
488256a93a4Safresh1
489256a93a4Safresh1Note that setting precision and accuracy at the same time is not possible.
490256a93a4Safresh1
491256a93a4Safresh1=item p or precision
492256a93a4Safresh1
493256a93a4Safresh1This sets the precision for all math operations. The argument can be any
494256a93a4Safresh1integer. Negative values mean a fixed number of digits after the dot, while a
495256a93a4Safresh1positive value rounds to this digit left from the dot. 0 means round to integer.
496256a93a4Safresh1See Math::BigInt's bfround() method for details.
497256a93a4Safresh1
498256a93a4Safresh1    perl -Mbigfloat=p,-50 -le 'print sqrt(20)'
499256a93a4Safresh1
500256a93a4Safresh1Note that setting precision and accuracy at the same time is not possible.
501256a93a4Safresh1
502256a93a4Safresh1=item t or trace
503256a93a4Safresh1
504256a93a4Safresh1This enables a trace mode and is primarily for debugging.
505256a93a4Safresh1
506256a93a4Safresh1=item l, lib, try, or only
507256a93a4Safresh1
508256a93a4Safresh1Load a different math lib, see L<Math Library>.
509256a93a4Safresh1
510256a93a4Safresh1    perl -Mbigfloat=l,GMP -e 'print 2 ** 512'
511256a93a4Safresh1    perl -Mbigfloat=lib,GMP -e 'print 2 ** 512'
512256a93a4Safresh1    perl -Mbigfloat=try,GMP -e 'print 2 ** 512'
513256a93a4Safresh1    perl -Mbigfloat=only,GMP -e 'print 2 ** 512'
514256a93a4Safresh1
515256a93a4Safresh1=item hex
516256a93a4Safresh1
517256a93a4Safresh1Override the built-in hex() method with a version that can handle big numbers.
518256a93a4Safresh1This overrides it by exporting it to the current package. Under Perl v5.10.0 and
519256a93a4Safresh1higher, this is not so necessary, as hex() is lexically overridden in the
520256a93a4Safresh1current scope whenever the C<bigfloat> pragma is active.
521256a93a4Safresh1
522256a93a4Safresh1=item oct
523256a93a4Safresh1
524256a93a4Safresh1Override the built-in oct() method with a version that can handle big numbers.
525256a93a4Safresh1This overrides it by exporting it to the current package. Under Perl v5.10.0 and
526256a93a4Safresh1higher, this is not so necessary, as oct() is lexically overridden in the
527256a93a4Safresh1current scope whenever the C<bigfloat> pragma is active.
528256a93a4Safresh1
529256a93a4Safresh1=item v or version
530256a93a4Safresh1
531256a93a4Safresh1this prints out the name and version of the modules and then exits.
532256a93a4Safresh1
533256a93a4Safresh1    perl -Mbigfloat=v
534256a93a4Safresh1
535256a93a4Safresh1=back
536256a93a4Safresh1
537256a93a4Safresh1=head2 Math Library
538256a93a4Safresh1
539256a93a4Safresh1Math with the numbers is done (by default) by a backend library module called
540256a93a4Safresh1Math::BigInt::Calc. The default is equivalent to saying:
541256a93a4Safresh1
542256a93a4Safresh1    use bigfloat lib => 'Calc';
543256a93a4Safresh1
544256a93a4Safresh1you can change this by using:
545256a93a4Safresh1
546256a93a4Safresh1    use bigfloat lib => 'GMP';
547256a93a4Safresh1
548256a93a4Safresh1The following would first try to find Math::BigInt::Foo, then Math::BigInt::Bar,
549256a93a4Safresh1and if this also fails, revert to Math::BigInt::Calc:
550256a93a4Safresh1
551256a93a4Safresh1    use bigfloat lib => 'Foo,Math::BigInt::Bar';
552256a93a4Safresh1
553256a93a4Safresh1Using c<lib> warns if none of the specified libraries can be found and
554256a93a4Safresh1L<Math::BigInt> fell back to one of the default libraries. To suppress this
555256a93a4Safresh1warning, use c<try> instead:
556256a93a4Safresh1
557256a93a4Safresh1    use bigfloat try => 'GMP';
558256a93a4Safresh1
559256a93a4Safresh1If you want the code to die instead of falling back, use C<only> instead:
560256a93a4Safresh1
561256a93a4Safresh1    use bigfloat only => 'GMP';
562256a93a4Safresh1
563256a93a4Safresh1Please see respective module documentation for further details.
564256a93a4Safresh1
565256a93a4Safresh1=head2 Method calls
566256a93a4Safresh1
567256a93a4Safresh1Since all numbers are now objects, you can use all methods that are part of the
568256a93a4Safresh1Math::BigFloat API.
569256a93a4Safresh1
570256a93a4Safresh1But a warning is in order. When using the following to make a copy of a number,
571256a93a4Safresh1only a shallow copy will be made.
572256a93a4Safresh1
573256a93a4Safresh1    $x = 9; $y = $x;
574256a93a4Safresh1    $x = $y = 7;
575256a93a4Safresh1
576256a93a4Safresh1Using the copy or the original with overloaded math is okay, e.g., the following
577256a93a4Safresh1work:
578256a93a4Safresh1
579256a93a4Safresh1    $x = 9; $y = $x;
580256a93a4Safresh1    print $x + 1, " ", $y,"\n";     # prints 10 9
581256a93a4Safresh1
582256a93a4Safresh1but calling any method that modifies the number directly will result in B<both>
583256a93a4Safresh1the original and the copy being destroyed:
584256a93a4Safresh1
585256a93a4Safresh1    $x = 9; $y = $x;
586256a93a4Safresh1    print $x->badd(1), " ", $y,"\n";        # prints 10 10
587256a93a4Safresh1
588256a93a4Safresh1    $x = 9; $y = $x;
589256a93a4Safresh1    print $x->binc(1), " ", $y,"\n";        # prints 10 10
590256a93a4Safresh1
591256a93a4Safresh1    $x = 9; $y = $x;
592256a93a4Safresh1    print $x->bmul(2), " ", $y,"\n";        # prints 18 18
593256a93a4Safresh1
594256a93a4Safresh1Using methods that do not modify, but test that the contents works:
595256a93a4Safresh1
596256a93a4Safresh1    $x = 9; $y = $x;
597256a93a4Safresh1    $z = 9 if $x->is_zero();                # works fine
598256a93a4Safresh1
599256a93a4Safresh1See the documentation about the copy constructor and C<=> in overload, as well
600256a93a4Safresh1as the documentation in Math::BigFloat for further details.
601256a93a4Safresh1
602256a93a4Safresh1=head2 Methods
603256a93a4Safresh1
604256a93a4Safresh1=over 4
605256a93a4Safresh1
606256a93a4Safresh1=item inf()
607256a93a4Safresh1
608256a93a4Safresh1A shortcut to return Math::BigFloat->binf(). Useful because Perl does not always
609256a93a4Safresh1handle bareword C<inf> properly.
610256a93a4Safresh1
611256a93a4Safresh1=item NaN()
612256a93a4Safresh1
613256a93a4Safresh1A shortcut to return Math::BigFloat->bnan(). Useful because Perl does not always
614256a93a4Safresh1handle bareword C<NaN> properly.
615256a93a4Safresh1
616256a93a4Safresh1=item e
617256a93a4Safresh1
618256a93a4Safresh1    # perl -Mbigfloat=e -wle 'print e'
619256a93a4Safresh1
620256a93a4Safresh1Returns Euler's number C<e>, aka exp(1)
621256a93a4Safresh1
622256a93a4Safresh1=item PI
623256a93a4Safresh1
624256a93a4Safresh1    # perl -Mbigfloat=PI -wle 'print PI'
625256a93a4Safresh1
626256a93a4Safresh1Returns PI.
627256a93a4Safresh1
628256a93a4Safresh1=item bexp()
629256a93a4Safresh1
630256a93a4Safresh1    bexp($power, $accuracy);
631256a93a4Safresh1
632256a93a4Safresh1Returns Euler's number C<e> raised to the appropriate power, to the wanted
633256a93a4Safresh1accuracy.
634256a93a4Safresh1
635256a93a4Safresh1Example:
636256a93a4Safresh1
637256a93a4Safresh1    # perl -Mbigfloat=bexp -wle 'print bexp(1,80)'
638256a93a4Safresh1
639256a93a4Safresh1=item bpi()
640256a93a4Safresh1
641256a93a4Safresh1    bpi($accuracy);
642256a93a4Safresh1
643256a93a4Safresh1Returns PI to the wanted accuracy.
644256a93a4Safresh1
645256a93a4Safresh1Example:
646256a93a4Safresh1
647256a93a4Safresh1    # perl -Mbigfloat=bpi -wle 'print bpi(80)'
648256a93a4Safresh1
649256a93a4Safresh1=item accuracy()
650256a93a4Safresh1
651256a93a4Safresh1Set or get the accuracy.
652256a93a4Safresh1
653256a93a4Safresh1=item precision()
654256a93a4Safresh1
655256a93a4Safresh1Set or get the precision.
656256a93a4Safresh1
657256a93a4Safresh1=item round_mode()
658256a93a4Safresh1
659256a93a4Safresh1Set or get the rounding mode.
660256a93a4Safresh1
661256a93a4Safresh1=item div_scale()
662256a93a4Safresh1
663256a93a4Safresh1Set or get the division scale.
664256a93a4Safresh1
665256a93a4Safresh1=item upgrade()
666256a93a4Safresh1
667256a93a4Safresh1Set or get the class that the downgrade class upgrades to, if any. Set the
668256a93a4Safresh1upgrade class to C<undef> to disable upgrading.
669256a93a4Safresh1
670256a93a4Safresh1Upgrading is disabled by default.
671256a93a4Safresh1
672256a93a4Safresh1=item downgrade()
673256a93a4Safresh1
674256a93a4Safresh1Set or get the class that the upgrade class downgrades to, if any. Set the
675256a93a4Safresh1downgrade class to C<undef> to disable upgrading.
676256a93a4Safresh1
677256a93a4Safresh1Downgrading is disabled by default.
678256a93a4Safresh1
679256a93a4Safresh1=item in_effect()
680256a93a4Safresh1
681256a93a4Safresh1    use bigfloat;
682256a93a4Safresh1
683256a93a4Safresh1    print "in effect\n" if bigfloat::in_effect;       # true
684256a93a4Safresh1    {
685256a93a4Safresh1        no bigfloat;
686256a93a4Safresh1        print "in effect\n" if bigfloat::in_effect;   # false
687256a93a4Safresh1    }
688256a93a4Safresh1
689256a93a4Safresh1Returns true or false if C<bigfloat> is in effect in the current scope.
690256a93a4Safresh1
691256a93a4Safresh1This method only works on Perl v5.9.4 or later.
692256a93a4Safresh1
693256a93a4Safresh1=back
694256a93a4Safresh1
695256a93a4Safresh1=head1 CAVEATS
696256a93a4Safresh1
697256a93a4Safresh1=over 4
698256a93a4Safresh1
699256a93a4Safresh1=item Hexadecimal, octal, and binary floating point literals
700256a93a4Safresh1
701256a93a4Safresh1Perl (and this module) accepts hexadecimal, octal, and binary floating point
702256a93a4Safresh1literals, but use them with care with Perl versions before v5.32.0, because some
703256a93a4Safresh1versions of Perl silently give the wrong result.
704256a93a4Safresh1
705256a93a4Safresh1=item Operator vs literal overloading
706256a93a4Safresh1
707256a93a4Safresh1C<bigrat> works by overloading handling of integer and floating point literals,
708256a93a4Safresh1converting them to L<Math::BigRat> objects.
709256a93a4Safresh1
710256a93a4Safresh1This means that arithmetic involving only string values or string literals are
711256a93a4Safresh1performed using Perl's built-in operators.
712256a93a4Safresh1
713256a93a4Safresh1For example:
714256a93a4Safresh1
715256a93a4Safresh1    use bigrat;
716256a93a4Safresh1    my $x = "900000000000000009";
717256a93a4Safresh1    my $y = "900000000000000007";
718256a93a4Safresh1    print $x - $y;
719256a93a4Safresh1
720256a93a4Safresh1outputs C<0> on default 32-bit builds, since C<bigfloat> never sees the string
721256a93a4Safresh1literals. To ensure the expression is all treated as C<Math::BigFloat> objects,
722256a93a4Safresh1use a literal number in the expression:
723256a93a4Safresh1
724256a93a4Safresh1    print +(0+$x) - $y;
725256a93a4Safresh1
726256a93a4Safresh1=item Ranges
727256a93a4Safresh1
728256a93a4Safresh1Perl does not allow overloading of ranges, so you can neither safely use ranges
729256a93a4Safresh1with C<bigfloat> endpoints, nor is the iterator variable a C<Math::BigFloat>.
730256a93a4Safresh1
731256a93a4Safresh1    use 5.010;
732256a93a4Safresh1    for my $i (12..13) {
733256a93a4Safresh1      for my $j (20..21) {
734256a93a4Safresh1        say $i ** $j;  # produces a floating-point number,
735256a93a4Safresh1                       # not an object
736256a93a4Safresh1      }
737256a93a4Safresh1    }
738256a93a4Safresh1
739256a93a4Safresh1=item in_effect()
740256a93a4Safresh1
741256a93a4Safresh1This method only works on Perl v5.9.4 or later.
742256a93a4Safresh1
743256a93a4Safresh1=item hex()/oct()
744256a93a4Safresh1
745256a93a4Safresh1C<bigfloat> overrides these routines with versions that can also handle big
746256a93a4Safresh1integer values. Under Perl prior to version v5.9.4, however, this will not
747256a93a4Safresh1happen unless you specifically ask for it with the two import tags "hex" and
748256a93a4Safresh1"oct" - and then it will be global and cannot be disabled inside a scope with
749256a93a4Safresh1C<no bigfloat>:
750256a93a4Safresh1
751256a93a4Safresh1    use bigfloat qw/hex oct/;
752256a93a4Safresh1
753256a93a4Safresh1    print hex("0x1234567890123456");
754256a93a4Safresh1    {
755256a93a4Safresh1        no bigfloat;
756256a93a4Safresh1        print hex("0x1234567890123456");
757256a93a4Safresh1    }
758256a93a4Safresh1
759256a93a4Safresh1The second call to hex() will warn about a non-portable constant.
760256a93a4Safresh1
761256a93a4Safresh1Compare this to:
762256a93a4Safresh1
763256a93a4Safresh1    use bigfloat;
764256a93a4Safresh1
765256a93a4Safresh1    # will warn only under Perl older than v5.9.4
766256a93a4Safresh1    print hex("0x1234567890123456");
767256a93a4Safresh1
768256a93a4Safresh1=back
769256a93a4Safresh1
770256a93a4Safresh1=head1 EXAMPLES
771256a93a4Safresh1
772256a93a4Safresh1Some cool command line examples to impress the Python crowd ;)
773256a93a4Safresh1
774256a93a4Safresh1    perl -Mbigfloat -le 'print sqrt(33)'
775256a93a4Safresh1    perl -Mbigfloat -le 'print 2**255'
776256a93a4Safresh1    perl -Mbigfloat -le 'print 4.5+2**255'
777256a93a4Safresh1    perl -Mbigfloat -le 'print 3/7 + 5/7 + 8/3'
778256a93a4Safresh1    perl -Mbigfloat -le 'print 123->is_odd()'
779256a93a4Safresh1    perl -Mbigfloat -le 'print log(2)'
780256a93a4Safresh1    perl -Mbigfloat -le 'print exp(1)'
781256a93a4Safresh1    perl -Mbigfloat -le 'print 2 ** 0.5'
782256a93a4Safresh1    perl -Mbigfloat=a,65 -le 'print 2 ** 0.2'
783256a93a4Safresh1    perl -Mbigfloat=l,GMP -le 'print 7 ** 7777'
784256a93a4Safresh1
785256a93a4Safresh1=head1 BUGS
786256a93a4Safresh1
787256a93a4Safresh1Please report any bugs or feature requests to
788256a93a4Safresh1C<bug-bignum at rt.cpan.org>, or through the web interface at
789256a93a4Safresh1L<https://rt.cpan.org/Ticket/Create.html?Queue=bignum> (requires login).
790256a93a4Safresh1We will be notified, and then you'll automatically be notified of
791256a93a4Safresh1progress on your bug as I make changes.
792256a93a4Safresh1
793256a93a4Safresh1=head1 SUPPORT
794256a93a4Safresh1
795256a93a4Safresh1You can find documentation for this module with the perldoc command.
796256a93a4Safresh1
797256a93a4Safresh1    perldoc bigfloat
798256a93a4Safresh1
799256a93a4Safresh1You can also look for information at:
800256a93a4Safresh1
801256a93a4Safresh1=over 4
802256a93a4Safresh1
803256a93a4Safresh1=item * GitHub
804256a93a4Safresh1
805256a93a4Safresh1L<https://github.com/pjacklam/p5-bignum>
806256a93a4Safresh1
807256a93a4Safresh1=item * RT: CPAN's request tracker
808256a93a4Safresh1
809256a93a4Safresh1L<https://rt.cpan.org/Dist/Display.html?Name=bignum>
810256a93a4Safresh1
811256a93a4Safresh1=item * MetaCPAN
812256a93a4Safresh1
813256a93a4Safresh1L<https://metacpan.org/release/bignum>
814256a93a4Safresh1
815256a93a4Safresh1=item * CPAN Testers Matrix
816256a93a4Safresh1
817256a93a4Safresh1L<http://matrix.cpantesters.org/?dist=bignum>
818256a93a4Safresh1
819256a93a4Safresh1=back
820256a93a4Safresh1
821256a93a4Safresh1=head1 LICENSE
822256a93a4Safresh1
823256a93a4Safresh1This program is free software; you may redistribute it and/or modify it under
824256a93a4Safresh1the same terms as Perl itself.
825256a93a4Safresh1
826256a93a4Safresh1=head1 SEE ALSO
827256a93a4Safresh1
828256a93a4Safresh1L<bigint> and L<bigrat>.
829256a93a4Safresh1
830256a93a4Safresh1L<Math::BigInt>, L<Math::BigFloat>, L<Math::BigRat> and L<Math::Big> as well as
831256a93a4Safresh1L<Math::BigInt::FastCalc>, L<Math::BigInt::Pari> and L<Math::BigInt::GMP>.
832256a93a4Safresh1
833256a93a4Safresh1=head1 AUTHORS
834256a93a4Safresh1
835256a93a4Safresh1=over 4
836256a93a4Safresh1
837256a93a4Safresh1=item *
838256a93a4Safresh1
839256a93a4Safresh1(C) by Tels L<http://bloodgate.com/> in early 2002 - 2007.
840256a93a4Safresh1
841256a93a4Safresh1=item *
842256a93a4Safresh1
843256a93a4Safresh1Maintained by Peter John Acklam E<lt>pjacklam@gmail.comE<gt>, 2014-.
844256a93a4Safresh1
845256a93a4Safresh1=back
846256a93a4Safresh1
847256a93a4Safresh1=cut
848