WeBWorK Main Forum

Decimal approximations not being accepted in Fraction context

Decimal approximations not being accepted in Fraction context

by Rick Lynch -
Number of replies: 3

Hi All! I am having some issues with the fraction context not allowing decimal approximations. For example, even if I set the absolute tolerance to be 0.1, it will not accept 0.38 for 3/8. However, it will accept the exact decimal, 0.375. Please see the code below and the attached screenshot.

Any idea why it won't take decimal approximations? Thanks!


DOCUMENT();      

loadMacros(
    "PGstandard.pl",
    "MathObjects.pl",
    "PGML.pl",
    "contextFraction.pl",
);

TEXT(beginproblem());

Context("Fraction");
Context()->flags->set(tolerance => 0.1, tolType => "absolute");

$ans = Fraction(3,8);

BEGIN_PGML
[`3/8 = `] [__]{$ans}
END_PGML
ENDDOCUMENT();

Attachment no_dec_approxs.PNG
In reply to Rick Lynch

Re: Decimal approximations not being accepted in Fraction context

by Alex Jordan -

Often if the problem author is putting the answer in Fraction context, they have in mind an exact fraction answer. In a very basic setting if I were asking a student to reduce 2/6, I'd want 1/3, not 0.333333.

But I'm not sure those tolerance settings mean anything in Fraction context. I glanced at the compare subroutine for fractions and it appears to me to use comparison of the perl values ratios, not of math objects. (I think this is as it should be, but that might be just my opinion.)

If you are OK with the answer being entered as a decimal, you could define the answer in Numeric context. If you also want it to be presented as a reduced fraction, you could do all the setup work in Fraction context, and then do something like:

Context("Numeric")->flags->set(reduceConstants=>0);
$ans = Formula("$ans");

In reply to Alex Jordan

Re: Decimal approximations not being accepted in Fraction context

by Rick Lynch -
I agree that most of the time we would want an exact answer. However, in a statistics class, for example, I might want to display a nice fraction number in the solution, but also allow a decimal, like 72/243 being approximately 0.296.

Thanks for an alternative, quick way to allow for both. I was doing it a different way without loading contextFraction.pl by calling new Fraction and overriding the displayed answer:

Context("Numeric");
$ans = Compute(3/8);
$dans = new Fraction(3,8);
$dans->reduce;

Context()->texStrings;
$dans = $dans->print();

BEGIN_PGML
[`3/8 = `] [__]{$ans->cmp(correct_ans_latex_string=>$dans)}
END_PGML
In reply to Rick Lynch

Re: Decimal approximations not being accepted in Fraction context

by Danny Glin -

If all you're looking for is the correct answer to be displayed as a fraction, then you can simply pass a string to Compute rather than a real number (which is the way Compute is supposed to be used):

Context("Numeric");
$ans = Compute("3/8");

BEGIN_PGML
[`3/8 = `] [__]{$ans}
END_PGML
Note that this won't reduce fractions.  In that case you will have to do something with Fraction objects.