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 Math::Big‐
18 Int and for regular integer arithmetic. Unlike BigInt, though,
19 Math::GMP uses the GNU gmp library for all of its calculations, as
20 opposed to straight Perl functions. This can result in speed improve‐
21 ments.
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 pro‐
29 vide as seamless an interface as possible. However, if you need a per‐
30 fect 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
48 $x = Math::GMP->new(123);
49
50 Creates a new Math::GMP object from the passed string or scalar.
51
52 $x = Math::GMP->new('abcd', 36);
53
54 Creates a new Math::GMP object from the first parameter which should be
55 represented in the base specified by the second parameter.
56
57 bfac
58
59 $x = Math::GMP->new(5);
60 $x->bfac(); # 1*2*3*4*5 = 120
61
62 Calculates the factorial of $x and modifies $x to contain the result.
63
64 band
65
66 $x = Math::GMP->new(6);
67 $x->band(3); # 0b110 & 0b11 = 1
68
69 Calculates the bit-wise AND of it's two arguments and modifies the
70 first argument.
71
72 bxor
73
74 $x = Math::GMP->new(6);
75 $x->bxor(3); # 0b110 & 0b11 = 0b101
76
77 Calculates the bit-wise XOR of it's two arguments and modifies the
78 first argument.
79
80 bior
81
82 $x = Math::GMP->new(6);
83 $x->bior(3); # 0b110 & 0b11 = 0b111
84
85 Calculates the bit-wise OR of it's two arguments and modifies the first
86 argument.
87
88 bgcd
89
90 $x = Math::GMP->new(6);
91 $x->bgcd(4); # 6 / 2 = 2, 4 / 2 = 2 => 2
92
93 Calculates the Greatest Common Divisior of it's two arguments and
94 returnes the result.
95
96 legendre
97
98 jacobi
99
100 fibonacci
101
102 $x = Math::GMP->fibonacci(16);
103
104 Calculates the n'th number in the Fibonacci sequence.
105
107 As of version 1.0, Math::GMP is mostly compatible with the old
108 Math::BigInt version. It is not a full replacement for the rewritten
109 Math::BigInt versions, though. See the SEE ALSO section on how to
110 achieve to use Math::GMP and retain full compatibility to Math::BigInt.
111
112 There are some slight incompatibilities, such as output of positive
113 numbers not being prefixed by a '+' sign. This is intentional.
114
115 There are also some things missing, and not everything might work as
116 expected.
117
119 Math::BigInt has a new interface to use a different library than the
120 default pure Perl implementation. You can use, for instance, Math::GMP
121 with it:
122
123 use Math::BigInt lib => 'GMP';
124
125 If Math::GMP is not installed, it will fall back to it's own Perl
126 implementation.
127
128 See Math::BigInt and Math::BigInt::GMP or Math::BigInt::Pari or
129 Math::BigInt::BitVect.
130
132 Chip Turner <chip@redhat.com>, based on the old Math::BigInt by Mark
133 Biggar and Ilya Zakharevich. Further extensive work provided by Tels
134 <tels@bloodgate.com>.
135
136
137
138perl v5.8.8 2004-09-28 Math::GMP(3)