diff --git a/macros/contexts/contextSignificantFigures.pl b/macros/contexts/contextSignificantFigures.pl index 8e5287f18b..e2f698c6e0 100644 --- a/macros/contexts/contextSignificantFigures.pl +++ b/macros/contexts/contextSignificantFigures.pl @@ -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 will parse the number or string to keep track of significant figures. For example, @@ -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. @@ -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 will also create a SigFigNumber with the second argument the number of @@ -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; diff --git a/t/contexts/significant_figures.t b/t/contexts/significant_figures.t index 89f23d2000..d3bba087b1 100644 --- a/t/contexts/significant_figures.t +++ b/t/contexts/significant_figures.t @@ -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();