WeBWorK Problems

Making A Currency object with a PiecewiseFunction object

Making A Currency object with a PiecewiseFunction object

by Robin Cruz -
Number of replies: 2

I am trying to use a Piecewise function to calculate values and then convert these values to Currency objects-- so far, I'm not getting it to work.  When the answer is submitted it is scored as incorrect and the message is displayed: "Unexpected charactoer '$' " The code I've gotten so far is listed below.

Thanks -- rac

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

DOCUMENT();        # This should be the first executable line in the problem.

loadMacros(
  "PGstandard.pl",
  "MathObjects.pl",
  "contextCurrency.pl",
  "contextPiecewiseFunction.pl"
);

TEXT(&beginproblem);

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

Context("PiecewiseFunction");

$x1 = random(5000,7000,1000);
$x2 = random(8000,12000,1000);

$m1 = random(.02,.04,.01);
$m2 = $m1 + random(.02,.04,.01);
$b2 = $m1*$x1;
$m3 = $m2 + random(.01,.02,.01);
$b3 = $b2+$m2*($x2-$x1);

$T = Formula("$m1 x if x <= $x1
              else $b2 + $m2 (x -$x1) if $x1 < x <= $x2
              else $b3 + $m3 (x -$x2) if x > $x2");

Context("Numeric");

$input = random(4000,15000,10);

$tax = $T->eval(x=>$input)->inContext(Context());

Context("Currency");

$income = Currency($input);

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

BEGIN_TEXT
Suppose the state income tax for a single person in 2005 was
determined by the rule
 \[ T(x) = \{$T->TeX\} \]
where \(x\) is the taxable income for a single person.  Find the tax due on the income:
$PAR

For $income the tax = \{ans_rule(15)\}

END_TEXT

######################################
#  Answers

ANS(Currency($tax)->cmp);

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

ENDDOCUMENT();

In reply to Robin Cruz

Re: Making A Currency object with a PiecewiseFunction object

by Davide Cervone -
You are doing some very sophisticated coding. Nice work. The program is actually correct; it turns out that there was a but in the Currency context that would allow Currency($tax) to return a Real rather than a Currency object, and that's why the $ was not defined for this answer.

I have fixed the macros/contextCurrency.pl file (and also lib/Value/Real.pm and lib/Value/Complex/pm) to correct this error. You will need to update those files to get your problem to work correctly.

One small optimization I might recommend for you. I think that

    Context("Numeric");
    $input = random(4000,15000,10);
    $tax = $T->eval(x=>$input)->inContext(Context());
    Context("Currency");
    $income = Currency($input);
    .
    .
    .
    ANS(Currency($tax)->cmp);
could be replaced by
    Context("Currency");
    $input = random(4000,15000,10);
    $tax = Currency($T->eval(x=>$input));
    $income = Currency($input);
    .
    .
    .
    ANS($tax->cmp);
There is no need to go to the Numeric context, and using Currency() at this point makes it unnecessary to do the inContext() call.

Hope that helps.

Davide

In reply to Davide Cervone

Re: Making A Currency object with a PiecewiseFunction object

by Robin Cruz -

Thanks for the fix and the suggestions.  It's working great now.

--rac