Skip to content

Commit cf0f0a9

Browse files
committed
Add the option to give partial credit to an answer
with the correct value, but incorrect number of significant figures. Add additional tests for partial credit.
1 parent f219150 commit cf0f0a9

2 files changed

Lines changed: 101 additions & 7 deletions

File tree

macros/contexts/contextSignificantFigures.pl

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ =head1 DESCRIPTION
2323
where the latter context, you or the student are not allowed to perform any operations on
2424
any numbers.
2525
26-
This is primarily for decimal numbers and keep track of signficant figures. With the context loaded,
26+
This is primarily for decimal numbers and keep track of significant figures. With the context loaded,
2727
a call to C<Real> will parse the number or string to keep track of significant figures. For example,
2828
2929
$x = Real('10.45');
3030
$y = Real('37.1834');
3131
32-
and these numbers will have 6 and 4 signficant figures respectively. To query the number of significant
32+
and these numbers will have 6 and 4 significant figures respectively. To query the number of significant
3333
figures, use the C<sigfigs> method. For example, C<< $x->sigfigs >> will return 4.
3434
3535
The standard arithmetic operations +, -, *, / are defined for these and the result will have the correct
@@ -41,7 +41,7 @@ =head1 DESCRIPTION
4141
4242
$x * $y;
4343
44-
returns C<106.4> (with only 4 signficant figures, since one of them only has four).
44+
returns C<106.4> (with only 4 significant figures, since one of them only has four).
4545
4646
Finally, we can also perform subtraction as in
4747
@@ -52,12 +52,12 @@ =head1 DESCRIPTION
5252
5353
=head2 Significant Figure Rules
5454
55-
A reminder about signficant figures is that all non-zero digits are significant. The rule about a zero's
55+
A reminder about significant figures is that all non-zero digits are significant. The rule about a zero's
5656
significance depends on where it is in a number.
5757
5858
=over
5959
60-
=item * Zeros between any significant digits are signficant. The zeros in 12.0034 are significant. There are 6
60+
=item * Zeros between any significant digits are significant. The zeros in 12.0034 are significant. There are 6
6161
significant figures in this number.
6262
6363
=item * Zeros to the left of a non-zero digit are not significant. The zeros in 0.00123 are not signficant.
@@ -67,7 +67,7 @@ =head2 Significant Figure Rules
6767
significant. There are 6 significant figures in this number.
6868
6969
=item * Zeros to the left of the decimal point and to the right of a non zero digit are not significant. The
70-
zeros in 12300 are not significant. There are 3 significant figures in this number. However, the presesence of
70+
zeros in 12300 are not significant. There are 3 significant figures in this number. However, the presence of
7171
a significant zero changes the rule. The zeros in 12300.0 are all significant because the rightmost 0 is
7272
significant and therefore the other zeros are significant.
7373
@@ -116,13 +116,26 @@ =head3 Setting the number of significant figures.
116116
which has 6 significant figures. If C<< $x->sigfigs(4) >>, then the result is the number '12.35', where
117117
rounding has been performed.
118118
119+
=head2 Flags
120+
121+
The flag C<partial_credit> on the context will award partial credit for an answer that is correct to
122+
within the given tolerance but the number of significant figures is not correct. In addition, a
123+
warning is shown to the student. The value of C<partial_credit> should be between 0 and 1.
124+
125+
For example,
126+
127+
Context('SignificantFigures')->flags->set(tolerance => 0.01, partial_credit => 0.6);
128+
129+
will set the tolerance to 0.01 (this is the same as tolerance for reals) and the amount of
130+
partial credit to give for the correct answer with wrong number of significant figures.
131+
119132
=head2 SigFigNumber
120133
121134
The function C<SigFigNumber> will also create a SigFigNumber with the second argument the number of
122135
significant figures. For example,
123136
124137
$a = SigFigNumber(12.345);
125-
$b = SigFigNumber(10000,3);
138+
$b = SigFigNumber(10000, 3);
126139
127140
will create a number with 5 and 3 significant figures respectively.
128141
@@ -332,6 +345,29 @@ sub ROUND {
332345
return sprintf("%.0E", $x + $d) - $d;
333346
}
334347

348+
# This method checks to see if the student answer is equal (in the Value::Real sense)
349+
# to the correct answer, but the incorrect number of significant figures.
350+
# If so, show a warning and given partial credit.
351+
352+
sub cmp_postprocess {
353+
my ($self, $ansHash) = @_;
354+
355+
return unless $self->getFlag('partial_credit') && $ansHash->score < 1;
356+
357+
my $student = $ansHash->{student_value};
358+
my $correct = $ansHash->{correct_value};
359+
360+
# Create Value::Real versions of the student and correct answer
361+
362+
my $student_real = Value::Real->new($student->string);
363+
my $correct_real = Value::Real->new($correct->string);
364+
365+
if ($student_real == $correct_real && $student->sigfigs != $correct->sigfigs) {
366+
$ansHash->{ans_message} = "Incorrect number of significant figures";
367+
$ansHash->score($self->getFlag('partial_credit'));
368+
}
369+
}
370+
335371
package context::SignificantFigures::BOP::parse;
336372
our @ISA = ("Parser::BOP");
337373

t/contexts/significant_figures.t

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,62 @@ subtest 'Significant Figures for integers' => sub {
417417
is $a6->E, 5, '-3.28 * 10^5 = -3.28 * 10^5 (check exp)';
418418
};
419419

420+
subtest 'Significant Figures for partial credit' => sub {
421+
422+
# test an actual problem
423+
424+
my $source = <<~ 'END_SOURCE';
425+
DOCUMENT();
426+
427+
loadMacros("PGstandard.pl","PGML.pl",'contextSignificantFigures.pl');
428+
Context('SignificantFigures')->flags->set(tolerance => 0.01,partial_credit=>0.6);
429+
$a=Real('123.0');
430+
BEGIN_PGML
431+
Enter the value [$a]
432+
433+
[_]{$a}
434+
END_PGML
435+
ENDDOCUMENT();
436+
END_SOURCE
437+
438+
ok my $pg = WeBWorK::PG->new(
439+
r_source => \$source,
440+
inputs_ref => { AnSwEr0001 => '123.0' },
441+
processAnswers => 1
442+
),
443+
'source string renders';
444+
445+
is $pg->{result}{score}, 1, 'correct answer is scored correctly';
446+
447+
my $pg2 = WeBWorK::PG->new(
448+
r_source => \$source,
449+
processAnswers => 1,
450+
inputs_ref => { AnSwEr0001 => '123.00' },
451+
);
452+
453+
is $pg2->{result}{score}, 0.6, 'reduced credit is scored correctly';
454+
like $pg2->{answers}{AnSwEr0001}{ans_message}, qr/Incorrect number of significant figures/,
455+
'Answer processed showing message.';
456+
457+
my $pg3 = WeBWorK::PG->new(
458+
r_source => \$source,
459+
processAnswers => 1,
460+
inputs_ref => { AnSwEr0001 => '1.230 * 10^2' },
461+
);
462+
463+
is $pg3->{result}{score}, 1, 'scientific notation is correct.';
464+
465+
my $pg4 = WeBWorK::PG->new(
466+
r_source => \$source,
467+
processAnswers => 1,
468+
inputs_ref => { AnSwEr0001 => '1.23 * 10^2' },
469+
);
470+
471+
is $pg4->{result}{score}, 0.6, 'scientific notation is scored with correct partial credit.';
472+
473+
like $pg4->{answers}{AnSwEr0001}{ans_message}, qr/Incorrect number of significant figures/,
474+
'Answer processed showing message.';
475+
476+
};
477+
420478
done_testing();

0 commit comments

Comments
 (0)