Home | History | Annotate | Line # | Download | only in perl
      1      1.1  mrg # GMP perl module
      2      1.1  mrg 
      3  1.1.1.2  mrg # Copyright 2001-2004 Free Software Foundation, Inc.
      4      1.1  mrg #
      5  1.1.1.2  mrg #  This file is part of the GNU MP Library.
      6      1.1  mrg #
      7  1.1.1.2  mrg #  The GNU MP Library is free software; you can redistribute it and/or modify
      8  1.1.1.2  mrg #  it under the terms of either:
      9      1.1  mrg #
     10  1.1.1.2  mrg #    * the GNU Lesser General Public License as published by the Free
     11  1.1.1.2  mrg #      Software Foundation; either version 3 of the License, or (at your
     12  1.1.1.2  mrg #      option) any later version.
     13      1.1  mrg #
     14  1.1.1.2  mrg #  or
     15  1.1.1.2  mrg #
     16  1.1.1.2  mrg #    * the GNU General Public License as published by the Free Software
     17  1.1.1.2  mrg #      Foundation; either version 2 of the License, or (at your option) any
     18  1.1.1.2  mrg #      later version.
     19  1.1.1.2  mrg #
     20  1.1.1.2  mrg #  or both in parallel, as here.
     21  1.1.1.2  mrg #
     22  1.1.1.2  mrg #  The GNU MP Library is distributed in the hope that it will be useful, but
     23  1.1.1.2  mrg #  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     24  1.1.1.2  mrg #  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     25  1.1.1.2  mrg #  for more details.
     26  1.1.1.2  mrg #
     27  1.1.1.2  mrg #  You should have received copies of the GNU General Public License and the
     28  1.1.1.2  mrg #  GNU Lesser General Public License along with the GNU MP Library.  If not,
     29  1.1.1.2  mrg #  see https://www.gnu.org/licenses/.
     30      1.1  mrg 
     31      1.1  mrg # [Note: The above copyright notice is repeated in the documentation section
     32      1.1  mrg # below, in order to get it into man pages etc generated by the various pod
     33      1.1  mrg # conversions.  When changing, be sure to update below too.]
     34      1.1  mrg 
     35      1.1  mrg 
     36      1.1  mrg # This code is designed to work with perl 5.005, so it and the sub-packages
     37      1.1  mrg # aren't as modern as they could be.
     38      1.1  mrg 
     39      1.1  mrg package GMP;
     40      1.1  mrg 
     41      1.1  mrg require Symbol;
     42      1.1  mrg require Exporter;
     43      1.1  mrg require DynaLoader;
     44      1.1  mrg @ISA = qw(Exporter DynaLoader);
     45      1.1  mrg 
     46      1.1  mrg @EXPORT = qw();
     47      1.1  mrg @EXPORT_OK = qw(version);
     48      1.1  mrg %EXPORT_TAGS = ('all' => [qw(
     49      1.1  mrg                              get_d get_d_2exp get_si get_str integer_p
     50      1.1  mrg                              printf sgn sprintf)],
     51      1.1  mrg 		'constants' => [()]);
     52      1.1  mrg Exporter::export_ok_tags('all');
     53      1.1  mrg 
     54      1.1  mrg $VERSION = '2.00';
     55      1.1  mrg bootstrap GMP $VERSION;
     56      1.1  mrg 
     57      1.1  mrg 
     58      1.1  mrg # The format string is cut up into "%" specifiers so GMP types can be
     59      1.1  mrg # passed to GMP::sprintf_internal.  Any "*"s are interpolated before
     60      1.1  mrg # calling sprintf_internal, which saves worrying about variable
     61      1.1  mrg # argument lists there.
     62      1.1  mrg #
     63      1.1  mrg # Because sprintf_internal is only called after the conversion and
     64      1.1  mrg # operand have been checked there won't be any crashes from a bad
     65      1.1  mrg # format string.
     66      1.1  mrg #
     67      1.1  mrg sub sprintf {
     68      1.1  mrg   my $fmt = shift;
     69      1.1  mrg   my $out = '';
     70      1.1  mrg   my ($pre, $dummy, $pat, $rest);
     71      1.1  mrg 
     72      1.1  mrg   while (($pre, $dummy, $pat, $rest) = ($fmt =~ /^((%%|[^%])*)(%[- +#.*hlLqv\d]*[bcdfeEgGinopsuxX])(.*)$/s)) {
     73      1.1  mrg 
     74      1.1  mrg     $out .= $pre;
     75      1.1  mrg 
     76      1.1  mrg     my $pat2 = $pat;    # $pat with "*"s expanded
     77      1.1  mrg     my @params = ();    # arguments per "*"s
     78      1.1  mrg     while ($pat2 =~ /[*]/) {
     79      1.1  mrg       my $arg = shift;
     80      1.1  mrg       $pat2 =~ s/[*]/$arg/;
     81      1.1  mrg       push @params, $arg;
     82      1.1  mrg     }
     83      1.1  mrg 
     84      1.1  mrg     if (UNIVERSAL::isa($_[0],"GMP::Mpz")) {
     85      1.1  mrg       if ($pat2 !~ /[dioxX]$/) {
     86      1.1  mrg 	die "GMP::sprintf: unsupported output format for mpz: $pat2\n";
     87      1.1  mrg       }
     88      1.1  mrg       $pat2 =~ s/(.)$/Z$1/;
     89      1.1  mrg       $out .= sprintf_internal ($pat2, shift);
     90      1.1  mrg 
     91      1.1  mrg     } elsif (UNIVERSAL::isa($_[0],"GMP::Mpq")) {
     92      1.1  mrg       if ($pat2 !~ /[dioxX]$/) {
     93      1.1  mrg 	die "GMP::sprintf: unsupported output format for mpq: $pat2\n";
     94      1.1  mrg       }
     95      1.1  mrg       $pat2 =~ s/(.)$/Q$1/;
     96      1.1  mrg       $out .= sprintf_internal ($pat2, shift);
     97      1.1  mrg 
     98      1.1  mrg     } elsif (UNIVERSAL::isa($_[0],"GMP::Mpf")) {
     99      1.1  mrg       if ($pat2 !~ /[eEfgG]$/) {
    100      1.1  mrg 	die "GMP::sprintf: unsupported output format for mpf: $pat2\n";
    101      1.1  mrg       }
    102      1.1  mrg       $pat2 =~ s/(.)$/F$1/;
    103      1.1  mrg       $out .= sprintf_internal ($pat2, shift);
    104      1.1  mrg 
    105      1.1  mrg     } elsif ($pat =~ /n$/) {
    106      1.1  mrg       # do it this way so h, l or V type modifiers are respected, and use a
    107      1.1  mrg       # dummy variable to avoid a warning about discarding the value
    108      1.1  mrg       my $dummy = sprintf "%s$pat", $out, $_[0];
    109      1.1  mrg       shift;
    110      1.1  mrg 
    111      1.1  mrg     } else {
    112      1.1  mrg       $out .= sprintf $pat, @params, shift;
    113      1.1  mrg     }
    114      1.1  mrg 
    115      1.1  mrg     $fmt = $rest;
    116      1.1  mrg   }
    117      1.1  mrg   $out .= $fmt;
    118      1.1  mrg   return $out;
    119      1.1  mrg }
    120      1.1  mrg 
    121      1.1  mrg sub printf {
    122      1.1  mrg   if (ref($_[0]) eq 'GLOB') {
    123      1.1  mrg     my $h = Symbol::qualify_to_ref(shift, caller);
    124      1.1  mrg     print $h GMP::sprintf(@_);
    125      1.1  mrg   } else {
    126      1.1  mrg     print STDOUT GMP::sprintf(@_);
    127      1.1  mrg   }
    128      1.1  mrg }
    129      1.1  mrg 
    130      1.1  mrg 1;
    131      1.1  mrg __END__
    132      1.1  mrg 
    133      1.1  mrg 
    134      1.1  mrg 
    135      1.1  mrg =head1 NAME
    136      1.1  mrg 
    137      1.1  mrg GMP - Perl interface to the GNU Multiple Precision Arithmetic Library
    138      1.1  mrg 
    139      1.1  mrg =head1 SYNOPSIS
    140      1.1  mrg 
    141      1.1  mrg     use GMP;
    142      1.1  mrg     use GMP::Mpz;
    143      1.1  mrg     use GMP::Mpq;
    144      1.1  mrg     use GMP::Mpf;
    145      1.1  mrg     use GMP::Rand;
    146      1.1  mrg 
    147      1.1  mrg =head1 DESCRIPTION
    148      1.1  mrg 
    149      1.1  mrg This module provides access to GNU MP arbitrary precision integers,
    150      1.1  mrg rationals and floating point.
    151      1.1  mrg 
    152      1.1  mrg No functions are exported from these packages by default, but can be
    153      1.1  mrg selected in the usual way, or the tag :all for everything.
    154      1.1  mrg 
    155      1.1  mrg     use GMP::Mpz qw(gcd, lcm);   # just these functions
    156      1.1  mrg     use GMP::Mpq qw(:all);       # everything in mpq
    157      1.1  mrg 
    158      1.1  mrg =head2 GMP::Mpz
    159      1.1  mrg 
    160      1.1  mrg This class provides arbitrary precision integers.  A new mpz can be
    161      1.1  mrg constructed with C<mpz>.  The initial value can be an integer, float,
    162      1.1  mrg string, mpz, mpq or mpf.  Floats, mpq and mpf will be automatically
    163      1.1  mrg truncated to an integer.
    164      1.1  mrg 
    165      1.1  mrg     use GMP::Mpz qw(:all);
    166      1.1  mrg     my $a = mpz(123);
    167      1.1  mrg     my $b = mpz("0xFFFF");
    168      1.1  mrg     my $c = mpz(1.5);       # truncated
    169      1.1  mrg 
    170      1.1  mrg The following overloaded operators are available, and corresponding
    171      1.1  mrg assignment forms like C<+=>,
    172      1.1  mrg 
    173      1.1  mrg =over 4
    174      1.1  mrg 
    175      1.1  mrg =item
    176      1.1  mrg 
    177      1.1  mrg + - * / % E<lt>E<lt> E<gt>E<gt> ** & | ^ ! E<lt> E<lt>= == != E<gt> E<gt>=
    178      1.1  mrg E<lt>=E<gt> abs not sqrt
    179      1.1  mrg 
    180      1.1  mrg =back
    181      1.1  mrg 
    182      1.1  mrg C</> and C<%> round towards zero (as per the C<tdiv> functions in GMP).
    183      1.1  mrg 
    184      1.1  mrg The following functions are available, behaving the same as the
    185      1.1  mrg corresponding GMP mpz functions,
    186      1.1  mrg 
    187      1.1  mrg =over 4
    188      1.1  mrg 
    189      1.1  mrg =item
    190      1.1  mrg 
    191      1.1  mrg bin, cdiv, cdiv_2exp, clrbit, combit, congruent_p, congruent_2exp_p,
    192      1.1  mrg divexact, divisible_p, divisible_2exp_p, even_p, fac, fdiv, fdiv_2exp, fib,
    193      1.1  mrg fib2, gcd, gcdext, hamdist, invert, jacobi, kronecker, lcm, lucnum, lucnum2,
    194      1.1  mrg mod, mpz_export, mpz_import, nextprime, odd_p, perfect_power_p,
    195      1.1  mrg perfect_square_p, popcount, powm, probab_prime_p, realloc, remove, root,
    196      1.1  mrg roote, scan0, scan1, setbit, sizeinbase, sqrtrem, tdiv, tdiv_2exp, tstbit
    197      1.1  mrg 
    198      1.1  mrg =back
    199      1.1  mrg 
    200      1.1  mrg C<cdiv>, C<fdiv> and C<tdiv> and their C<2exp> variants return a
    201      1.1  mrg quotient/remainder pair.  C<fib2> returns a pair F[n] and F[n-1], similarly
    202      1.1  mrg C<lucnum2>.  C<gcd> and C<lcm> accept a variable number of arguments (one or
    203      1.1  mrg more).  C<gcdext> returns a triplet of gcd and two cofactors, for example
    204      1.1  mrg 
    205      1.1  mrg     use GMP::Mpz qw(:all);
    206      1.1  mrg     $a = 7257;
    207      1.1  mrg     $b = 10701;
    208      1.1  mrg     ($g, $x, $y) = gcdext ($a, $b);
    209      1.1  mrg     print "gcd($a,$b) is $g, and $g == $a*$x + $b*$y\n";
    210      1.1  mrg 
    211      1.1  mrg C<mpz_import> and C<mpz_export> are so named to avoid the C<import> keyword.
    212      1.1  mrg Their parameters are as follows,
    213      1.1  mrg 
    214      1.1  mrg     $z = mpz_import ($order, $size, $endian, $nails, $string);
    215      1.1  mrg     $string = mpz_export ($order, $size, $endian, $nails, $z);
    216      1.1  mrg 
    217      1.1  mrg The order, size, endian and nails parameters are as per the corresponding C
    218      1.1  mrg functions.  The string input for C<mpz_import> is interpreted as byte data
    219      1.1  mrg and must be a multiple of $size bytes.  C<mpz_export> conversely returns a
    220      1.1  mrg string of byte data, which will be a multiple of $size bytes.
    221      1.1  mrg 
    222      1.1  mrg C<invert> returns the inverse, or undef if it doesn't exist.  C<remove>
    223      1.1  mrg returns a remainder/multiplicity pair.  C<root> returns the nth root, and
    224      1.1  mrg C<roote> returns a root/bool pair, the bool indicating whether the root is
    225      1.1  mrg exact.  C<sqrtrem> and C<rootrem> return a root/remainder pair.
    226      1.1  mrg 
    227      1.1  mrg C<clrbit>, C<combit> and C<setbit> expect a variable which they can modify,
    228      1.1  mrg it doesn't make sense to pass a literal constant.  Only the given variable
    229      1.1  mrg is modified, if other variables are referencing the same mpz object then a
    230      1.1  mrg new copy is made of it.  If the variable isn't an mpz it will be coerced to
    231      1.1  mrg one.  For instance,
    232      1.1  mrg 
    233      1.1  mrg     use GMP::Mpz qw(setbit);
    234      1.1  mrg     setbit (123, 0);  # wrong, don't pass a constant
    235      1.1  mrg     $a = mpz(6);
    236      1.1  mrg     $b = $a;
    237      1.1  mrg     setbit ($a, 0);   # $a becomes 7, $b stays at 6
    238      1.1  mrg 
    239      1.1  mrg C<scan0> and C<scan1> return ~0 if no 0 or 1 bit respectively is found.
    240      1.1  mrg 
    241      1.1  mrg =head2 GMP::Mpq
    242      1.1  mrg 
    243      1.1  mrg This class provides rationals with arbitrary precision numerators and
    244      1.1  mrg denominators.  A new mpq can be constructed with C<mpq>.  The initial value
    245      1.1  mrg can be an integer, float, string, mpz, mpq or mpf, or a pair of integers or
    246      1.1  mrg mpz's.  No precision is lost when converting a float or mpf, the exact value
    247      1.1  mrg is retained.
    248      1.1  mrg 
    249      1.1  mrg     use GMP::Mpq qw(:all);
    250      1.1  mrg     $a = mpq();              # zero
    251      1.1  mrg     $b = mpq(0.5);           # gives 1/2
    252      1.1  mrg     $b = mpq(14);            # integer 14
    253      1.1  mrg     $b = mpq(3,4);           # fraction 3/4
    254      1.1  mrg     $b = mpq("7/12");        # fraction 7/12
    255      1.1  mrg     $b = mpq("0xFF/0x100");  # fraction 255/256
    256      1.1  mrg 
    257      1.1  mrg When a fraction is given, it should be in the canonical form specified in
    258      1.1  mrg the GMP manual, which is denominator positive, no common factors, and zero
    259      1.1  mrg always represented as 0/1.  If not then C<canonicalize> can be called to put
    260      1.1  mrg it in that form.  For example,
    261      1.1  mrg 
    262      1.1  mrg     use GMP::Mpq qw(:all);
    263      1.1  mrg     $q = mpq(21,15);   # eek! common factor 3
    264      1.1  mrg     canonicalize($q);  # get rid of it
    265      1.1  mrg 
    266      1.1  mrg The following overloaded operators are available, and corresponding
    267      1.1  mrg assignment forms like C<+=>,
    268      1.1  mrg 
    269      1.1  mrg =over 4
    270      1.1  mrg 
    271      1.1  mrg =item
    272      1.1  mrg 
    273      1.1  mrg + - * / E<lt>E<lt> E<gt>E<gt> ** ! E<lt> E<lt>= == != E<gt> E<gt>=
    274      1.1  mrg E<lt>=E<gt> abs not
    275      1.1  mrg 
    276      1.1  mrg =back
    277      1.1  mrg 
    278      1.1  mrg The following functions are available,
    279      1.1  mrg 
    280      1.1  mrg =over 4
    281      1.1  mrg 
    282      1.1  mrg =item
    283      1.1  mrg 
    284      1.1  mrg den, inv, num
    285      1.1  mrg 
    286      1.1  mrg =back
    287      1.1  mrg 
    288      1.1  mrg C<inv> calculates 1/q, as per the corresponding GMP function.  C<num> and
    289      1.1  mrg C<den> return an mpz copy of the numerator or denominator respectively.  In
    290      1.1  mrg the future C<num> and C<den> might give lvalues so the original mpq can be
    291      1.1  mrg modified through them, but this is not done currently.
    292      1.1  mrg 
    293      1.1  mrg =head2 GMP::Mpf
    294      1.1  mrg 
    295      1.1  mrg This class provides arbitrary precision floating point numbers.  The
    296      1.1  mrg mantissa is an arbitrary user-selected precision and the exponent is a fixed
    297      1.1  mrg size (one machine word).
    298      1.1  mrg 
    299      1.1  mrg A new mpf can be constructed with C<mpf>.  The initial value can be an
    300      1.1  mrg integer, float, string, mpz, mpq or mpf.  The second argument specifies the
    301      1.1  mrg desired precision in bits, or if omitted then the default precision is used.
    302      1.1  mrg 
    303      1.1  mrg     use GMP::Mpf qw(:all);
    304      1.1  mrg     $a = mpf();         # zero
    305      1.1  mrg     $b = mpf(-7.5);     # default precision
    306      1.1  mrg     $c = mpf(1.5, 500); # 500 bits precision
    307      1.1  mrg     $d = mpf("1.0000000000000001");
    308      1.1  mrg 
    309      1.1  mrg The following overloaded operators are available, with the corresponding
    310      1.1  mrg assignment forms like C<+=>,
    311      1.1  mrg 
    312      1.1  mrg =over 4
    313      1.1  mrg 
    314      1.1  mrg =item
    315      1.1  mrg 
    316      1.1  mrg + - * / E<lt>E<lt> E<gt>E<gt> ** ! E<lt> E<lt>= == != E<gt> E<gt>=
    317      1.1  mrg E<lt>=E<gt> abs not sqrt
    318      1.1  mrg 
    319      1.1  mrg =back
    320      1.1  mrg 
    321      1.1  mrg The following functions are available, behaving the same as the
    322      1.1  mrg corresponding GMP mpf functions,
    323      1.1  mrg 
    324      1.1  mrg =over 4
    325      1.1  mrg 
    326      1.1  mrg =item
    327      1.1  mrg 
    328      1.1  mrg ceil, floor, get_default_prec, get_prec, mpf_eq, set_default_prec, set_prec,
    329      1.1  mrg trunc
    330      1.1  mrg 
    331      1.1  mrg =back
    332      1.1  mrg 
    333      1.1  mrg C<mpf_eq> is so named to avoid clashing with the perl C<eq> operator.
    334      1.1  mrg 
    335      1.1  mrg C<set_prec> expects a variable which it can modify, it doesn't make sense to
    336      1.1  mrg pass a literal constant.  Only the given variable is modified, if other
    337      1.1  mrg variables are referencing the same mpf object then a new copy is made of it.
    338      1.1  mrg If the variable isn't an mpf it will be coerced to one.
    339      1.1  mrg 
    340      1.1  mrg Results are the same precision as inputs, or if two mpf's are given to a
    341      1.1  mrg binary operator then the precision of the first is used.  For example,
    342      1.1  mrg 
    343      1.1  mrg     use GMP::Mpf qw(mpf);
    344      1.1  mrg     $a = mpf(2.0, 100);
    345      1.1  mrg     $b = mpf(2.0, 500);
    346      1.1  mrg     $c = $a + $b;         # gives 100 bits precision
    347      1.1  mrg 
    348      1.1  mrg Mpf to string conversion via "" or the usual string contexts uses C<$#> the
    349      1.1  mrg same as normal float to string conversions, or defaults to C<%.g> if C<$#>
    350      1.1  mrg is not defined.  C<%.g> means all significant digits in the selected
    351      1.1  mrg precision.
    352      1.1  mrg 
    353      1.1  mrg =head2 GMP class
    354      1.1  mrg 
    355      1.1  mrg The following functions are available in the GMP class,
    356      1.1  mrg 
    357      1.1  mrg =over 4
    358      1.1  mrg 
    359      1.1  mrg =item
    360      1.1  mrg 
    361      1.1  mrg fits_slong_p, get_d, get_d_2exp, get_si, get_str, integer_p, printf, sgn,
    362      1.1  mrg sprintf, version
    363      1.1  mrg 
    364      1.1  mrg =back
    365      1.1  mrg 
    366      1.1  mrg C<get_d_2exp> accepts any integer, string, float, mpz, mpq or mpf operands
    367      1.1  mrg and returns a float and an integer exponent,
    368      1.1  mrg 
    369      1.1  mrg     ($dbl, $exp) = get_d_2exp (mpf ("3.0"));
    370      1.1  mrg     # dbl is 0.75, exp is 2
    371      1.1  mrg 
    372      1.1  mrg C<get_str> takes an optional second argument which is the base, defaulting
    373      1.1  mrg to decimal.  A negative base means upper case, as per the C functions.  For
    374      1.1  mrg integer, integer string, mpz or mpq operands a string is returned.
    375      1.1  mrg 
    376      1.1  mrg     use GMP qw(:all);
    377      1.1  mrg     use GMP::Mpq qw(:all);
    378      1.1  mrg     print get_str(mpq(-5,8)),"\n";      # -5/8
    379      1.1  mrg     print get_str(255,16),"\n";         # ff
    380      1.1  mrg 
    381      1.1  mrg For float, float strings or mpf operands, C<get_str> accepts an optional
    382      1.1  mrg third parameter being how many digits to produce, defaulting to 0 which
    383      1.1  mrg means all digits.  (Only as many digits as can be accurately represented by
    384      1.1  mrg the float precision are ever produced though.)  A string/exponent pair is
    385      1.1  mrg returned, as per the C mpf_get_str function.  For example,
    386      1.1  mrg 
    387      1.1  mrg     use GMP qw(:all);
    388      1.1  mrg     use GMP::Mpf qw(:all);
    389      1.1  mrg     ($s, $e) = get_str(111.111111111, 10, 4);
    390      1.1  mrg     printf ".$se$e\n";                  # .1111e3
    391      1.1  mrg     ($s, $e) = get_str(1.625, 10);
    392      1.1  mrg     print "0.$s*10^$e\n";               # 0.1625*10^1
    393      1.1  mrg     ($s, $e) = get_str(mpf(2)**20, 16);
    394      1.1  mrg     printf ".%s@%x\n", $s, $e;          # .1@14
    395      1.1  mrg 
    396      1.1  mrg C<printf> and C<sprintf> allow formatted output of GMP types.  mpz and mpq
    397      1.1  mrg values can be used with integer conversions (d, o, x, X) and mpf with float
    398      1.1  mrg conversions (f, e, E, g, G).  All the standard perl printf features are
    399      1.1  mrg available too.  For example,
    400      1.1  mrg 
    401      1.1  mrg     use GMP::Mpz qw(mpz);
    402      1.1  mrg     use GMP::Mpf qw(mpf);
    403      1.1  mrg     GMP::printf ("%d %d %s", 123, mpz(2)**128, 'foo');
    404      1.1  mrg     GMP::printf STDERR "%.40f", mpf(1.234);
    405      1.1  mrg 
    406      1.1  mrg In perl 5.6.1 it doesn't seem to work to export C<printf>, the plain builtin
    407      1.1  mrg C<printf> is reached unless calls are C<&printf()> style.  Explicit use of
    408      1.1  mrg C<GMP::printf> is suggested.  C<sprintf> doesn't suffer this problem.
    409      1.1  mrg 
    410      1.1  mrg     use GMP qw(sprintf);
    411      1.1  mrg     use GMP::Mpq qw(mpq);
    412      1.1  mrg     $s = sprintf "%x", mpq(15,16);
    413      1.1  mrg 
    414      1.1  mrg C<version> is not exported by default or by tag :all, calling it as
    415      1.1  mrg C<GMP::version()> is recommended.  It returns the GMP library version
    416      1.1  mrg string, which is not to be confused with the module version number.
    417      1.1  mrg 
    418      1.1  mrg The other GMP module functions behave as per the corresponding GMP routines,
    419      1.1  mrg and accept any integer, string, float, mpz, mpq or mpf.  For example,
    420      1.1  mrg 
    421      1.1  mrg     use GMP qw(:all);
    422      1.1  mrg     use GMP::Mpz qw(mpz);
    423      1.1  mrg     $z = mpz(123);
    424      1.1  mrg     print sgn($z);    # gives 1
    425      1.1  mrg 
    426      1.1  mrg Because each of GMP::Mpz, GMP::Mpq and GMP::Mpf is a sub-class of GMP,
    427      1.1  mrg C<-E<gt>> style calls work too.
    428      1.1  mrg 
    429      1.1  mrg     use GMP qw(:all);
    430      1.1  mrg     use GMP::Mpq qw(mpf);
    431      1.1  mrg     $q = mpq(-5,7);
    432      1.1  mrg     if ($q->integer_p())   # false
    433      1.1  mrg       ...
    434      1.1  mrg 
    435      1.1  mrg =head2 GMP::Rand
    436      1.1  mrg 
    437      1.1  mrg This class provides objects holding an algorithm and state for random number
    438      1.1  mrg generation.  C<randstate> creates a new object, for example,
    439      1.1  mrg 
    440      1.1  mrg     use GMP::Rand qw(randstate);
    441      1.1  mrg     $r = randstate();
    442      1.1  mrg     $r = randstate('lc_2exp_size', 64);
    443      1.1  mrg     $r = randstate('lc_2exp', 43840821, 1, 32);
    444      1.1  mrg     $r = randstate('mt');
    445      1.1  mrg     $r = randstate($another_r);
    446      1.1  mrg 
    447      1.1  mrg With no parameters this corresponds to the C function
    448      1.1  mrg C<gmp_randinit_default>, and is a compromise between speed and randomness.
    449      1.1  mrg 'lc_2exp_size' corresponds to C<gmp_randinit_lc_2exp_size>, 'lc_2exp'
    450      1.1  mrg corresponds to C<gmp_randinit_lc_2exp>, and 'mt' corresponds to
    451      1.1  mrg C<gmp_randinit_mt>.  Or when passed another randstate object, a copy of that
    452      1.1  mrg object is made.
    453      1.1  mrg 
    454      1.1  mrg 'lc_2exp_size' can fail if the requested size is bigger than the internal
    455      1.1  mrg table provides for, in which case undef is returned.  The maximum size
    456      1.1  mrg currently supported is 128.  The other forms always succeed.
    457      1.1  mrg 
    458      1.1  mrg A randstate can be seeded with an integer or mpz, using the C<seed> method.
    459      1.1  mrg /dev/random might be a good source of randomness, or time() or
    460      1.1  mrg Time::HiRes::time() might be adequate, depending on the application.
    461      1.1  mrg 
    462      1.1  mrg     $r->seed(time()));
    463      1.1  mrg 
    464      1.1  mrg Random numbers can be generated with the following functions,
    465      1.1  mrg 
    466      1.1  mrg =over 4
    467      1.1  mrg 
    468      1.1  mrg =item
    469      1.1  mrg 
    470      1.1  mrg mpf_urandomb, mpz_rrandomb, mpz_urandomb, mpz_urandomm,
    471      1.1  mrg gmp_urandomb_ui, gmp_urandomm_ui
    472      1.1  mrg 
    473      1.1  mrg =back
    474      1.1  mrg 
    475      1.1  mrg Each constructs a new mpz or mpf and with a distribution per the
    476      1.1  mrg corresponding GMP function.  For example,
    477      1.1  mrg 
    478      1.1  mrg     use GMP::Rand (:all);
    479      1.1  mrg     $r = randstate();
    480      1.1  mrg     $a = mpz_urandomb($r,256);         # uniform mpz, 256 bits
    481      1.1  mrg     $b = mpz_urandomm($r,mpz(3)**100); # uniform mpz, 0 to 3**100-1
    482      1.1  mrg     $c = mpz_rrandomb($r,1024);        # special mpz, 1024 bits
    483      1.1  mrg     $f = mpf_urandomb($r,128);         # uniform mpf, 128 bits, 0<=$f<1
    484      1.1  mrg     $f = gmp_urandomm_ui($r,56);       # uniform int, 0 to 55
    485      1.1  mrg 
    486      1.1  mrg =head2 Coercion
    487      1.1  mrg 
    488      1.1  mrg Arguments to operators and functions are converted as necessary to the
    489      1.1  mrg appropriate type.  For instance C<**> requires an unsigned integer exponent,
    490      1.1  mrg and an mpq argument will be converted, so long as it's an integer in the
    491      1.1  mrg appropriate range.
    492      1.1  mrg 
    493      1.1  mrg     use GMP::Mpz (mpz);
    494      1.1  mrg     use GMP::Mpq (mpq);
    495      1.1  mrg     $p = mpz(3) ** mpq(45);   # allowed, 45 is an integer
    496      1.1  mrg 
    497      1.1  mrg It's an error if a conversion to an integer or mpz would cause any
    498      1.1  mrg truncation.  For example,
    499      1.1  mrg 
    500      1.1  mrg     use GMP::Mpz (mpz);
    501      1.1  mrg     $p = mpz(3) + 1.25;       # not allowed
    502      1.1  mrg     $p = mpz(3) + mpz(1.25);  # allowed, explicit truncation
    503      1.1  mrg 
    504      1.1  mrg Comparisons, however, accept any combination of operands and are always done
    505      1.1  mrg exactly.  For example,
    506      1.1  mrg 
    507      1.1  mrg     use GMP::Mpz (mpz);
    508      1.1  mrg     print mpz(3) < 3.1;       # true
    509      1.1  mrg 
    510      1.1  mrg Variables used on the left of an assignment operator like C<+=> are subject
    511      1.1  mrg to coercion too.  An integer, float or string will change type when an mpz,
    512      1.1  mrg mpq or mpf is applied to it.  For example,
    513      1.1  mrg 
    514      1.1  mrg     use GMP::Mpz (mpz);
    515      1.1  mrg     $a = 1;
    516      1.1  mrg     $a += mpz(1234);   # $a becomes an mpz
    517      1.1  mrg 
    518      1.1  mrg =head2 Overloading
    519      1.1  mrg 
    520      1.1  mrg The rule for binary operators in the C<overload> mechanism is that if both
    521      1.1  mrg operands are class objects then the method from the first is used.  This
    522      1.1  mrg determines the result type when mixing GMP classes.  For example,
    523      1.1  mrg 
    524      1.1  mrg     use GMP::Mpz (mpz);
    525      1.1  mrg     use GMP::Mpq (mpq);
    526      1.1  mrg     use GMP::Mpf (mpf);
    527      1.1  mrg     $z = mpz(123);
    528      1.1  mrg     $q = mpq(3,2);
    529      1.1  mrg     $f = mpf(1.375)
    530      1.1  mrg     print $q+$f;     # gives an mpq
    531      1.1  mrg     print $f+$z;     # gives an mpf
    532      1.1  mrg     print $z+$f;     # not allowed, would lose precision
    533      1.1  mrg 
    534      1.1  mrg =head2 Constants
    535      1.1  mrg 
    536      1.1  mrg A special tag C<:constants> is recognised in the module exports list.  It
    537      1.1  mrg doesn't select any functions, but indicates that perl constants should be
    538      1.1  mrg GMP objects.  This can only be used on one of GMP::Mpz, GMP::Mpq or GMP::Mpf
    539      1.1  mrg at any one time, since they apply different rules.
    540      1.1  mrg 
    541      1.1  mrg GMP::Mpz will treat constants as mpz's if they're integers, or ordinary
    542      1.1  mrg floats if not.  For example,
    543      1.1  mrg 
    544      1.1  mrg     use GMP::Mpz qw(:constants);
    545      1.1  mrg     print 764861287634126387126378128,"\n";   # an mpz
    546      1.1  mrg     print 1.25,"\n";                          # a float
    547      1.1  mrg 
    548      1.1  mrg GMP::Mpq is similar, treating integers as mpq's and leaving floats to the
    549      1.1  mrg normal perl handling.  Something like 3/4 is read as two integer mpq's and a
    550      1.1  mrg division, but that's fine since it gives the intended fraction.
    551      1.1  mrg 
    552      1.1  mrg     use GMP::Mpq qw(:constants);
    553      1.1  mrg     print 3/4,"\n";    # an mpq
    554      1.1  mrg     print 1.25,"\n";   # a float
    555      1.1  mrg 
    556      1.1  mrg GMP::Mpf will treat all constants as mpf's using the default precision.
    557      1.1  mrg BEGIN blocks can be used to set that precision while the code is parsed.
    558      1.1  mrg For example,
    559      1.1  mrg 
    560      1.1  mrg     use GMP::Mpf qw(:constants);
    561      1.1  mrg     BEGIN { GMP::Mpf::set_default_prec(256); }
    562      1.1  mrg     print 1/3;
    563      1.1  mrg     BEGIN { GMP::Mpf::set_default_prec(64); }
    564      1.1  mrg     print 5/7;
    565      1.1  mrg 
    566      1.1  mrg A similar special tag :noconstants is recognised to turn off the constants
    567      1.1  mrg feature.  For example,
    568      1.1  mrg 
    569      1.1  mrg     use GMP::Mpz qw(:constants);
    570      1.1  mrg     print 438249738748174928193,"\n";   # an mpz
    571      1.1  mrg     use GMP::Mpz qw(:noconstants);
    572      1.1  mrg     print 438249738748174928193,"\n";   # now a float
    573      1.1  mrg 
    574      1.1  mrg All three 'integer', 'binary' and 'float' constant methods are captured.
    575      1.1  mrg 'float' is captured even for GMP::Mpz and GMP::Mpq since perl by default
    576      1.1  mrg treats integer strings as floats if they don't fit a plain integer.
    577      1.1  mrg 
    578      1.1  mrg =head1 SEE ALSO
    579      1.1  mrg 
    580      1.1  mrg GMP manual, L<perl>, L<overload>.
    581      1.1  mrg 
    582      1.1  mrg =head1 BUGS
    583      1.1  mrg 
    584      1.1  mrg In perl 5.005_03 on i386 FreeBSD, the overloaded constants sometimes provoke
    585      1.1  mrg seg faults.  Don't know if that's a perl bug or a GMP module bug, though it
    586      1.1  mrg does seem to go bad before reaching anything in GMP.xs.
    587      1.1  mrg 
    588      1.1  mrg There's no way to specify an arbitrary base when converting a string to an
    589      1.1  mrg mpz (or mpq or mpf), only hex or octal with 0x or 0 (for mpz and mpq, but
    590      1.1  mrg not for mpf).
    591      1.1  mrg 
    592      1.1  mrg These modules are not reentrant or thread safe, due to the implementation of
    593      1.1  mrg the XSUBs.
    594      1.1  mrg 
    595      1.1  mrg Returning a new object from the various functions is convenient, but
    596      1.1  mrg assignment versions could avoid creating new objects.  Perhaps they could be
    597      1.1  mrg named after the C language functions, eg. mpq_inv($q,$q);
    598      1.1  mrg 
    599      1.1  mrg It'd be good if C<num> and C<den> gave lvalues so the underlying mpq could
    600      1.1  mrg be manipulated.
    601      1.1  mrg 
    602      1.1  mrg C<printf> could usefully accept %b for mpz, mpq and mpf, and perhaps %x for
    603      1.1  mrg mpf too.
    604      1.1  mrg 
    605      1.1  mrg C<get_str> returning different style values for integer versus float is a
    606      1.1  mrg bit unfortunate.  With mpz, mpq and mpf objects there's no doubt what it
    607      1.1  mrg will do, but on a plain scalar its action depends on whether the scalar was
    608      1.1  mrg promoted to a float at any stage, and then on the GMP module rules about
    609      1.1  mrg using the integer or float part.
    610      1.1  mrg 
    611      1.1  mrg =head1 INTERNALS
    612      1.1  mrg 
    613      1.1  mrg In usual perl object style, an mpz is a reference to an object blessed into
    614      1.1  mrg class C<GMP::Mpz>.  The object holds a pointer to the C language C<mpz_t>
    615      1.1  mrg structure.  Similarly for mpq, mpf and randstate.
    616      1.1  mrg 
    617      1.1  mrg A free list of mpz and mpq values is kept to avoid repeated initializing and
    618      1.1  mrg clearing when objects are created and destroyed.  This aims to help speed,
    619      1.1  mrg but it's not clear whether it's really needed.
    620      1.1  mrg 
    621      1.1  mrg mpf doesn't use a free list because the precision of new objects can be
    622      1.1  mrg different each time.
    623      1.1  mrg 
    624      1.1  mrg No interface to C<mpf_set_prec_raw> is provided.  It wouldn't be very useful
    625      1.1  mrg since there's no way to make an operation store its result in a particular
    626      1.1  mrg object.  The plain C<set_prec> is useful though, for truncating to a lower
    627      1.1  mrg precision, or as a sort of directive that subsequent calculations involving
    628      1.1  mrg that variable should use a higher precision.
    629      1.1  mrg 
    630      1.1  mrg The overheads of perl dynamic typing (operator dispatch, operand type
    631      1.1  mrg checking or coercion) will mean this interface is slower than using C
    632      1.1  mrg directly.
    633      1.1  mrg 
    634      1.1  mrg Some assertion checking is available as a compile-time option.
    635      1.1  mrg 
    636      1.1  mrg =head1 COPYRIGHT
    637      1.1  mrg 
    638  1.1.1.2  mrg Copyright 2001-2004 Free Software Foundation, Inc.
    639      1.1  mrg 
    640      1.1  mrg This file is part of the GNU MP Library.
    641      1.1  mrg 
    642      1.1  mrg The GNU MP Library is free software; you can redistribute it and/or modify
    643  1.1.1.2  mrg it under the terms of either:
    644  1.1.1.2  mrg 
    645  1.1.1.2  mrg   * the GNU Lesser General Public License as published by the Free
    646  1.1.1.2  mrg     Software Foundation; either version 3 of the License, or (at your
    647  1.1.1.2  mrg     option) any later version.
    648  1.1.1.2  mrg 
    649  1.1.1.2  mrg or
    650  1.1.1.2  mrg 
    651  1.1.1.2  mrg   * the GNU General Public License as published by the Free Software
    652  1.1.1.2  mrg     Foundation; either version 2 of the License, or (at your option) any
    653  1.1.1.2  mrg     later version.
    654  1.1.1.2  mrg 
    655  1.1.1.2  mrg or both in parallel, as here.
    656      1.1  mrg 
    657      1.1  mrg The GNU MP Library is distributed in the hope that it will be useful, but
    658      1.1  mrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    659  1.1.1.2  mrg or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    660  1.1.1.2  mrg for more details.
    661      1.1  mrg 
    662  1.1.1.2  mrg You should have received copies of the GNU General Public License and the
    663  1.1.1.2  mrg GNU Lesser General Public License along with the GNU MP Library.  If not,
    664  1.1.1.2  mrg see https://www.gnu.org/licenses/.
    665      1.1  mrg 
    666      1.1  mrg =cut
    667      1.1  mrg 
    668      1.1  mrg # Local variables:
    669      1.1  mrg # perl-indent-level: 2
    670      1.1  mrg # fill-column: 76
    671      1.1  mrg # End:
    672