1Math::GMP(3) User Contributed Perl Documentation Math::GMP(3)
2
3
4
6 Math::GMP - High speed arbitrary size integer math
7
9 use Math::GMP;
10 my $n = new Math::GMP 2;
11
12 $n = $n ** (256*1024);
13 $n = $n - 1;
14 print "n is now $n\n";
15
17 Math::GMP was designed to be a drop-in replacement both for
18 Math::BigInt and for regular integer arithmetic. Unlike BigInt,
19 though, Math::GMP uses the GNU gmp library for all of its calculations,
20 as opposed to straight Perl functions. This can result in speed
21 improvements.
22
23 The downside is that this module requires a C compiler to install -- a
24 small tradeoff in most cases. Also, this module is not 100% compatible
25 to Math::BigInt.
26
27 A Math::GMP object can be used just as a normal numeric scalar would be
28 -- the module overloads most of the normal arithmetic operators to
29 provide as seamless an interface as possible. However, if you need a
30 perfect interface, you can do the following:
31
32 use Math::GMP qw(:constant);
33
34 $n = 2 ** (256 * 1024);
35 print "n is $n\n";
36
37 This would fail without the ':constant' since Perl would use normal
38 doubles to compute the 250,000 bit number, and thereby overflow it into
39 meaninglessness (smaller exponents yield less accurate data due to
40 floating point rounding).
41
43 Although the non-overload interface is not complete, the following
44 functions do exist:
45
46 new
47 $x = Math::GMP->new(123);
48
49 Creates a new Math::GMP object from the passed string or scalar.
50
51 $x = Math::GMP->new('abcd', 36);
52
53 Creates a new Math::GMP object from the first parameter which should be
54 represented in the base specified by the second parameter.
55
56 bfac
57 $x = Math::GMP->new(5);
58 $x->bfac(); # 1*2*3*4*5 = 120
59
60 Calculates the factorial of $x and modifies $x to contain the result.
61
62 band
63 $x = Math::GMP->new(6);
64 $x->band(3); # 0b110 & 0b11 = 1
65
66 Calculates the bit-wise AND of it's two arguments and modifies the
67 first argument.
68
69 bxor
70 $x = Math::GMP->new(6);
71 $x->bxor(3); # 0b110 & 0b11 = 0b101
72
73 Calculates the bit-wise XOR of it's two arguments and modifies the
74 first argument.
75
76 bior
77 $x = Math::GMP->new(6);
78 $x->bior(3); # 0b110 & 0b11 = 0b111
79
80 Calculates the bit-wise OR of it's two arguments and modifies the first
81 argument.
82
83 bgcd
84 $x = Math::GMP->new(6);
85 $x->bgcd(4); # 6 / 2 = 2, 4 / 2 = 2 => 2
86
87 Calculates the Greatest Common Divisior of it's two arguments and
88 returns the result.
89
90 legendre
91 jacobi
92 fibonacci
93 $x = Math::GMP->fibonacci(16);
94
95 Calculates the n'th number in the Fibonacci sequence.
96
97 probab_prime
98 $x = Math::GMP->new(7);
99 $x->probab_prime(10);
100
101 Probabilistically Determines if the number is a prime. Argument is the
102 number of checks to perform. Returns 0 if the number is definitely not
103 a prime, 1 if it may be, and 2 if it is definitely is a prime.
104
106 As of version 1.0, Math::GMP is mostly compatible with the old
107 Math::BigInt version. It is not a full replacement for the rewritten
108 Math::BigInt versions, though. See the SEE ALSO section on how to
109 achieve to use Math::GMP and retain full compatibility to Math::BigInt.
110
111 There are some slight incompatibilities, such as output of positive
112 numbers not being prefixed by a '+' sign. This is intentional.
113
114 There are also some things missing, and not everything might work as
115 expected.
116
118 Math::BigInt has a new interface to use a different library than the
119 default pure Perl implementation. You can use, for instance, Math::GMP
120 with it:
121
122 use Math::BigInt lib => 'GMP';
123
124 If Math::GMP is not installed, it will fall back to it's own Perl
125 implementation.
126
127 See Math::BigInt and Math::BigInt::GMP or Math::BigInt::Pari or
128 Math::BigInt::BitVect.
129
131 Chip Turner <chip@redhat.com>, based on the old Math::BigInt by Mark
132 Biggar and Ilya Zakharevich. Further extensive work provided by Tels
133 <tels@bloodgate.com>.
134
135
136
137perl v5.12.2 2009-09-17 Math::GMP(3)