Difference between revisions of "Real (MathObject Class)"

From WeBWorK_wiki
Jump to navigation Jump to search
Line 1: Line 1:
 
== The Real Class ==
 
== The 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) &lt; Real(1.0000001)</code> will be false. 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:
+
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) &lt; Real(1.0000001)</code> will be false. 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.
+
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:
+
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 18:
 
$d = Compute("2 - pi"); # parens only needed in Perl expressions
 
$d = Compute("2 - pi"); # parens only needed in Perl expressions
   
  +
<br>
   
 
[[Category:MathObject_Classes]]
 
[[Category:MathObject_Classes]]

Revision as of 16:36, 2 August 2012

The 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 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.

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