blib/lib/Math/GMP.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 121 CC 10 SCAR 23.3
CC
10
Cov
0%
AUTOLOAD
30.0
import
12.0
new
12.0
CRAP 10.3
1 package Math::GMP;
2  
3 # Math::GMP, a Perl module for high-speed arbitrary size integer
4 # calculations
5 # Copyright (C) 2000-2008 James H. Turner
6 # Copyright (C) 2008-2009 Greg Sabino Mullane
7  
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Library General Public
10 # License as published by the Free Software Foundation; either
11 # version 2 of the License, or (at your option) any later version.
12  
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Library General Public License for more details.
17  
18 # You should have received a copy of the GNU Library General Public
19 # License along with this library; if not, write to the Free
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  
22 # You can contact the author at chip@redhat.com, chipt@cpan.org, or by mail:
23  
24 # Chip Turner
25 # Red Hat Inc.
26 # 2600 Meridian Park Blvd
27 # Durham, NC 27713
28  
29 use strict;
30 use warnings;
31 use 5.006;
32 use Carp;
33 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD);
34  
35 use overload (
36 '""' => sub { stringify($_[0]) },
37 '0+' => sub { intify($_[0]) },
38  
39 '<=>' => \&op_spaceship,
40 '==' => \&op_eq,
41  
42 '+' => \&op_add,
43 '-' => \&op_sub,
44  
45 '&' => \&band,
46 '^' => \&bxor,
47 '|' => \&bior,
48  
49 '%' => \&op_mod,
50 '**' => sub { $_[2] ? op_pow($_[1], $_[0]) : op_pow($_[0], $_[1]) },
51 '*' => \&op_mul,
52 '/' => \&op_div,
53 );
54  
55 require Exporter;
56 require DynaLoader;
57 require AutoLoader;
58  
59 @ISA = qw(Exporter DynaLoader);
60 # Items to export into callers namespace by default. Note: do not export
61 # names by default without a very good reason. Use EXPORT_OK instead.
62 # Do not simply export all your public functions/methods/constants.
63  
64 our $VERSION = '2.07';
65  
66 sub AUTOLOAD {
67 # This AUTOLOAD is used to 'autoload' constants from the constant()
68 # XS function. If a constant is not found then control is passed
69 # to the AUTOLOAD in AutoLoader.
70  
71 my $constname;
72 ($constname = $AUTOLOAD) =~ s/.*:://;
73 croak '& not defined' if $constname eq 'constant';
74 my $val = constant($constname, @_ ? $_[0] : 0);
75 if ($! != 0) {
76 if ($! =~ /Invalid/) {
77 $AutoLoader::AUTOLOAD = $AUTOLOAD;
78 goto &AutoLoader::AUTOLOAD;
79 } else {
80 croak "Your vendor has not defined Math::GMP macro $constname";
81 }
82 }
83 no strict 'refs'; ## no critic
84 *$AUTOLOAD = sub () { $val };
85 goto &$AUTOLOAD;
86 }
87  
88 bootstrap Math::GMP $VERSION;
89  
90 use strict;
91 sub import {
92 shift;
93 return unless @_;
94 die "unknown import: @_" unless @_ == 1 and $_[0] eq ':constant';
95 overload::constant integer => sub { Math::GMP->new(shift) };
96 return;
97 }
98  
99  
100 sub new {
101 my $class = shift;
102 my $ival = shift || 0;
103 my $base = shift;
104  
105 $ival =~ s/^\+//;
106 $ival =~ s/[ _]//g;
107 my $ret;
108 if ($base) {
109 $ret = Math::GMP::new_from_scalar_with_base($ival, $base);
110 } else {
111 $ival = 0 if $ival =~ /[^\d\-xA-Fa-f]/;
112 $ret = Math::GMP::new_from_scalar($ival);
113 }
114  
115 return $ret;
116 }
117  
118 BEGIN
119 {
120 *DESTROY = \&Math::GMP::destroy;
121 *gcd = \&bgcd;
122 *lcm = \&blcm;
123 }
124  
125 sub add {
126 croak 'add: not enough arguments, two required' if @_ < 2;
127  
128 my $ret = Math::GMP->new(0);
129 add_to_self($ret, shift) while @_;
130  
131 return $ret;
132 }
133  
134 __END__
135  
136 =head1 NAME
137  
138 Math::GMP - High speed arbitrary size integer math
139  
140 =head1 SYNOPSIS
141  
142 use Math::GMP;
143 my $n = new Math::GMP 2;
144  
145 $n = $n ** (256*1024);
146 $n = $n - 1;
147 print "n is now $n\n";
148  
149 =head1 DESCRIPTION
150  
151 Math::GMP was designed to be a drop-in replacement both for
152 Math::BigInt and for regular integer arithmetic. Unlike BigInt,
153 though, Math::GMP uses the GNU gmp library for all of its
154 calculations, as opposed to straight Perl functions. This can result
155 in speed improvements.
156  
157 The downside is that this module requires a C compiler to install -- a
158 small tradeoff in most cases. Also, this module is not 100% compatible
159 with Math::BigInt.
160  
161 A Math::GMP object can be used just as a normal numeric scalar would
162 be -- the module overloads most of the normal arithmetic operators to
163 provide as seamless an interface as possible. However, if you need a
164 perfect interface, you can do the following:
165  
166 use Math::GMP qw(:constant);
167  
168 $n = 2 ** (256 * 1024);
169 print "n is $n\n";
170  
171 This would fail without the ':constant' since Perl would use normal
172 doubles to compute the 250,000 bit number, and thereby overflow it
173 into meaninglessness (smaller exponents yield less accurate data due
174 to floating point rounding).
175  
176 =head1 METHODS
177  
178 Although the non-overload interface is not complete, the following
179 functions do exist:
180  
181 =head2 new
182  
183 $x = Math::GMP->new(123);
184  
185 Creates a new Math::GMP object from the passed string or scalar.
186  
187 $x = Math::GMP->new('abcd', 36);
188  
189 Creates a new Math::GMP object from the first parameter which should
190 be represented in the base specified by the second parameter.
191  
192 =head2 bfac
193  
194 $x = Math::GMP->new(5);
195 $x->bfac(); # 1*2*3*4*5 = 120
196  
197 Calculates the factorial of $x and modifies $x to contain the result.
198  
199 =head2 band
200  
201 $x = Math::GMP->new(6);
202 $x->band(3); # 0b110 & 0b11 = 1
203  
204 Calculates the bit-wise AND of its two arguments and modifies the first
205 argument.
206  
207 =head2 bxor
208  
209 $x = Math::GMP->new(6);
210 $x->bxor(3); # 0b110 & 0b11 = 0b101
211  
212 Calculates the bit-wise XOR of its two arguments and modifies the first
213 argument.
214  
215 =head2 bior
216  
217 $x = Math::GMP->new(6);
218 $x->bior(3); # 0b110 & 0b11 = 0b111
219  
220 Calculates the bit-wise OR of its two arguments and modifies the first
221 argument.
222  
223 =head2 bgcd
224  
225 $x = Math::GMP->new(6);
226 $x->bgcd(4); # 6 / 2 = 2, 4 / 2 = 2 => 2
227  
228 Returns the Greatest Common Divisor of the two arguments.
229  
230 =head2 blcm
231  
232 $x = Math::GMP->new(6);
233 $x->blcm(4); # 6 * 2 = 12, 4 * 3 = 12 => 12
234  
235 Returns the Least Common Multiple of the two arguments.
236  
237 =head2 bmodinv
238  
239 $x = Math::GMP->new(5);
240 $x->bmodinv(7); # 5 * 3 == 1 (mod 7) => 3
241  
242 Returns the modular inverse of $x (mod $y), if defined. This currently
243 returns 0 if there is no inverse (but that may change in the future).
244 Behaviour is undefined when $y is 0.
245  
246 =head2 bsqrt
247  
248 $x = Math::GMP->new(6);
249 $x->bsqrt(); # int(sqrt(6)) => 2
250  
251 Returns the integer square root of its argument.
252  
253 =head2 legendre
254  
255 $x = Math::GMP->new(6);
256 $x->legendre(3);
257  
258 Returns the value of the Legendre symbol ($x/$y). The value is defined only
259 when $y is an odd prime; when the value is not defined, this currently
260 returns 0 (but that may change in the future).
261  
262 =head2 jacobi
263  
264 $x = Math::GMP->new(6);
265 $x->jacobi(3);
266  
267 Returns the value of the Jacobi symbol ($x/$y). The value is defined only
268 when $y is odd; when the value is not defined, this currently returns 0
269 (but that may change in the future).
270  
271 =head2 fibonacci
272  
273 $x = Math::GMP->fibonacci(16);
274  
275 Calculates the n'th number in the Fibonacci sequence.
276  
277 =head2 probab_prime
278  
279 $x = Math::GMP->new(7);
280 $x->probab_prime(10);
281  
282 Probabilistically determines if the number is a prime. Argument is the number
283 of checks to perform. Returns 0 if the number is definitely not a prime,
284 1 if it may be, and 2 if it definitely is a prime.
285  
286 =head1 BUGS
287  
288 As of version 1.0, Math::GMP is mostly compatible with the old
289 Math::BigInt version. It is not a full replacement for the rewritten
290 Math::BigInt versions, though. See the L<SEE ALSO section|SEE ALSO>
291 on how to achieve to use Math::GMP and retain full compatibility to
292 Math::BigInt.
293  
294 There are some slight incompatibilities, such as output of positive
295 numbers not being prefixed by a '+' sign. This is intentional.
296  
297 There are also some things missing, and not everything might work as
298 expected.
299  
300 =head1 SEE ALSO
301  
302 Math::BigInt has a new interface to use a different library than the
303 default pure Perl implementation. You can use, for instance, Math::GMP
304 with it:
305  
306 use Math::BigInt lib => 'GMP';
307  
308 If Math::GMP is not installed, it will fall back to its own Perl
309 implementation.
310  
311 See L<Math::BigInt> and L<Math::BigInt::GMP> or
312 L<Math::BigInt::Pari> or L<Math::BigInt::BitVect>.
313  
314 =head1 AUTHOR
315  
316 Chip Turner <chip@redhat.com>, based on the old Math::BigInt by Mark Biggar
317 and Ilya Zakharevich. Further extensive work provided by Tels
318 <tels@bloodgate.com>.
319  
320  
321 =cut