Constants in Problems

Provides constants in a PG problem.

Complete Code

Download file: ConstantsInProblems.pg

PG problem file

Explanation

DOCUMENT();
loadMacros('PGstandard.pl', 'PGML.pl', 'PGcourse.pl');

Preamble

These standard macros need to be loaded.
Context()->constants->add(k => 0.01);

# This means that student answers are not reduced to the numerical value
# specified in the Context
Context()->flags->set(formatStudentAnswer => 'parsed');

$ans = Compute('k');

Setup

No changes are needed in the tagging and description or initialization sections of the problem file. In the problem set-up section, we add to the Context the constants we’re going to use. Here we define a constant k, and assign it a value that will be used when expressions involving k are evaluated. Do not set k=1, because if you do, then kxand x/k are equivalent, for example. Obviously, do not set k=0.

In this case we specified constants->add(), so that the constant k is added to existing constants in the problem. If we had used constants->are(), we would also remove all predefined constants from the Context (in a manner similar to the use of variables->add() and variables->are() when defining variables in a problem.

One other tweak that we might want to put in here is to reset a Context flag so that students’ answers are not reduced to numerical values when they are previewed or submitted. This is done by setting the formatStudentAnswer flag, as shown.

BEGIN_PGML
[`f(x) = x - k`] (where [`k > 0`] is constant) is zero when [`x =`] [___]{$ans}
END_PGML

Statement

This is the problem statement in PGML.
BEGIN_PGML_SOLUTION
Solution explanation goes here.
END_PGML_SOLUTION

ENDDOCUMENT();

Solution

A solution should be provided here.