WeBWorK Problems

Numberless unit answers

Numberless unit answers

by Alex Jordan -
Number of replies: 1
Is it possible to leverage the unit parser to get answers that are purely unit with no magnitude?

For example:
"If t is measured in seconds and f(t) is measured in meters, what units does f'(t) have?"

I'd like the answer to be "m/s". I'd like to use the unit parser so it will handle "m / s" and the like. I'd be OK accepting anything the unit parser feels is equivalent, like "km/ks", even though I don't see why a student would try that. I would *not* want to accept something dimensionally equivalent like "ft/s" that isn't fully equivalent.

I considered a custom checker that would take a student-submitted "m/s" and use it to create NumberWithUnits(1, "m/s") for comparing. But all kinds of things could go wrong if the student enters weird stuff. I'm wondering if this issue has been addressed before in some better way.
In reply to Alex Jordan

Re: Numberless unit answers

by Davide Cervone -
I think this will do what you want:

The units are: \{ANS_RULE(10)\}

ANS(Compute("m/s")->cmp(checker => sub {
  my ($correct,$student,$ans) = @_;
  $correct = $correct->value; # get perl string from String object
  $student = $student->value; # ditto
  my $context = Context(); Context("Numeric");
  my $check = NumberWithUnits("1 $correct")->cmp->evaluate("1 $student");
  $ans->{correct_ans_latex_string} = substr($check->{correct_ans_latex_string},3);
  if ($check->{score}) {
    $ans->{preview_latex_string} = substr($check->{preview_latex_string},3);
    $ans->{preview_text_string} = substr($check->{preview_text_string},3);
  return $check->{score};
This uses the ArbitraryString context so that there are no errors due to unknown units or such, and uses your idea of comparing "1 m/s", which is a good one. Note that you have to change contexts within the cuts checker in order to be able to use NumberWithUnits() (that is a pretty old macro file, and needs some updating). I've also added some code to copy the typeset versions of the student and correct answers so that they will display as typeset fractions and powers.

Hope that does what you need.