contextSignificantFigures.pl - Implements a context to handle numbers where significant figures is important.
This file implements a MathObject SignificantFigures class that provides the ability to create numbers for given significant figures as well as the operations +, -, *, /, **
To load this context, use
loadMacros('contextSignificantFigures.pl');
and then set this context with
Context('SignificantFigures');
or
Context('LimitedSignificantFigures');
where the latter context, operations between numbers are not allowed.
This is primarily for decimal numbers and keep track of significant figures. With the context loaded, a call to Real will parse the number or string to keep track of significant figures. For example,
$x = Real('37.45');
$y = Real('37.1834');
and these numbers will have 4 and 6 significant figures respectively. To query the number of significant figures, use the sigfigs method. For example, $x->sigfigs will return 4.
The standard arithmetic operations +, -, *, / are defined for these and the result will have the correct number of significant figures. For example
$x + $y;
returns the value 74.63, where the first number is rounded to the hundredths place before adding.
$x * $y;
returns 1392 or 1.392E+02.
Finally, we can also perform subtraction as in
$x - $y;
however, subtraction can lose significant figures. The answer to this is 0.27, resulting in only 2 significant figures.
A reminder about significant figures is that all non-zero digits are significant. The rule about a zero's significance depends on where it is in a number.
Zeros between any significant digits are significant. The zeros in 12.0034 are significant. There are 6 significant figures in this number.
Zeros to the left of a non-zero digit are not significant. The zeros in 0.00123 are not significant. There are 3 significant figures in this number.
Zeros to right of the decimal point and to the right a non-zero digit are significant. The zeros in 12.3400 are significant. There are 6 significant figures in this number.
Zeros to the left of the decimal point and to the right of a non-zero digit are not significant. The zeros in 12300 are not significant. There are 3 significant figures in this number. However, the presence of a significant zero changes the rule. The zeros in 12300.0 are all significant because the rightmost 0 is significant and therefore the other zeros are significant.
Note: If a number only consists of zeros has some different rules generally as a result of other operations.
The number 0 has infinite significant figures.
The number 0. has 1 significant figure.
The number 0.00 has 3 significant figures.
The number of significant figures can be set in two ways. The first, in creation of the number if the option sigfigs is used. For example
Real('100', sigfigs => 2)
will give the number 1.0 * 10^2. Alternatively, if a number is already created, then the sigfigs method can set the number of significant figures. If
$x = Real('1000');
then $x has 1 significant figure, but $x->sigfigs(3) will update that to 1.00 * 10^3.
One can set a number with infinite significant figures with sigfigs => 'inf'. This is often done with known constants. A simple example would be that in the perimeter of a square with side x, or 4x, the 4 would have infinite significant figures, meaning that the result would have the same significant figures as the number x. Example:
$x = Real(17.05);
$p = Real(4, sigfigs => 'inf') * $x;
One can set the number of significant figures after a number has been created with the sigfigs method. For example,
$x = Real(12.3456);
which has 6 significant figures. If $x->sigfigs(4), then the result is the number '12.35', where rounding has been performed.
There are two flags that give authors some control over messaging for near-correct answers. The default behavior is that a correct answer in this context is only given when a student has correct number of significant figures and the correct answer (to all digits).
If an author wants show a message and possibly give partial credit for a correct answer (within tolerance) but the incorrect number of significant figures, then set the partial_incorrect_sf flag to a value between 0 and 1.
If the flag partial_incorrect_sf is set, and a student has the correct answer to within tolerance (using any of the tolerances set by Value::Real) as well as the the incorrect number of significant figures, then a message will be shown to the student and the student will receive partial credit with this value.
For example,
Context('SignificantFigures')->flags->set(tolerance => 0.01, partial_incorrect_sf => 0.6);
will set the tolerance to 0.01 (this is the same tolerance flag for reals) and the amount of partial credit to give for the correct answer with wrong number of significant figures.
Note that if the author would like to have the message shown, but no partial credit, use partial_incorrect_sf => 0.
If an author would like to show a message and possibly give partial credit for a correct answer (within tolerance) and correct number of significant figures, then the flag partial_sf_within_tolerance can be used.
If this flag exists and a student has the correct number of significant figures and the answer is within tolerance (using those set by Value::Real) then a message will be shown and the student will receive this value on the answer.
For example,
Context('SignificantFigures')->flags->set(partial_sf_within_tolerance => 0.8);
Note that if the author would like to have the message shown, but no partial credit, use partial_sf_within_tolerance => 0.
The function SigFigNumber will also create a SigFigNumber with the second argument the number of significant figures. For example,
$a = SigFigNumber(12.345);
$b = SigFigNumber(10000,3);
will create a number with 5 and 3 significant figures respectively.