Skip to content

Fraction eval method should return Real MathObject not perl real #1208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/PGalias.pm
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ sub new {
# caller to pass in a problemUUID that will provide the required uniqueness. That could include a course name, a
# student login name, etc.
$self->{unique_id_stub} = create_uuid_as_string(UUID_V3, UUID_NS_URL,
join('-', $envir->{psvn} // (), $envir->{problemSeed}, $envir->{problemUUID} // ()));
join('-', $envir->{psvn} // (), $envir->{problemSeed} // (), $envir->{problemUUID} // ()));

# Check the parameters.
$self->warning_message('The displayMode is not defined') unless $self->{displayMode};
Expand Down
44 changes: 38 additions & 6 deletions macros/contexts/contextFraction.pl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ =head1 DESCRIPTION
permissible to enter a whole number WITH a fraction, as in C<2 1/2> for
"two and one half", or C<5/2>.

The fourth is the same as LimiteFraction, but students must enter proper
The fourth is the same as LimitedFraction, but students must enter proper
fractions, and results are shown as proper fractions.

It is also possible to add fractions to an existing context using
Expand Down Expand Up @@ -205,6 +205,20 @@ =head1 DESCRIPTION
default in all four contexts. You should not set both
C<requirePureFractions> and C<requireProperFractions> to 1.

=item S<C<< fractionTolerance >>>

Thie determines the tolerance to use when comparing a fraction to a
real number. The fraction will be converted to a real, and then this
is used as the tolerance in a relative-tolerance comparison of the two
reals. The default is 1E-10, meaning the decimal must match to
roughly 10 digits.

=item S<C<< contFracMaxDen >>>

This is the largest denominator to allow for continued-fraction
computations when trying to produce a fraction from a real number.
The default is 10**8.

=back

Fraction objects have two methods that can be useful when
Expand Down Expand Up @@ -326,6 +340,7 @@ sub extending {
requireProperFractions => $options{requireProperFractions} || 0,
requirePureFractions => $options{requirePureFractions} || 0,
showMixedNumbers => $options{showMixedNumbers} || 0,
fractionTolerance => $options{fractionTolerance} || 10**-10,
contFracMaxDen => $options{contFracMaxDen} // 10**8,
},
reductions => { 'a/b' => 1, 'a b/c' => 1, '0 a/b' => 1 },
Expand Down Expand Up @@ -454,8 +469,8 @@ sub toFraction {
if ($x == 0) {
($a, $b) = (0, 1);
} else {
my $sign = $x / abs($x);
($a, $b) = $self->continuedFraction(abs($x), $max);
my $sign = $x / CORE::abs($x);
($a, $b) = $self->continuedFraction(CORE::abs($x), $max);
$a = $sign * $a;
}
my $Real = $self->Package("Real");
Expand Down Expand Up @@ -909,7 +924,7 @@ sub formula {
sub eval {
my $self = shift;
my ($a, $b) = $self->value;
return $a / $b;
return $self->Package('Real')->new($self->context, $a / $b);
}

#
Expand Down Expand Up @@ -991,8 +1006,25 @@ sub power {
}

sub compare {
my ($self, $l, $r) = Value::checkOpOrderWithPromote(@_);
return $l->eval <=> $r->eval;
my ($self, $other, $flag) = @_;
$other = Value::makeValue($other) unless Value::isValue($other);
if ($other->classMatch('Fraction')) {
my ($s, $l, $r) = Value::checkOpOrder($self, $other, $flag);
my ($a, $b) = $l->value;
my ($c, $d) = $r->value;
return ($a * $d) <=> ($c * $b);
}
if ($other->classMatch('Real')) {
my $tolerance = $self->getFlag('fractionTolerance', 10**-10);
my $self = $self->eval->with(tolerance => $tolerance, tolType => 'relative');
$other = $other->value;
my ($s, $l, $r) = Value::checkOpOrder($self, $other, $flag);
return $l <=> $r;
}
if ($other->classMatch("Infinity")) {
return $other->{isNegative} ? 1 : -1;
}
$self->Error("You can't compare %s to %s", $self->showClass, $other->showClass);
}

##################################################
Expand Down