Difference between revisions of "Real (MathObject Class)"
m (moved Real MathObject Class to Real (MathObject Class)) |
(Added Parser::Number::NoDecimals example) |
||
(8 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | == |
+ | === Real class === |
− | + | The Real class implements real numbers with "fuzzy" comparison (governed by the same tolerances and settings that control student answer checking). For example, <code>Real(1.0) == Real(1.0000001)</code> will be true, while <code>Real(1.0) < Real(1.0000001)</code> will be false. Reals can be created in any Context, but the <code>Numeric</code> Context is the most frequently used for Reals. |
|
+ | |||
+ | |||
+ | === Creation === |
||
+ | |||
+ | Reals are created via the <code>Real()</code> function, or by <code>Compute()</code>. Reals can be added, subtracted, and so on, and the results will still be MathObject Reals. Similarly, <code>sin()</code>, <code>sqrt()</code>, <code>ln()</code>, and the other functions return Real objects if their arguments are Reals. For example: |
||
Context("Numeric"); |
Context("Numeric"); |
||
Line 9: | Line 9: | ||
$c = sqrt($a); # same as Real(sqrt(2)); |
$c = sqrt($a); # same as Real(sqrt(2)); |
||
− | + | This allows you to compute with Reals just as you would with native Perl real numbers. |
|
− | : The value <code>pi</code> can be used in your Perl code to represent the value of <math>\pi</math>. Note that you must use <code>-(pi)</code> for <math>-\pi</math> in Perl expressions (but not in strings that will be parsed by MathObjects, such as student answers or arguments to <code>Compute()</code>). For instance: |
||
+ | |||
+ | === Pre-defined Reals === |
||
+ | |||
+ | The value <code>pi</code> can be used in your Perl code to represent the value of <math>\pi</math>. Note that you must use <code>-(pi)</code> for <math>-\pi</math> in Perl expressions (but not in strings that will be parsed by MathObjects, such as student answers or arguments to <code>Compute()</code>). For instance: |
||
$a = pi + 2; # same as Real("pi + 2"); |
$a = pi + 2; # same as Real("pi + 2"); |
||
Line 18: | Line 21: | ||
$d = Compute("2 - pi"); # parens only needed in Perl expressions |
$d = Compute("2 - pi"); # parens only needed in Perl expressions |
||
+ | The value <code>e</code>, for the base of the natural log, <math>e</math>, can be used in student answers and parsed strings. |
||
+ | |||
+ | $e = Compute("e"); |
||
+ | $p = Compute("e^2"); |
||
+ | |||
+ | |||
+ | === Answer Checker === |
||
+ | |||
+ | As with all MathObjects, you obtain an answer checker for a Real object via the <code>cmp()</code> method: |
||
+ | |||
+ | ANS(Real(2)->cmp); |
||
+ | |||
+ | The Real class supports the [[Answer_Checker_Options_(MathObjects)| common answer-checker options]], and the following additional options: |
||
+ | |||
+ | {| class="wikitable" |
||
+ | ! Option !! Description !! style="padding:5px" | Default |
||
+ | |- style="vertical-align: top" |
||
+ | | style="padding: 5px; white-space: nowrap" | <code>ignoreInfinity => 1</code> or <code>0</code> |
||
+ | | style="padding: 5px" | Do/don't report type mismatches if the student enters an infinity. |
||
+ | | style="text-align:center" | <code>1</code> |
||
+ | |} |
||
+ | |||
+ | |||
+ | === Methods === |
||
+ | |||
+ | The Real class supports the [[Common MathObject Methods]]. There are no additional methods for this class. |
||
+ | |||
+ | The command |
||
+ | |||
+ | Parser::Number::NoDecimals(); |
||
+ | |||
+ | will add a check so that a number with decimal places is not allowed. This can be used to require students to enter values in terms of `pi` or `sqrt(2)` for example. You can also supply a Context as an argument to disallow decimals in that context: |
||
+ | |||
+ | Parser::Number::NoDecimals($context); |
||
+ | |||
+ | Without a parameter, the current context is assumed. |
||
+ | |||
+ | |||
+ | === Properties === |
||
+ | |||
+ | The Real class supports the [[Common MathObject Properties]], and the following additional ones: |
||
+ | |||
+ | {| class="wikitable" |
||
+ | ! Property !! Description !! style="padding:5px" | Default |
||
+ | |||
+ | |- style="vertical-align: baseline" |
||
+ | | style="padding: 5px; white-space: nowrap" | <code>$r->{period}</code> |
||
+ | | style="padding: 5px" | When set, this value indicates that the real is periodic, with period given by this value. So angles might use <code>period</code> set to <code>2*pi</code>. |
||
+ | |||
+ | '''Example:''' |
||
+ | : <code>$r = Real(pi/3)->with(period => 2*pi);</code> |
||
+ | : <code>$r == 7*pi/3; # will be true</code> |
||
+ | | style="text-align:center" | <code>undef</code> |
||
+ | |||
+ | |- style="vertical-align: baseline" |
||
+ | | style="padding: 5px; white-space: nowrap" | <code>$r->{logPeriodic}</code> |
||
+ | | style="padding: 5px" | When <code>period</code> is defined, and <code>logPeriodic</code> is set to <code>1</code> this indicates that the periodicity is logarithmic (i.e., the period refers to the log of the value, not the value itself). |
||
+ | |||
+ | '''Example:''' |
||
+ | : <code>$r = Real(4)->with(period => 10, logPeriodic => 1);</code> |
||
+ | : <code>$r == 88105; # true since 88105 is nearly exp(10+log(4))</code> |
||
+ | | style="text-align:center" | <code>0</code> |
||
+ | |} |
||
+ | |||
+ | |||
+ | <br> |
||
[[Category:MathObject_Classes]] |
[[Category:MathObject_Classes]] |
Latest revision as of 15:29, 4 February 2013
Real class
The Real class implements real numbers with "fuzzy" comparison (governed by the same tolerances and settings that control student answer checking). For example, Real(1.0) == Real(1.0000001)
will be true, while Real(1.0) < Real(1.0000001)
will be false. Reals can be created in any Context, but the Numeric
Context is the most frequently used for Reals.
Creation
Reals are created via the Real()
function, or by Compute()
. Reals can be added, subtracted, and so on, and the results will still be MathObject Reals. Similarly, sin()
, sqrt()
, ln()
, and the other functions return Real objects if their arguments are Reals. For example:
Context("Numeric"); $a = Real(2); $b = $a + 5; # same as Real(7); $c = sqrt($a); # same as Real(sqrt(2));
This allows you to compute with Reals just as you would with native Perl real numbers.
Pre-defined Reals
The value pi
can be used in your Perl code to represent the value of [math]\pi[/math]. Note that you must use -(pi)
for [math]-\pi[/math] in Perl expressions (but not in strings that will be parsed by MathObjects, such as student answers or arguments to Compute()
). For instance:
$a = pi + 2; # same as Real("pi + 2"); $b = 2 - (pi); # same as Real("2 - pi"); $c = sin(pi/2); # same as Real(1); $d = Compute("2 - pi"); # parens only needed in Perl expressions
The value e
, for the base of the natural log, [math]e[/math], can be used in student answers and parsed strings.
$e = Compute("e"); $p = Compute("e^2");
Answer Checker
As with all MathObjects, you obtain an answer checker for a Real object via the cmp()
method:
ANS(Real(2)->cmp);
The Real class supports the common answer-checker options, and the following additional options:
Option | Description | Default |
---|---|---|
ignoreInfinity => 1 or 0
|
Do/don't report type mismatches if the student enters an infinity. | 1
|
Methods
The Real class supports the Common MathObject Methods. There are no additional methods for this class.
The command
Parser::Number::NoDecimals();
will add a check so that a number with decimal places is not allowed. This can be used to require students to enter values in terms of `pi` or `sqrt(2)` for example. You can also supply a Context as an argument to disallow decimals in that context:
Parser::Number::NoDecimals($context);
Without a parameter, the current context is assumed.
Properties
The Real class supports the Common MathObject Properties, and the following additional ones:
Property | Description | Default |
---|---|---|
$r->{period}
|
When set, this value indicates that the real is periodic, with period given by this value. So angles might use period set to 2*pi .
Example:
|
undef
|
$r->{logPeriodic}
|
When period is defined, and logPeriodic is set to 1 this indicates that the periodicity is logarithmic (i.e., the period refers to the log of the value, not the value itself).
Example:
|
0
|