Log Message:
-----------
Updates to make mode=>'frac' properly emulate the original num_cmp
behavior (it now allows decimals and fractions instead of requiring a
fraction). There is also a new LimitedNumeric-StrictFraction context
(not currently used) that disallows decimals and requires fractions.
Modified Files:
--------------
pg/lib/Parser:
Number.pm
pg/lib/Parser/Legacy:
LimitedNumeric.pm
Revision Data
-------------
Index: Number.pm
===================================================================
RCS file: /webwork/cvs/system/pg/lib/Parser/Number.pm,v
retrieving revision 1.10
retrieving revision 1.11
diff -Llib/Parser/Number.pm -Llib/Parser/Number.pm -u -r1.10 -r1.11
--- lib/Parser/Number.pm
+++ lib/Parser/Number.pm
@@ -15,9 +15,10 @@
return $equation->{context}{parser}{Complex}->new($equation,$value,$ref)
if (ref($value) eq 'ARRAY');
$value = $value->value while Value::isReal($value);
- $value = $value + 0; # format the value as a number, just in case
$num = bless {
- value => $value, type => $Value::Type{number}, isConstant => 1,
+ value => $value + 0, # format the value as a number, just in case
+ value_string => $value, # for decimal checking, etc.
+ type => $Value::Type{number}, isConstant => 1,
ref => $ref, equation => $equation,
}, $class;
my $x = Value::Real->make($value);
@@ -79,7 +80,7 @@
sub _NoDecimals {
my $self = shift;
$self->Error("You are not allowed to type decimal numbers in this problem")
- unless $self->{value} =~ m/^[-+]?[0-9]+$/;
+ unless $self->{value_string} =~ m/^[-+]?[0-9]+$/;
}
Index: LimitedNumeric.pm
===================================================================
RCS file: /webwork/cvs/system/pg/lib/Parser/Legacy/LimitedNumeric.pm,v
retrieving revision 1.2
retrieving revision 1.3
diff -Llib/Parser/Legacy/LimitedNumeric.pm -Llib/Parser/Legacy/LimitedNumeric.pm -u -r1.2 -r1.3
--- lib/Parser/Legacy/LimitedNumeric.pm
+++ lib/Parser/Legacy/LimitedNumeric.pm
@@ -10,6 +10,12 @@
# Context("LimiteNumeric");
# Context("LimitedNumeric-Fraction");
#
+# There is also a third version, which is a strict fraction
+# mode (not in the original num_cmp) where ONLY fractions
+# can be entered (not decimals).
+#
+# Context("LimitedNumeric-StrictFraction");
+#
#
@@ -23,7 +29,7 @@
$self->SUPER::_check;
my $uop = $self->{def}{string} || $self->{uop};
$self->Error("You can only use '%s' with (non-negative) numbers",$uop)
- unless $self->{op}->class =~ /Number|DIVIDE/;
+ unless $self->{op}->class =~ /Number|INTEGER|DIVIDE/;
}
sub class {'MINUS'};
@@ -40,13 +46,25 @@
my $self = shift;
$self->SUPER::_check;
my $bop = $self->{def}{string} || $self->{bop};
- $self->Error("You can only use '%s' between (non-negative) numbers",$bop)
- unless $self->{lop}->class =~ /Number|MINUS/ &&
- $self->{rop}->class eq 'Number';
+ $self->Error("You can only use '%s' between (non-negative) integers",$bop)
+ unless $self->{lop}->class =~ /INTEGER|MINUS/ &&
+ $self->{rop}->class eq 'INTEGER';
}
sub class {'DIVIDE'};
+#
+# Distinguish integers from decimals
+#
+package Parser::Legacy::LimitedNumeric::Number;
+our @ISA = qw(Parser::Number);
+
+sub class {
+ my $self = shift;
+ return "INTEGER" if $self->{value_string} =~ m/^[-+]?[0-9]+$/;
+ return "Number";
+}
+
package Parser::Legacy::LimitedNumeric;
@@ -66,7 +84,7 @@
#
# For the Fraction versions, allow the modified division, and
-# make sure numbers are just integers
+# make sure numbers used in fractions are just integers
#
$context = $Parser::Context::Default::context{Numeric}->copy;
$Parser::Context::Default::context{'LimitedNumeric-Fraction'} = $context;
@@ -82,6 +100,13 @@
);
$context->parens->undefine('|','{','[');
$context->functions->disable('All');
+$context->{parser}{Number} = "Parser::Legacy::LimitedNumeric::Number";
+
+#
+# For strict fractions, don't allow decimal numbers
+#
+$context = $context->copy;
+$Parser::Context::Default::context{'LimitedNumeric-StrictFraction'} = $context;
Parser::Number::NoDecimals($context);
1;
|