PREP 2015 Question Authoring - Archived

Units displayed in "Correct answer" with Number

Units displayed in "Correct answer" with Number

by tim Payer -
Number of replies: 3
Can the units (with numbers) for a correct answer be displayed with the numeric value under the "Correct Answer"?

For the code below the student will see a green field for the correct answer when she enters the correct number with units, but there is no corresponding display of these units attached to the correct numeric value under the correct answer header. Can these units be included with the correct answer also?

Thanks.

# Webwork Workshop 2015  for Payer, Homework 19, Practice:
# Exercises for Definite Integral applications: pg413, #61.

DOCUMENT();
loadMacros("PGstandard.pl",
           "MathObjects.pl",
           "parserNumberWithUnits.pl",
           "PGML.pl");

TEXT(beginproblem());

Context("Numeric");
$a =Real(random(0.5, 5,1));
$b =Real(random(0.1,0.6,0.1));
$c =Real(random(3,19,1));
$e = 0;
#$e =Real(random(0, 3,1));
$d =Real(random(4, 12,1));
$lo = Formula($b*$e**2+$c);
$up = Formula($b*$d**2+$c);
$ans =Formula("-$a/$b*(($up)**0.5-($lo)**0.5)");
$ans1 =Compute("$ans");
$ans2 =NumberWithUnits($ans1, "mg/cm^3");

BEGIN_PGML

The concentration of a drug in [`\frac{mg}{cm^3}`] within a patient's blood stream after an injection is decreasing at the rate of   [`A'(t)`]  where [`t`] is in hours. By how much does the concentration change over the first [`[$d]`] hours?  
*NOTE!*  Include the correct *units* within your answer.  
[``A'(t) = \frac{-[$a] t}{\sqrt{[$b]t^2+[$c]}} ``]
 
 [______________]{$ans2}
 

END_PGML

BEGIN_PGML_SOLUTION
*SOLUTION*

END_PGML_SOLUTION

ENDDOCUMENT();
In reply to tim Payer

Re: Units displayed in "Correct answer" with Number

by Douglas Young -
Webwork does a really good job of keeping up with units. You'll need to load the "parserNumberWithUnits.pl" macro. After calculating the correct answer, set up another variable with the unit. I've included a simple problem I created below that does this. The problem also includes a picture, which is called inside the [@ @], so you should ignore this part. A list of units that webwork recognizes can be found at: http://webwork.maa.org/wiki/Units#.VYx-TLxBXcA


##DESCRIPTION
#
##ENDDESCRIPTION

DOCUMENT(); # This should be the first executable line in the problem.

loadMacros(
"PGstandard.pl",
"PGunion.pl",
"MathObjects.pl",
"PGML.pl",
"PGcourse.pl",
"parserNumberWithUnits.pl"
);

TEXT(beginproblem);

##############################
#
# Setup
#

Context("Numeric")->flags->set(
tolerance => .01,
tolType => 'absolute',
);



$R1 = non_zero_random(1.0,10.0,0.1);
$R2 = non_zero_random(1.0,10.0,0.1);
$V = non_zero_random(20.0, 50.0, 0.1);

$RT = $R1 + $R2;
$RTans = NumberWithUnits($RT,'ohm');
$IT = $V/$RT;
$ITans = NumberWithUnits($IT,'amp');
$V1 = $IT*$R1;
$V1ans = NumberWithUnits($V1,'V');
$V2 = $IT*$R2;
$V2ans = NumberWithUnits($V2,'V');


##############################
#
# Main text
#

BEGIN_PGML
[@ image("2resistors-series.png", width => 200, height => 200 ) @]*

Two resistors, [` [$R1]~ \Omega `] and [` [$R2]~ \Omega `], are wired in series to a [$V] volt battery, as shown in the figure above. Find the total resistace of the circuit, the total current in the circuit, and the voltage drop across each resistor.

[` R_{Total} `] = [____________________]{$RTans}
[` I_{Total} `] = [____________________]{$ITans}
[` V_{1} `] = [____________________]{$V1ans}
[` V_{2} `] = [____________________]{$V2ans}
END_PGML

BEGIN_PGML_HINT
For series combo: [` R_{Total} = R_1 + R_2 , V_1 = I_T R_1 , V_2 = I_T R_2 , I_1 = I_2 = I_{battery} , I_T = V_{battery}/R_T `]
END_PGML_HINT

$showPartialCorrectAnswers = 1;

ENDDOCUMENT(); # This should be the last executable line in the problem.


In reply to Douglas Young

Re: Units displayed in "Correct answer" with Number

by tim Payer -
Okay the problem was in using the commands of "Formula" and or "Compute" in the calculation of values to arrive at an answer. I am assuming that when one is using "parserNumbersWithUnits.pl" all calculations to obtain the answer must be free of these commands.

now the units will display along with the correct value.
In reply to tim Payer

Re: Units displayed in "Correct answer" with Number

by Davide Cervone -
You should be able to do what you did originally and have it work, so this is a bug in the NumberWithUnits() macro. It turns out that when the number that you pass to NumberWithUnits() is already a MathObject, NumberWithUnits() will not create a new one but reuses that object and adds the unit information to it. But if the MathObject came from Compute(), it will have set the correct answer to what was passed to it, and NumberWithUnits() unfortunately doesn't change that, so the units are missing when the correct answer is used.


I've put an updated copy of parserNumberWithUnits.pl in our course's templates/macros folder as a temporary fix, but will need to do a more thorough one for the official release. in any case, your original file should show correct units now.