@@ -92,7 +92,7 @@ sub splitUnits {
9292 : $aUnit . ' (?:\s*[/* ]\s*' . $aUnit . ' )*' ;
9393 $unitPattern = $unitPattern . ' (?:\/' . $unitPattern . ' )*' if $parseMathQuill ;
9494 my $unitSpace = " ($aUnit ) +($aUnit )" ;
95- my ($num , $units ) = $string =~ m ! ^(.*?(?:[)}\] 0-9a-z]|\d\. ))\s *($unitPattern )\s *$ ! ;
95+ my ($num , $units ) = $string =~ m ! ^(.*?(?:[)}\] 0-9a-z]|\d\. ))? \s *($unitPattern )\s *$ ! ;
9696 if ($units ) {
9797 while ($units =~ s / $unitSpace/ $1 *$2 / ) { }
9898 $units =~ s / // g ;
@@ -238,20 +238,26 @@ sub unitsPreFilter {
238238 $ans -> {correct_value }{context }
239239 && $ans -> {correct_value }-> context-> flag(' useMathQuill' )
240240 && (!defined $ans -> {mathQuillOpts } || $ans -> {mathQuillOpts } !~ / ^\s *disabled\s *$ /i ));
241+ if (defined ($units ) && $units ne ' ' && $num eq ' ' ) {
242+ $self -> cmp_Error($ans , " Units must follow a number" );
243+ $ans -> {unit_error } = $ans -> {ans_message };
244+ $ans -> {student_ans } = ' ' ;
245+ return $ans ;
246+ }
241247 unless (defined ($num ) && defined ($units ) && $units ne ' ' ) {
242248 $self -> cmp_Error($ans , " Your answer doesn't look like " . lc ($self -> cmp_class));
243- $ans -> {error_flag } = ' UNITS_NONE ' ;
249+ $ans -> {unit_error } = $ans -> { ans_message } ;
244250 return $ans ;
245251 }
246252 if ($units =~ m ! /.*/! ) {
247253 $self -> cmp_Error($ans , " Your units can only contain one division" );
248- $ans -> {error_flag } = ' UNITS_DIVISION ' ;
254+ $ans -> {unit_error } = $ans -> { ans_message } ;
249255 return $ans ;
250256 }
251257 my $ref = { getUnits($units ) };
252258 if ($ref -> {ERROR }) {
253259 $self -> cmp_Error($ans , $ref -> {ERROR });
254- $ans -> {error_flag } = ' UNITS_BAD ' ;
260+ $ans -> {unit_error } = $ans -> { ans_message } ;
255261 return $ans ;
256262 }
257263 $ans -> {units } = $units ;
@@ -263,6 +269,14 @@ sub cmp_preprocess {
263269 my $self = shift ;
264270 my $ans = shift ;
265271
272+ if ($ans -> {unit_error }) {
273+ $ans -> {ans_message } = $ans -> {error_message } = $ans -> {unit_error };
274+ if ($ans -> {student_ans } eq ' ' ) {
275+ $ans -> {student_ans } = $ans -> {original_student_ans };
276+ $ans -> {preview_latex_string } = TeXunits($ans -> {student_ans });
277+ }
278+ return ;
279+ }
266280 my $units = $ans -> {units };
267281 return $ans unless $units ;
268282 $ans -> {student_ans } .= " " . $units ;
@@ -272,8 +286,8 @@ sub cmp_preprocess {
272286 if (!defined ($ans -> {student_value }) || $self -> checkStudentValue($ans -> {student_value })) {
273287 $ans -> {student_value } = undef ;
274288 $ans -> score(0);
275- $ans -> {error_flag } = ' UNITS_NO_NUMBER' ;
276289 $self -> cmp_Error($ans , " Units must follow a number" );
290+ $ans -> {unit_error } = $ans -> {ans_message };
277291 return ;
278292 }
279293
@@ -284,7 +298,7 @@ sub cmp_preprocess {
284298sub cmp_equal {
285299 my $self = shift ;
286300 my $ans = shift ;
287- if (!$ans -> {error_flag }) {
301+ if (!$ans -> {unit_error }) {
288302 my $meth = @{ ref ($self ) . ' ::ISA' }[-1] . ' ::cmp_equal' ;
289303 $meth = ' Value::cmp_equal' unless defined &$meth ;
290304 &$meth ($self , $ans , @_ );
@@ -298,7 +312,6 @@ sub cmp_postprocess {
298312 $self -> cmp_Error($ans , " The units for your answer are not correct" )
299313 unless $ans -> {correct_value }-> uPowers eq $ans -> {student_value }-> uPowers;
300314 }
301- $ans -> {error_flag } = undef if $ans -> {error_flag } =~ m / ^UNITS_/ ;
302315 return $ans ;
303316}
304317
@@ -350,7 +363,7 @@ sub makeValue {
350363 my $value = shift ;
351364 my %options = (context => $self -> context, @_ );
352365 my $num = Value::makeValue($value , %options );
353- return bless $num , ' Parser::Legacy::FormulaWithUnits' if $num -> classMatch(' Formula' );
366+ return bless $num , ' Parser::Legacy::FormulaWithUnits' if defined $num && $num -> classMatch(' Formula' );
354367 Value::Error(" A number with units must be a constant, not %s " , lc (Value::showClass($num )))
355368 unless Value::isReal($num );
356369 bless $num , $options {class };
0 commit comments