## WeBWorK Problems

### Accepting reduced fraction OR decimal

by Brittni Lorton -
Number of replies: 3

My team and I are attempting to create some problems where WW would accept a reduced fraction or its equivalent decimal but NOT accept an unsimplified fraction.

I have attempted some research on this and maybe I need bizarroArithmetic.pl? Maybe I need a custom answer checker? I'm not really sure so any advice would be greatly appreciated.

Basically, I would like to be able to accept an answer of 1/2 or 0.5, but NOT an answer of 5/10.

Also, it would be nice to be able to accept an answer of 1/13 or 0.76923 (or some other rounding of the decimal, which would be specified in the directions to the student as well) but NOT accept 4/52.

Thoughts?

### Re: Accepting reduced fraction OR decimal

by Alex Jordan -

Probably contextFraction.pl is what you want:

https://github.com/openwebwork/pg/blob/master/macros/contextFraction.pl

I haven't thought it all through, but maybe you would get what you want with Context("Fraction") and using studentsMustReduceFractions=>1.

### Re: Accepting reduced fraction OR decimal

by Brittni Lorton -
Hi Alex,

For some reason, that also doesn't seem to be working. Below is a sample code I tested using studentsMustReduceFractions=>1 and students are able to enter fractions but they aren't required to reduce them.
I also tried with LimitedFraction in this same problem and then the issue is that students can't type the exact decimal. I'd love a way to accept either 1/2 or 0.5 but NOT 3/6 or other nonreduced fractions.

###########################
# Initialization

DOCUMENT();

"PGstandard.pl",
"MathObjects.pl",
"parserAssignment.pl",
"PGML.pl",
"PGcourse.pl",
"contextFraction.pl",
);

TEXT(beginproblem());

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

Context("Fraction");
Context()->flags->set(studentsMustReduceFractions=>1);
$ace = Fraction("4/52");$red = Fraction("1/2");
$club = Fraction("13/52");$face = Fraction("12/52");

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

BEGIN_PGML

One card is randomly selected from a deck. Find the following probabilities. Enter your answer as a reduced fraction.

+ P(an ace) = [_]{$ace} + P(a red card) = [_]{$red}

+ P(a club) = [_]{$club} + P(a face card) = [_]{$face}

END_PGML

#################################

### Re: Accepting reduced fraction OR decimal

by Brittni Lorton -
I believe I spoke too soon. I have it working now with the below code. I believe I just had the studentMustRecuceFracionts=>1 in the wrong location of the code. Thanks!

###########################
# Initialization

DOCUMENT();

"PGstandard.pl",
"MathObjects.pl",
"parserAssignment.pl",
"PGML.pl",
"PGcourse.pl",
"contextFraction.pl",
);

TEXT(beginproblem());

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

Context("Fraction");
$ace = Fraction("1/13")->cmp(studentsMustReduceFractions=>1);$red = Fraction("1/2")->cmp(studentsMustReduceFractions=>1);
$club = Fraction("1/4")->cmp(studentsMustReduceFractions=>1);$face = Fraction("3/13")->cmp(studentsMustReduceFractions=>1);

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

BEGIN_PGML

One card is randomly selected from a deck. Find the following probabilities. Enter your answer as a reduced fraction.

+ P(an ace) = [_]{$ace} + P(a red card) = [_]{$red}

+ P(a club) = [_]{$club} + P(a face card) = [_]{$face}

END_PGML

#################################