Skip to content

Commit

Permalink
jmeDecimal: only convert to exponential notation when plaindecimal is…
Browse files Browse the repository at this point in the history
… false

fixes #1027, I think.

There's still a lingering issue that decimal values are subbed in
"plain", so the simplification step leads to them being rounded off as
normal `number` values. But this at least resolves the big inaccuracy.
  • Loading branch information
christianp committed Aug 15, 2023
1 parent 72b0d83 commit 6387d40
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
6 changes: 3 additions & 3 deletions runtime/scripts/jme-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -2307,12 +2307,12 @@ JMEifier.prototype = {
}
} else if(n instanceof Decimal) {
var out = math.niceDecimal(n,options);
if(out.length>20) {
out = n.toExponential().replace(/e\+0$/,'');
}
if(this.settings.plaindecimal) {
return out;
} else {
if(out.length > 20) {
out = n.toExponential().replace(/e\+0$/,'');
}
return 'dec("'+out+'")';
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions tests/jme-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -18111,12 +18111,12 @@ JMEifier.prototype = {
}
} else if(n instanceof Decimal) {
var out = math.niceDecimal(n,options);
if(out.length>20) {
out = n.toExponential().replace(/e\+0$/,'');
}
if(this.settings.plaindecimal) {
return out;
} else {
if(out.length > 20) {
out = n.toExponential().replace(/e\+0$/,'');
}
return 'dec("'+out+'")';
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions tests/numbas-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -18100,12 +18100,12 @@ JMEifier.prototype = {
}
} else if(n instanceof Decimal) {
var out = math.niceDecimal(n,options);
if(out.length>20) {
out = n.toExponential().replace(/e\+0$/,'');
}
if(this.settings.plaindecimal) {
return out;
} else {
if(out.length > 20) {
out = n.toExponential().replace(/e\+0$/,'');
}
return 'dec("'+out+'")';
}
} else {
Expand Down
10 changes: 10 additions & 0 deletions tests/parts/part-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,16 @@ Numbas.queueScript('part_tests',['qunit','json','jme','localisation','parts/numb
}
);

QUnit.test('Decimal values aren\'t substituted with scientific notation', function(assert) {
var data = {
"type": "jme",
"marks": 1,
"answer": "{dec(\"1.234567890123456e-1\")}",
};
var p = createPartFromJSON(data);
assert.equal(p.getCorrectAnswer(p.getScope()), '0.1234567890123456');
});

question_unit_test("Expression is case-sensitive",
{
"name":"case sensitivity",
Expand Down

0 comments on commit 6387d40

Please sign in to comment.