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