WeBWorK Problems

Fractions in Intervals

Fractions in Intervals

by Chris Wingard -
Number of replies: 5

Is there a way to force fractions to appear in intervals?  The correct answer keeps showing as, e.g., (-inf, 0.5) U (4, inf)   but I need that to be 1/2 instead of 0.5.  Here's the relevant code:


Context("Interval");

$a = random(2,4,1);
$c = random(2,4,1);

...

Context("Fraction-NoDecimals");
$endpt1 = Fraction(1, $a);

Context("Interval");
$soln = Union("(-inf, $endpt1) U ($c, inf)");

...

ANS( $soln -> cmp() );

----------------

I've tried several variations on this as well, but no luck.  Any help would be appreciated.  Thanks!

In reply to Chris Wingard

Re: Fractions in Intervals

by Chris Wingard -

I was playing around with this a bit more and notice that if I enter my answer as (-inf, 3/2) U (4, inf) and submit, then the "Entered" column near the top says I entered  (-inf, 1.5) U (4, inf)  even if I have this line:

Context()->flags->set(formatStudentAnswer=>'parsed');

It looks like the Interval context automatically converts fractions to decimals.  Is it possible to sort of "customize" the Interval context to prevent it from doing that?  I will try to look into that this weekend but if anyone knows if that will be ineffective or if anyone has a better solution, I'd love to know.  Thanks!

In reply to Chris Wingard

Re: Fractions in Intervals

by Davide Cervone -
The formatStudentAnswer=>'parsed' idea should work (it does for me). Make sure you are setting it on the "Interval" context (the one in which the union is being created). If you are having trouble with that, perhaps you can post a code snippet so that we can check what is going on better.

Davide
In reply to Davide Cervone

Re: Fractions in Intervals

by Chris Wingard -

Thanks.  I toyed around with it some more and got it working in the "Entered" column.  The "Correct" column still has the decimal though.  If at all possible I'd like to get rid of that since we do encourage our students to use fractions rather than decimals (except for apps problems).  Here's the code as I currently have it:

## Author('Chris Wingard')
## Institution('Michigan State University')
## MTH 103
## Section: Polynomial and Rational Inequalities

DOCUMENT();
loadMacros(
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGanswermacros.pl",
"PGstandard.pl",
"MathObjects.pl",
"contextFraction.pl",
"contextLimitedNumeric.pl",
"extraAnswerEvaluators.pl",
#"parserMultiAnswer.pl",
"answerHints.pl"
);

Context("Interval")->flags->set(
  reduceConstants => 0,
  reduceConstantFunctions => 0,
  strict_numeric => 1,
  formatStudentAnswer=>'parsed'
);
Parser::Number::NoDecimals;

$showPartialCorrectAnswers = 1;

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

do {
  $a = random(2,4,1);
  do {
    $b = random(1,3,1);
  } while (gcd($a, $b) != 1);

  do {
    $c = random(1,5,1);
  } while ($b == $c);

  $A = $a;
  $B = -($a*$c + $b);
  $C = $b*$c;
} while ($B**2 <= 1);

$fx = "\dfrac{1}{\sqrt{$A x^2 + $B x + $C}}";

$endpt1 = "$b/$a";

$soln = Union("(-inf, $endpt1) U ($c, inf)");

$display = "f(x) = $fx";

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

TEXT(beginproblem());

Context()->texStrings;

BEGIN_TEXT
$PAR
Find the domain of the function.
$PAR
\( \qquad $display \)
$PAR
Answer:  \{ ans_rule(30) \}
$PAR
$BBOLD
Note: Use INF or -INF for \(\infty\) or \(-\infty\), respectively. 
Make sure your answer $BR
is in interval notation, e.g., an answer of \(2 \le x \le 6\)
should be entered as \([2, 6]\), etc. $BR
Enter "{ }" without quotes if there is no solution (the empty set). $BR
Use the letter U for union.  e.g., an answer of \(x < 2 \textrm{ or }
x \ge 6\)
$BR should be entered as \((\textrm{-inf}, 2) \textrm{ U }
[6, \textrm{inf})\)
, etc.
$EBOLD
END_TEXT

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

Context()->normalStrings;

ANS( $soln -> cmp() );
#ANS( interval_cmp($soln) );

ENDDOCUMENT();

In reply to Chris Wingard

Re: Fractions in Intervals

by Nathan Wodarz -
Change the line

$soln = Union("(-inf, $endpt1) U ($c, inf)");

to

$soln = Compute("(-inf, $endpt1) U ($c, inf)");

and you should get what you're looking for.

Nathan
In reply to Nathan Wodarz

Re: Fractions in Intervals

by Chris Wingard -

Works perfectly now.

I swear I tried >12 different combinations of contexts, answer evaluators, $soln declarations but obviously missed the right one!

Thanks guys!