Skip to content

Commit 51d2673

Browse files
committed
in reldiff checking function, take the absolute value of the max error
The reldiff checking function checks `abs(r1-r2) < r2 * tolerance`. When `r2` is negative, it's important to take the absolute value of *both* sides.
1 parent d6b79e6 commit 51d2673

4 files changed

Lines changed: 16 additions & 3 deletions

File tree

‎runtime/scripts/jme.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4163,7 +4163,7 @@ var checkingFunctions = jme.checkingFunctions =
41634163
if(math.isComplexDecimal(r1) || math.isComplexDecimal(r2)) {
41644164
r1 = math.ensure_decimal(r1);
41654165
r2 = math.ensure_decimal(r2);
4166-
return r1.minus(r2).absoluteValue().re.lessThan(r2.re.times(tolerance));
4166+
return r1.minus(r2).absoluteValue().re.lessThan(r2.re.times(tolerance).absoluteValue());
41674167
}
41684168

41694169
if(r1===Infinity || r1===-Infinity)

‎tests/jme-runtime.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12933,7 +12933,7 @@ var checkingFunctions = jme.checkingFunctions =
1293312933
if(math.isComplexDecimal(r1) || math.isComplexDecimal(r2)) {
1293412934
r1 = math.ensure_decimal(r1);
1293512935
r2 = math.ensure_decimal(r2);
12936-
return r1.minus(r2).absoluteValue().re.lessThan(r2.re.times(tolerance));
12936+
return r1.minus(r2).absoluteValue().re.lessThan(r2.re.times(tolerance).absoluteValue());
1293712937
}
1293812938

1293912939
if(r1===Infinity || r1===-Infinity)

‎tests/numbas-runtime.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12269,7 +12269,7 @@ var checkingFunctions = jme.checkingFunctions =
1226912269
if(math.isComplexDecimal(r1) || math.isComplexDecimal(r2)) {
1227012270
r1 = math.ensure_decimal(r1);
1227112271
r2 = math.ensure_decimal(r2);
12272-
return r1.minus(r2).absoluteValue().re.lessThan(r2.re.times(tolerance));
12272+
return r1.minus(r2).absoluteValue().re.lessThan(r2.re.times(tolerance).absoluteValue());
1227312273
}
1227412274

1227512275
if(r1===Infinity || r1===-Infinity)

‎tests/parts/part-tests.mjs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,19 @@ Numbas.queueScript('part_tests',['qunit','json','jme','localisation','parts/numb
537537
assert.equal(p.getCorrectAnswer(p.getScope()), '(alpha*x)^2');
538538
});
539539

540+
QUnit.test('reldiff takes the absolute value of denominator', async function(assert) {
541+
var p = createPartFromJSON({
542+
"type": "jme",
543+
"marks": 1,
544+
"answer": "-t - 1/4",
545+
"checkingType": "reldiff",
546+
"checkingAccuracy": "0.001"
547+
});
548+
549+
var res = await mark_part(p, '-t - 1/4');
550+
assert.equal(res.credit, 1, '"-t - 1/4" is correct');
551+
});
552+
540553
QUnit.module('Pattern match');
541554
QUnit.test('Answer is "hi+"', async function(assert) {
542555
var p = createPartFromJSON({type:'patternmatch', answer: 'hi+', displayAnswer: 'hi'});

0 commit comments

Comments
 (0)