WeBWorK Problems

Requiring an integer answer

Requiring an integer answer

by Marc Renault -
Number of replies: 5
Hello!  I'm a new user and I'm trying to write questions using PGML.  I'm writing some very simple questions for developmental math, and it seems difficult to require the student to enter only an integer for an answer.  I got it to work, but it seems strange.  Here's the code

Context("Numeric");

$a = random(1,5);
$b = random(1,5);
$Q = "$a + $b";
$A = Compute($Q);

Context("LimitedNumeric");    # Is there an easier way
$A = Compute($A);             # to do this??

BEGIN_PGML
Evaluate [`[$Q]`]. [____________]{$A}

(By the way, the answer is [:[$A]:].)
END_PGML

Is there a more straighforward way to require that the student enter an integer?  If I set the context to "LimitedNumeric" at the very beginning, I get an error on the line $A = Compute($Q).

Thanks for any help,
Marc

In reply to Marc Renault

Re: Requiring an integer answer

by Paul Pearson -
Hi Marc,

I would like to encourage you (and everyone else) to post entire pg files (not just code snippets) along with webwork version information, which can be found at the bottom of every webwork page and looks like this:


Page generated at 06/06/2014 at 11:02am EDT

The first time you define $A, it is in the Numeric context.  When $A next appeared, you tried to coerce $A into the LimitedNumeric context.  I have a hunch that you should have used only the LimitedNumeric context and never used the Numeric context.  Further, you should have done your arithmetic using Perl objects and then promoted a Perl object to a MathObject only when necessary.  

The scalars $a and $b are Perl objects (not MathObjects) and so they can be added together to get another Perl object.  When $a and $b are added to give a Perl object, the LimitedNumeric context should not complain, since it only concerns itself with MathObjects.

Try:

### begin code ############################

Context("LimitedNumeric");

$a = random(1,5,1);
$b = random(1,5,1);

# Pre-compute the answer $c as a Perl object.
# Addition is allowed since $a and $b are Perl objects, not MathObjects.
$c = $a + $b; # so $c is a Perl object, not a MathObject

# Define a MathObject $C with the same value as the Perl object $c.
$C = Compute($c); # $C is a MathObject in the LimitedNumeric context

# The rest of your code probably works just fine as long as you use
# $C as the answer. 

### end code ##############################

Best regards,

Paul Pearson
In reply to Paul Pearson

Re: Requiring an integer answer

by Marc Renault -
Thank you, Paul.  I see now that doing the arithmetic in Perl is useful.  I think I was under the impression that I should use MathObjects for all my computations.

Thanks!
Marc
In reply to Marc Renault

Re: Requiring an integer answer

by Alex Jordan -
Paul has given a good reply, but just for completeness, the reason that you got an error is that defined $Q to be the string "1 + 2" (for example) rather than the value of 1 + 2. In LimitedNumeric context, addition and other operators are not permitted. So the step with Compute($Q) causes an error because it is actually something like Compute("1 + 2"). The parser in LimitedNumeric says that it won't deal with this because of the plus sign being passed.

As an exception to the rule to do most calculations in Perl and use MathObjects at the end, if you use the Fraction context I might recommend using a MathObject earlier. This way things like Fraction(1,2) + 1/3 will nicely work out to Fraction(5,6) without you manually having to calculate least common denominators, etc.

Lastly, if it is clear to the student that the answer should be an integer, then you might want to read some of this thread (http://webwork.maa.org/moodle/mod/forum/discuss.php?d=3283). Specifically you can find a couple of posts in there from me about it. You'd want the tolType to be absolute, the tolerance to be something less than 1, and you may also want to set the cmp_class to say "an integer".
In reply to Alex Jordan

Re: Requiring an integer answer

by Marc Renault -
Ah! I understand now.  In LimitedNumeric I can first cast the integers $a and $b individually with Compute() then assign $ans = $a + $b.  Beautiful.  

I tried a fraction example too, and that worked out well.  It is very nice that the Fraction context does the reducing for you.

Marc
In reply to Marc Renault

Re: Requiring an integer answer

by Davide Cervone -
Note that LimitedNumeric does not require that the student enter an integer. For example, if $a = 3 and $b = 4, then an answer of 7.000001 is marked correct. If you want to force integers, then you might add
    Parser:Number::NoDecimals();
right after the line where you specify the LimitedNumeric context.

Davide