Eval()vs.substitute(): Difference between revisions
Jump to navigation
Jump to search
No edit summary |
(added historical tag and gave updated problem link) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{historical}} | |||
<p style="font-size: 120%;font-weight:bold">This problem has been replaced with [https://openwebwork.github.io/pg-docs/sample-problems/problem-techniques/EvalVersusSubstitute.html a newer version of this problem]</p> | |||
The aim of this page is to demonstrate the difference between <code>eval()</code> and <code>substitute</code>. | The aim of this page is to demonstrate the difference between <code>eval()</code> and <code>substitute</code>. | ||
Line 10: | Line 14: | ||
$f = Compute(sqrt(3x + 1); | $f = Compute(sqrt(3x + 1); | ||
$fx = $f-> | $fx = $f->substitute(x=>"3"); | ||
displays (3.16228) | displays (3.16228) | ||
Line 16: | Line 20: | ||
One of the biggest differences between <code>eval()</code> and <code>substitute</code> is when they are used in conjunction with flags such as <code>reduceConstants</code> and <code>reduceConstantFunctions</code>, which is described in more detail at:[[FormattingCorrectAnswers:_NumbersAndFormulas]] | One of the biggest differences between <code>eval()</code> and <code>substitute</code> is when they are used in conjunction with flags such as <code>reduceConstants</code> and <code>reduceConstantFunctions</code>, which is described in more detail at:[[FormattingCorrectAnswers:_NumbersAndFormulas]] | ||
[[Category:Authors]] |
Latest revision as of 13:32, 28 June 2023
This problem has been replaced with a newer version of this problem
The aim of this page is to demonstrate the difference between eval()
and substitute
.
Note:PGLabs is an efficient way to check code.
$f = Compute(sqrt(3x + 1); $fx = $f->eval(x=>"3"); displays 3.16228
The correct answer is a number.
$f = Compute(sqrt(3x + 1); $fx = $f->substitute(x=>"3"); displays (3.16228)
The correct answer is a formula (hence the parenthesis)
One of the biggest differences between eval()
and substitute
is when they are used in conjunction with flags such as reduceConstants
and reduceConstantFunctions
, which is described in more detail at:FormattingCorrectAnswers:_NumbersAndFormulas