Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions macros/contexts/contextSignificantFigures.pl
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ =head1 DESCRIPTION

or

Context('LimitedSignificantFigures'); # TO BE DONE
Context('LimitedSignificantFigures');

where the latter context, you or the student are not allowed to perform any operations on
any numbers.
where the latter context, the student are not allowed to perform any operations on
any numbers. See below.

This is primarily for decimal numbers and keep track of signficant figures. With the context loaded,
a call to C<Real> will parse the number or string to keep track of significant figures. For example,
Expand All @@ -39,13 +39,13 @@ =head1 DESCRIPTION

returns the value C<20.63>, where the first number is rounded to the hundredths place before adding.

$x * $y;
$x * $y;

returns C<106.4> (with only 4 signficant figures, since one of them only has four).

Finally, we can also perform subtraction as in

$x - $y;
$x - $y;

however, subtraction can lose significant figures. The answer to this is C<0.23>, so only 2 significant
figures.
Expand Down Expand Up @@ -116,6 +116,16 @@ =head3 Setting the number of significant figures.
which has 6 significant figures. If C<< $x->sigfigs(4) >>, then the result is the number '12.35', where
rounding has been performed.

=head3 LimitedSignificantFigures

An additional available context is set with

Context('LimitedSignificantFigures');

which disallows students from using any operators in their answers. For instructors
if the intent is to have your students understand Significant Figures, this is probably
the context you want to use.

=head2 SigFigNumber

The function C<SigFigNumber> will also create a SigFigNumber with the second argument the number of
Expand Down Expand Up @@ -335,6 +345,10 @@ package context::SignificantFigures;

sub Init {
my $context = $main::context{SignificantFigures} = context::SignificantFigures::Context->new();
$context = $main::context{LimitedSignificantFigures} = $context->copy;
$context->{name} = 'LimitedSignificantFigures';
$context->operators->undefine($context->operators->names);
$context->parens->undefine('|', '{', '[');
}

package context::SignificantFigures::Context;
Expand Down
30 changes: 25 additions & 5 deletions t/contexts/significant_figures.t
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,30 @@ subtest 'Division' => sub {

};

# subtest 'Significant Figures for integers' => sub {
# # my $a1 = Compute('1.0 * 10^2');
# my $a1 = Compute('1.0E+02');
# is $a1->format('E'), '1.0E+02', '1.0 * 10^2 internally is 1.0E+02';
# };
subtest 'Significant Figures for integers' => sub {
# my $a1 = Compute('1.0 * 10^2');
my $a1 = Compute('1.0E+02');
is $a1->format('E'), '1.0E+02', '1.0 * 10^2 internally is 1.0E+02';
};

subtest 'Use Compute for operations' => sub {
my $a1 = Compute('4.078+2.3');
is $a1->format('E'), '6.4E+00', 'Check that addition in Compute is correct.';
my $a2 = Compute('4.078-2.3');
is $a2->format('E'), '1.8E+00', 'Check that subtraction in Compute is correct.';
my $a3 = Compute('4.03*2.3');
is $a3->format('E'), '9.3E+00', 'Check that multiplication in Compute is correct.';
my $a4 = Compute('4.03/2.3');
is $a4->format('E'), '1.8E+00', 'Check that division in Compute is correct.';
};

subtest 'Check the LimitedSignificantFigures context' => sub {
Context('LimitedSignificantFigures');
like dies { Compute('4.078+2.3') }, qr/Can't use '\+' in this context/, 'Check that + is not defined.';
like dies { Compute('4.078-2.3') }, qr/Can't use '\-' in this context/, 'Check that - is not defined.';
like dies { Compute('4.03*2.3') }, qr/Can't use '\*' in this context/, 'Check that * is not defined.';
like dies { Compute('4.03/2.3') }, qr/Can't use '\/' in this context/, 'Check that / is not defined.';
like dies { Compute('4.03^3') }, qr/Can't use '\^' in this context/, 'Check that ^ is not defined.';
};

done_testing();