Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Round PG score eariler in logic chain to avoid floating point errors. #739

Open
somiaj opened this issue Oct 12, 2022 · 1 comment
Open

Comments

@somiaj
Copy link
Contributor

somiaj commented Oct 12, 2022

I ran across an issue where lib/WeBWorK/PG/Translator.pm (line 1514) in avg_problem_grader is warning that Error in grading this problem the total 1 is larger than 1, which should only trigger if $total > $count which appear to both be 1 when using a single MultiAnswer grader.

Here is my test problem:

DOCUMENT();
loadMacros(
    'PGstandard.pl',
    'PGML.pl',
    'parserMultiAnswer.pl',
);
$showPartialCorrectAnswers = 1;

$ans1 = Compute("pi");
$ans2 = Compute("x^2");
$ans3 = Compute("3");
$ans4 = Compute("4");
$ans5 = Compute("5");
$ans6 = Compute("6");

$ma = MultiAnswer($ans1, $ans2, $ans3, $ans4, $ans5, $ans6)->with(
    singleResult => 1,
    allowBlankAnswer => 1,
    checkTypes => 1,
    format => '%s; %s; %s; %s; %s; %s',
    tex_format => '%s; %s; %s; %s; %s; %s',
    checker => sub {
        my ($correct, $student, $self, $ansHash) = @_;
        my @c = @{$correct};
        my @s = @{$student};
        my $score = 0;
        $score += 0.3 if ($c[0] == $s[0]);
        $score += 0.5 if ($c[1] == $s[1]);
        foreach (2..5) {
            $score += 0.05 if ($c[$_] == $s[$_]);
        }
        #Value->Error($score);
        return $score;
    }
);

BEGIN_PGML
+ Enter [`\pi`]: [_]{$ma}
+ Enter [`x^2`]: [_]{$ma}
+ Enter [`3`]: [_]{$ma}
+ Enter [`4`]: [_]{$ma}
+ Enter [`5`]: [_]{$ma}
+ Enter [`6`]: [_]{$ma}
END_PGML
ENDDOCUMENT();

The warning only happens if the problem has all answers parts correct. I have tried this with both 2 and 4 answer boxes and the warning doesn't occur, so it is something subtle in the grader I wrote, but can't track it down.

@somiaj
Copy link
Contributor Author

somiaj commented Oct 13, 2022

Trying to debug this, it seems that this is floating point rounding issue. But when the number is actually printed as a string, the floating point error is lost. I was able to fix the issue in this problem by just using return round(100*$score)/100; which seemed to get rid of any hidden floating point issues this particular sum of values created. (Note if I used 0.3 + 0.3 + 0.1 + 0.1 + 0.1 + 0.1, the warning would vanish, the problem would say partial credit was 100% correct, but the WeBWorK AttemptsTable.pm would say the problem was incorrect, so rounding issue was on the other side).

I think the issue is PG should round the score value earlier in the logic chain to remove any possible floating point errors before it attempts any logical checks on the score (such as to see if the score is larger than 1, which is done in both PG and WeBWorK).

@somiaj somiaj changed the title PG warns that 1 is larger than 1 with MultiAnswer Round PG score eariler in logic chain to avoid floating point errors. Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant