## WeBWorK Problems

### fractions as endpoints of intervals ### fractions as endpoints of intervals

by Richard Lynch -
Number of replies: 11
Hi Everyone,

I'm currently programming a domain problem for college algebra in which the answer has a fraction as an endpoint. In particular, the answer is

(-inf,0)U(0,1/$b)U(1/$b,inf)

where $b is a positive integer. I am wondering two things: 1. Is it possible to have the correct answer display the fraction rather than a decimal? I know this is not usually a big deal, but for college algebra we would rather have the correct answer display with the fractions when they print out the homework set. Plus, it would help in displaying the interval in the solution since I don't want the decimals to display (I wouldn't have to create a new variable$ddomain for the tex of it).
2. Even further, is it possible to force reduced fractions for the endpoints of the interval? Looking at the way that the interval context is set up, I don't see an obvious way of doing this, but maybe someone knows? I know this is probably related in someway with my previous post of fractions in lists.
I would greatly appreciate any input on these two questions!

Thanks!!
Rick ### Re: fractions as endpoints of intervals

by Davide Cervone -
In general, it is not easy to merge two different contexts (as they may each override something in common, for example). But in this case, it is not hard to allow intervals in the Fraction contexts, so you could do the following:
    loadMacros("contextFraction.pl");
Context("Fraction-NoDecimals");
Context()->parens->set(
'(' => {type => 'Interval'},
'[' => {type => 'Interval'},
'{' => {type => 'Set', removable => 0, emptyOK => 1},
);
Context()->constants->set(R => {TeX => '{\bf R}'});

to allow intervals and fractions in the same context. Now if you create an answer using fractions, it will display as fractions, and since decimals aren't allowed, students will have to enter the endpoints as fractions.

I suspect your next question will be about forcing reduced fractions. That will have to be handled similarly to how it was done with lists in your previous question.

Hope that helps.

Davide ### Re: fractions as endpoints of intervals

by Davide Cervone -
It turns out that there is a small bug in the fraction context that causes problems when the intervals include infinity or -infinity. I have a fixed version that is available from GitHub if you want it now. You can put this file in your pg/macros directory if you administer WeBWorK, or in your course's templates/macros directory if not. ### Re: fractions as endpoints of intervals

by Richard Lynch -
Hi Davide, thanks for your time! I am just now getting around to trying this out. I placed your updated fraction context file into the macros folder and then added the code to a problem. However, when I try to put a fraction as an endpoint of an interval, I get the error:
Can't convert a Fraction to an Infinity at line 36 of (eval 16602)
Died within main::Interval called at line 36 of (eval 16602)
Here is the code I'm trying to use:

###########################################################################
# initialization
###########################################################################
DOCUMENT();
"MathObjects.pl",
"PGstandard.pl",
"contextFraction.pl",
);

TEXT(beginproblem());
$showPartialCorrectAnswers = 1; ########################################################################### # setup contexts and variables ########################################################################### Context("Fraction-NoDecimals"); Context()->parens->set( '(' => {type => 'Interval'}, '[' => {type => 'Interval'}, '{' => {type => 'Set', removable => 0, emptyOK => 1}, ); Context()->constants->add(R => Interval("(-infinity,infinity)")); Context()->constants->set(R => {TeX => '{\bf R}'});$var = list_random('x','y','z','s','t','u','v','w');
$f = list_random('f','F','q','Q','A','a','B','b','g','G'); Context()->variables->are($var=>"Real", uc($var)=>"Real", lc($f)=>"Real", uc($f)=>"Real");$a = random(1,15);
$b = random(2,9);$b2 = ($b)**2;$pmcase = random(0,1);
$pm = ($pmcase == 0) ? "+" : "-";
$endpt = Fraction(1,$b);
$domain = ($pmcase == 0) ? Interval("(-inf,-$endpt)U(-$endpt,0)U(0,inf)") : Interval("(-inf,0)U(0,$endpt)U($endpt,inf)");
$dispf = "\displaystyle$f($var) = \frac{$a}{$b$pm \frac{1}{$var}}"; ########################################################################### # state the problem ########################################################################### Context()->texStrings; BEGIN_TEXT Find the domain of the function$PAR$dispf$$PAR and write your answer using interval notation.$PAR
Domain: \{ans_rule(30)\}
END_TEXT
Context()->normalStrings;

###########################################################################
###########################################################################
ANS($domain->cmp()); ENDDOCUMENT(); In reply to Richard Lynch ### Re: fractions as endpoints of intervals by Davide Cervone - OK, if you add  Context()->{precedence}{Fraction} = 2.5;  after  Context()->constants->set(R => {TeX => '{\bf R}'});  that should take care of it. I thought I had handled that in a different way, but apparently it didn't cover all the situations. Davide In reply to Davide Cervone ### Re: fractions as endpoints of intervals by Richard Lynch - That did the trick! Works beautifully. I wish I had asked sooner because this would have saved me a ton of time of time when it comes to displaying the answers to the students. It still will save me a ton, so I greatly appreciate it. I'm going to try to force reduced answers now, so I may post again if I can't figure it out. Thanks!! In reply to Davide Cervone ### Re: fractions as endpoints of intervals by Richard Lynch - Hi Davide, I just attempted to reduce fractions and I failed. I realized that if the answer is a union, the answer checker iterates through the intervals checking one by one (since order doesn't matter). So the entry_type is not even a fraction anymore. In fact setting this, the error when a student enters one interval instead of a union is: "There should be more fractions in your union" which doesn't make any sense. I'm not sure how save the fact that I checked the endpoints. I can access the endpoints using @{$student->data}, but that didn't help me much. I know I'm giving up rather quickly, but I really don't have a clue! I just don't understand the backings of WW well enough yet. If you could shed some light, that'd be great. Thanks in advance!! ### Re: fractions as endpoints of intervals

by Davide Cervone -
I suppose one question is what type of feedback (if any) are you wanting? If one fraction is not reduced, do you want to just say "your fractions must be reduced" and mark it wrong, or do you want to indicate which one is not reduced (or give a message for each unreduced fraction)? And do you want this message only if the fraction is otherwise correct? The latter is harder, as we found in the list checker from before.

Here, you are right that the checker will be for intervals, and you would need to look at the two endpoints to see if they are fractions, and if so, check if they are reduced or not. ### Re: fractions as endpoints of intervals

by Richard Lynch -
Hi Davide, I would like to be as specific as possible. So I'd like to say in which interval a fraction is not reduced. Something like "The nth interval of your answer has an unreduced fraction". It doesn't have to say which endpoint, but if it wouldn't be much extra effort, then I would prefer it to, such as "The left endpoint of the nth interval is unreduced". If it is too much, don't worry about it. And yes, I do want the message to only display when the answer is correct and just not reduced. If the answer is incorrect, I just want to say that they're wrong regardless of it being reduced or not. Thanks! ### Re: fractions as endpoints of intervals

by Davide Cervone -
Sorry, I already had my answer posted before I got your response (there is a half-hour delay between writing and it getting mailed out). So I did the "your nth interval has an unreduced fraction" rather than "the left endpoint of the nth interval is unreduced". It would be possible to do as you suggest, but I doubt it is really worth it. If both endpoints aren't reduced, you would either need two messages, or a combined message, which is more complicated. It would also require something like "The mth number in your nth set is not reduced" if there are sets rather than intervals involved. And if there are several unreduced fractions in a set, that could get pretty complicated. So I would stick with the current wording unless you really need the more sophisticated version. ### Re: fractions as endpoints of intervals

by Richard Lynch -
Hi Davide, I agree with you. It isn't really worth it. The version you gave works well!! I'll just stick with it. Thanks again! ### Re: fractions as endpoints of intervals

by Davide Cervone -
Here is a checker that does the trick:
    Context()->flags->set(reduceFractions=>0);

ANS($domain->cmp( checker => sub { my ($correct, $student,$ans, $nth,$value) = @_;
$student->context->setError("Your$nth $value contains an unreduced fraction","",undef,undef,$Value::CMP_WARNING)
if $student->{notReduced}; return 0 unless$correct == $student; my$reduced = 1;
foreach $x ($student->value) {
if (Value::classMatch($x,'Fraction') && !$x->isReduced) {
$reduced = 0; break; } }$student->{notReduced} = 1 unless $reduced ||$ans->{isPreview};
return \$reduced;
}
));

It gives a warning about unreduced fractions only if they are otherwise correct. This uses the same technique as I mentioned on another thread to mark the interval or set as not reduced when it is first checked, and then provides the message during the final pass when syntax errors for incorrect answers are collected.

This code works for both intervals and sets (it loops through all the entries in a set or the endpoints of an interval) and checks if they are unreduced fractions. It is important to handle sets as well as intervals, since the answer (-inf,1/2)U(1/2,inf) could be entered by the student as R-{1/2} and you want to check the 1/2 so that it is reduced.

Note that you need to set the reduceFractions flag to zero in order to prevent fractions from being reduced automatically.

Hope that does what you are looking for.

Davide