## 2017 Problem Authoring Workshop

### Specifying a NoDecimal context for a single variable. ### Specifying a NoDecimal context for a single variable.

by tim Payer -
Number of replies: 8
Greetings,

1.) I have used the the restricted context parser for an entire problem:

Context("Numeric");
Parser::Number::NoDecimals($context); But I have not been able to determine how to specify a NoDecimal context for just a specific variable within a homework problem that uses Context("Numeric"); for the remaining variables within a problem. Could you please reveal the syntax for a single variable to be restricted to a NoDecimal context? Thanks, Tim In reply to tim Payer ### Re: Specifying a NoDecimal context for a single variable. by tim Payer - a follow-up on overriding the context for a no decimal variable: I tried the following:$aa = Real(8);
## Excluding decimals:
$fnd = Compute("sqrt($aa-3)")->with(Parser::Number::NoDecimals($context)); ## Permitting Decimals ? Context("Numeric");$f = Compute("sqrt($aa-3)"); This will restrict the use of decimals for the$fnd variable, but not the $f variable. Is there a more concise way to specify a change in context? I will keep trying... In reply to tim Payer ### Re: Specifying a NoDecimal context for a single variable. by Davide Cervone - Here, you have effectively done what I recommend below, but not very cleanly. The with() is supposed to take a list of property assignments, e.g., with(tolerance => .01, tolType => 'absolute'), but you have passed it the result of Parser::Number::NoDecimals($context) instead. it turns out that the result is empty, so you are passing nothing to with(), so no properties are set for $fnd. But the Parser::Number:NoDecimals($context) is being made, so no decimals are being set for some context. But which one? It looks like it should be for the context pointed to by $context, but you have never set this value (at least not in the code snippet you have provided), so $context is undefined, and you are passing an undefined value to Parser>>Number::NoDecimals(). This is equivalent to passing no value to that function, and if you don't pass it a context, it sets the current context to not allow decimals.

So that means you have effectively done

Context("Numeric");
Parser::Number::NoDecimals();
$aa = Real(8);$fnd = Compute("sqrt($aa-3)"); Context("Numeric");$f = Compute("sqrt($aa-3)");  which is essentially the same as my suggestion below. So you have the right idea, but the details are not as clean as they could be. In reply to Davide Cervone ### Re: Specifying a NoDecimal context for a single variable. by tim Payer - Thank you Davide. It seems that once a Context has been set that context will exist for all variables that follow, until another context is set for those variables that follow a new context. I had mistakenly thought that I could flag a particular variable with a given context. But I see that the way to do it is to treat the context as separate line of code that will delineate for each variable between these stated lines of Context. Thanks again. It really helps when you explain the why of the code. Tim In reply to tim Payer ### Re: Specifying a NoDecimal context for a single variable. by Davide Cervone - You are on the right track with your second post above, but have the syntax wrong (I'll explain that shortly). What you want to do is use two separate contexts, with with no decimals and one with decimals. Because MathObjects remember the context in which they were created, you can create one variable in the first and one in the second, and the first will now allow decimals, while the second allows them. Here is an example: loadMacros("PGML.pl"); Context("Numeric"); Parser::Number::NoDecimals(Context());$r1 = Real(5);

Context("Numeric");
$r2 = Real(5); BEGIN_PGML [__________]{$r1} and [__________]{$r2} END_PGML  Here, $r1 will no allow decimals, while \$r2 will. Try it out in the Interactive PG Lab and see. ### Re: Specifying a NoDecimal context for a single variable.

by Barbra Steinhurst -
I am trying to use this to frame one of my homework problems last week (in set homework2, problem #3) to use only whole numbers and fractions (no decimals) in the problem statement. It is not quite working. Not sure if I'm missing something, or if this does something different than what I'm trying to do. ### Re: Specifying a NoDecimal context for a single variable.

by tim Payer -
Barbra,

http://webwork.maa.org/pod/pg_TRUNK/macros/contextFraction.pl.html

Tim  