blib/lib/Math/GMP_OLOAD.pm

filter: Statementstmt 100% 10 / 10 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 93.3% 14 / 15 CC 2 SCAR 4.1
CC
2
Cov
92%
BEGIN
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.5
↑ Summary
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 {
4 package Math::GMP_OLOAD;
5 BEGIN {
6 4 ▶ eval{ require Math::GMP;};
Subroutine BEGIN: called CC=2 SCAR=7.0
7 4 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 4 0 4
8 4 die "Math::GMP_OLOAD failed to load Math::GMP:\n$@";
9 }
10 }
11  
12 $Math::GMP_OLOAD::VERSION = '0.02';
13 }
14  
15 {
16 package Math::GMP;
17  
18 use overload "+" => sub ($$$) {
19 my($x, $y) = (shift, shift);
20 if(ref($y) eq 'Math::MPFR' || ref($y) eq 'Math::GMPq' || ref($y) eq 'Math::GMPz') {
21 return $y + $x;
22 }
23  
24 return Math::GMP::op_add($x, $y, 0);
25 };
26  
27 use overload "-" => sub ($$$) {
28 my($x, $y) = (shift, shift);
29 if(ref($y) eq 'Math::MPFR' || ref($y) eq 'Math::GMPq' || ref($y) eq 'Math::GMPz') {
30 return -($y - $x);
31 }
32  
33 return Math::GMP::op_sub($x, $y, shift);
34 };
35  
36 use overload "*" => sub ($$$) {
37 my($x, $y) = (shift, shift);
38 if(ref($y) eq 'Math::MPFR' || ref($y) eq 'Math::GMPq' || ref($y) eq 'Math::GMPz') {
39 return $y * $x;
40 }
41  
42 return Math::GMP::op_mul($x, $y, 0);
43 };
44  
45 use overload "/" => sub ($$$) {
46 my($x, $y) = (shift, shift);
47 if(ref($y) eq 'Math::MPFR') {
48 return Math::MPFR->new($x) / $y;
49 }
50 if(ref($y) eq 'Math::GMPq') {
51 return Math::GMPq->new($x) / $y;
52 }
53 if(ref($y) eq 'Math::GMPz') {
54 return Math::GMPz->new($x) / $y;
55 }
56  
57 return Math::GMP::op_div($x, $y, shift);
58 };
59  
60 use overload "**" => sub {
61 my($x, $y, $s) = (shift, shift, shift);
62 if(ref($y) eq 'Math::MPFR') {
63 # We've called GMP ** MPFR ($x ** $y)
64 return Math::MPFR::overload_pow($y, $x, 1);
65 }
66 if(ref($y) eq 'Math::GMPq') {
67 # We've called GMP ** GMPq ($x ** $y)
68 return Math::GMPq::overload_pow($y, $x, 1);
69 }
70 if(ref($y) eq 'Math::GMPz') {
71 # We've called GMP ** GMPz ($x ** $y)
72 return Math::GMPz::overload_pow(Math::GMPz->new($x), $y, 0);
73 }
74  
75 # We get to here because we've called either:
76 # OTHER ** GMP ($y ** $x), and $s is true
77 # or
78 # GMP ** OTHER ($x ** $y), and $s is false
79 $s ? op_pow($y, $x) : op_pow($x, $y);
80 };
81  
82 # use overload "**=" => sub {
83 # my($x, $y, $s) = (shift, shift, shift);
84 # if(ref($y) eq 'Math::MPFR') {
85 # # We've called GMP ** MPFR ($x ** $y)
86 # return Math::MPFR::overload_pow($y, $x, 1);
87 # }
88 # if(ref($y) eq 'Math::GMPq') {
89 # # We've called GMP ** GMPq ($x ** $y)
90 # return Math::GMPq::overload_pow($y, $x, 1);
91 # }
92 # if(ref($y) eq 'Math::GMPz') {
93 # # We've called GMP ** GMPz ($x ** $y)
94 # return Math::GMPz::overload_pow(Math::GMPz->new($x), $y, 0);
95 # }
96 #
97 # # We get to here because we've called either:
98 # # OTHER ** GMP ($y ** $x), and $s is true
99 # # or
100 # # GMP ** OTHER ($x ** $y), and $s is false
101 # $s ? op_pow($y, $x) : op_pow($x, $y);
102 # };
103  
104  
105 use overload "<" => sub {
106 my($x, $y) = (shift, shift);
107 if(ref($y) eq 'Math::MPFR' || ref($y) eq 'Math::GMPq' || ref($y) eq 'Math::GMPz') {
108 return 1 if $y > $x;
109 return 0;
110 }
111  
112 my $cmp = Math::GMP::op_spaceship($x, $y, shift);
113 return 1 if $cmp < 0;
114 return 0;
115 };
116  
117 use overload ">" => sub {
118 my($x, $y) = (shift, shift);
119 if(ref($y) eq 'Math::MPFR' || ref($y) eq 'Math::GMPq' || ref($y) eq 'Math::GMPz') {
120 return 1 if $y < $x;
121 return 0;
122 }
123  
124 my $cmp = Math::GMP::op_spaceship($x, $y, shift);
125 return 1 if $cmp > 0;
126 return 0;
127 };
128  
129 use overload "<=" => sub {
130 my($x, $y) = (shift, shift);
131 if(ref($y) eq 'Math::MPFR' || ref($y) eq 'Math::GMPq' || ref($y) eq 'Math::GMPz') {
132 return 1 if $y >= $x;
133 return 0;
134 }
135  
136 my $cmp = Math::GMP::op_spaceship($x, $y, shift);
137 return 1 if $cmp <= 0;
138 return 0;
139 };
140  
141 use overload ">=" => sub {
142 my($x, $y) = (shift, shift);
143 if(ref($y) eq 'Math::MPFR' || ref($y) eq 'Math::GMPq' || ref($y) eq 'Math::GMPz') {
144 return 1 if $y <= $x;
145 return 0;
146 }
147  
148 my $cmp = Math::GMP::op_spaceship($x, $y, shift);
149 return 1 if $cmp >= 0;
150 return 0;
151 };
152  
153 use overload "==" => sub ($$$) {
154 my($x, $y) = (shift, shift);
155 if(ref($y) eq 'Math::MPFR' || ref($y) eq 'Math::GMPq' || ref($y) eq 'Math::GMPz') {
156 return 1 if $y == $x;
157 return 0;
158 }
159  
160 my $cmp = Math::GMP::op_spaceship($x, $y, 0);
161 return 1 if $cmp == 0;
162 return 0;
163 };
164  
165 use overload "!=" => sub ($$$) {
166 my($x, $y) = (shift, shift);
167 if(ref($y) eq 'Math::MPFR' || ref($y) eq 'Math::GMPq' || ref($y) eq 'Math::GMPz') {
168 return 1 if $y != $x;
169 return 0;
170 }
171  
172 my $cmp = Math::GMP::op_spaceship($x, $y, 0);
173 return 1 if $cmp != 0;
174 return 0;
175 };
176  
177 use overload "<=>" => sub ($$$) {
178 my($x, $y) = (shift, shift);
179 if(ref($y) eq 'Math::MPFR' || ref($y) eq 'Math::GMPq' || ref($y) eq 'Math::GMPz') {
180 return ($y <=> $x) * -1;
181 }
182  
183 my $cmp = Math::GMP::op_spaceship($x, $y, shift);
184 return $cmp;
185 };
186 }
187  
188 1;
↑ Summary