Skip to content

Commit 1ad4426

Browse files
committed
Fix several issues with the contextInteger.pl macro.
Several times in the file the variable `$self` is used where it is not defined. So if the `$self->Error` method is actually called it results in the error "can't call Error on an undefined value". Also the wrong counts are used in the `randomPrime` method resulting in unexpected behavior. Finally, if the `$end` parameter to the `_getPrimesInRange` is prime, then include it in the returned primes in the range. Thus, the `randomPrime` method will potentially be the `$end` parameter. I.e., `randomPrime(1, 11)` could return 11. This is to address the things discussed in #1296.
1 parent df2eb9d commit 1ad4426

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

macros/contexts/contextInteger.pl

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ =head2 isPrime
7474
7575
=cut
7676

77-
loadMacros('MathObjects.pl');
77+
loadMacros('PGbasicmacros.pl', 'MathObjects.pl');
7878

7979
sub _contextInteger_init { context::Integer::Init() }
8080

@@ -124,7 +124,7 @@ sub Init {
124124
sub _divisor {
125125
my $power = abs(shift);
126126
my $a = abs(shift);
127-
$self->Error("Cannot perform divisor function on Zero") if $a == 0;
127+
Value::Error('Cannot perform divisor function on Zero') if $a == 0;
128128
$result = 1;
129129
$sqrt_a = int(sqrt($a));
130130
for (my $i = 2; $i < $sqrt_a; $i++) {
@@ -146,17 +146,17 @@ sub _divisor {
146146
sub _getPrimesInRange {
147147
my $index = shift;
148148
my $end = shift;
149-
$self->Error("Start of range must be a positive number.") if $index < 0;
150-
$self->Error("End of range must be greater than or equal to 2") if $end < 2;
151-
$self->Error("Start or range must be before end of range") if $index > $end;
149+
Value::Error('Start of range must be a positive number.') if $index < 0;
150+
Value::Error('End of range must be greater than or equal to 2') if $end < 2;
151+
Value::Error('Start of range must be before end of range') if $index > $end;
152152
@primes = ();
153153

154154
# consider switching to set upper limit and static array of primes
155155

156156
push(@primes, 2) if $index <= 2;
157157
# ensure index is odd
158158
$index++ if $index % 2 == 0;
159-
while ($index < $end) {
159+
while ($index <= $end) {
160160
push(@primes, $index) if context::Integer::Function::Numeric::isPrime($index);
161161
$index += 2;
162162
}
@@ -172,8 +172,8 @@ package context::Integer::Function::Numeric;
172172
#
173173
sub primeFactorization {
174174
my $a = abs(shift);
175-
$self->Error("Cannot factor Zero into primes.") if $a == 0;
176-
$self->Error("Cannot factor One into primes.") if $a == 1;
175+
Value::Error('Cannot factor Zero into primes.') if $a == 0;
176+
Value::Error('Cannot factor One into primes.') if $a == 1;
177177

178178
my %factors;
179179
my $n = $a;
@@ -200,7 +200,7 @@ sub primeFactorization {
200200
#
201201
sub phi {
202202
my $a = abs(shift);
203-
$self->Error("Cannot phi on Zero.") if $a == 0;
203+
Value::Error('Cannot phi on Zero.') if $a == 0;
204204
$result = $a;
205205
$n = $a;
206206
for (my $i = 2; ($i**2) <= $n; $i++) {
@@ -235,9 +235,8 @@ sub isPrime {
235235
sub randomPrime {
236236
my ($start, $end) = @_;
237237
my @primes = context::Integer::_getPrimesInRange($start, $end);
238-
$self->Error("Could not find any prime numbers in range.") if $#primes == 0;
239-
my $primeIndex = $main::PG_random_generator->random(0, ($#primes - 1), 1);
240-
return $primes[$primeIndex];
238+
Value::Error('Could not find any prime numbers in range.') unless @primes;
239+
return main::list_random(@primes);
241240
}
242241

243242
package context::Integer::Function::Numeric2;

0 commit comments

Comments
 (0)