Skip to content

Commit

Permalink
Fixes to do with scientific notation
Browse files Browse the repository at this point in the history
fixes #1096

There are a few different fixes to the same problem here.

The regex for scientific notation now allows spaces before and after the
'e', since we saw students doing this. I don't think there's any chance
of ambiguity, because this notation style is used for a number on its
own, not in the JME parser.

The marking script for number entry parts uses `cleanedStudentAnswer`
when counting the significant figures in the student's answer, rather
than `studentAnswer`, so that any parsing differences in the scientific
notation format don't mess it up.
  • Loading branch information
christianp committed Apr 23, 2024
1 parent e430c71 commit 84afbc0
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion marking_scripts/numberentry.jme
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ scientific_precision_offset (A number in scientific notation has 1 more signific
studentPrecision:
max(settings["precision"],
switch(
student_is_scientific, countsigfigs(studentAnswer)-scientific_precision_offset,
student_is_scientific, countsigfigs(cleanedStudentAnswer)-scientific_precision_offset,
settings["precisionType"]="dp", max(settings["precision"],countdp(cleanedStudentAnswer)),
settings["precisionType"]="sigfig", max(settings["precision"],countsigfigs(cleanedStudentAnswer)),
0
Expand Down
9 changes: 5 additions & 4 deletions runtime/scripts/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -1250,19 +1250,20 @@ var math = Numbas.math = /** @lends Numbas.math */ {
return dp;
}
},
/** Calculate the significant figures precision of a number.
/**
* Calculate the significant figures precision of a number.
*
* @param {number|string} n
* @param {number|string} n - if a string, only the "plain" number format or scientific notation is expected. Strings representing numbers should be cleaned first, using `Numbas.util.cleanNumber`.
* @param {boolean} [max] - Be generous with calculating sig. figs. for whole numbers. e.g. '1000' could be written to 4 sig figs.
* @returns {number}
*/
countSigFigs: function(n,max) {
n += '';
var m;
if(max) {
m = n.match(/^-?(?:(\d0*)$|(?:([1-9]\d*[1-9]0*)$)|([1-9]\d*\.\d+$)|(0\.0+$)|(?:0\.0*([1-9]\d*))|(?:(\d*(?:\.\d+)?)[Ee][+\-]?\d+)$)/i);
m = n.match(/^-?(?:(\d0*)$|(?:([1-9]\d*[1-9]0*)$)|([1-9]\d*\.\d+$)|(0\.0+$)|(?:0\.0*([1-9]\d*))|(?:(\d*(?:\.\d+)?)\s*[Ee]\s*[+\-]?\d+)$)/i);
} else {
m = n.match(/^-?(?:(\d)0*$|(?:([1-9]\d*[1-9])0*$)|([1-9]\d*\.\d+$)|(0\.0+$)|(?:0\.0*([1-9]\d*))|(?:(\d*(?:\.\d+)?)[Ee][+\-]?\d+)$)/i);
m = n.match(/^-?(?:(\d)0*$|(?:([1-9]\d*[1-9])0*$)|([1-9]\d*\.\d+$)|(0\.0+$)|(?:0\.0*([1-9]\d*))|(?:(\d*(?:\.\d+)?)\s*[Ee]\s*[+\-]?\d+)$)/i);
}
if(!m)
return 0;
Expand Down
2 changes: 1 addition & 1 deletion runtime/scripts/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,7 @@ var numberNotationStyles = util.numberNotationStyles = {
},
// Significand-exponent ("scientific") style
'scientific': {
re: /^(\d[ \d]*)(\x2E\d[ \d]*)?[eE]([\-+]?\d[ \d]*)/,
re: /^(\d[ \d]*)(\x2E\d[ \d]*)?\s*[eE]\s*([\-+]?\d[ \d]*)/,
clean: function(m) {
return Numbas.math.unscientific(m[0]);
},
Expand Down
11 changes: 6 additions & 5 deletions tests/jme-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -1946,7 +1946,7 @@ var numberNotationStyles = util.numberNotationStyles = {
},
// Significand-exponent ("scientific") style
'scientific': {
re: /^(\d[ \d]*)(\x2E\d[ \d]*)?[eE]([\-+]?\d[ \d]*)/,
re: /^(\d[ \d]*)(\x2E\d[ \d]*)?\s*[eE]\s*([\-+]?\d[ \d]*)/,
clean: function(m) {
return Numbas.math.unscientific(m[0]);
},
Expand Down Expand Up @@ -3471,19 +3471,20 @@ var math = Numbas.math = /** @lends Numbas.math */ {
return dp;
}
},
/** Calculate the significant figures precision of a number.
/**
* Calculate the significant figures precision of a number.
*
* @param {number|string} n
* @param {number|string} n - if a string, only the "plain" number format or scientific notation is expected. Strings representing numbers should be cleaned first, using `Numbas.util.cleanNumber`.
* @param {boolean} [max] - Be generous with calculating sig. figs. for whole numbers. e.g. '1000' could be written to 4 sig figs.
* @returns {number}
*/
countSigFigs: function(n,max) {
n += '';
var m;
if(max) {
m = n.match(/^-?(?:(\d0*)$|(?:([1-9]\d*[1-9]0*)$)|([1-9]\d*\.\d+$)|(0\.0+$)|(?:0\.0*([1-9]\d*))|(?:(\d*(?:\.\d+)?)[Ee][+\-]?\d+)$)/i);
m = n.match(/^-?(?:(\d0*)$|(?:([1-9]\d*[1-9]0*)$)|([1-9]\d*\.\d+$)|(0\.0+$)|(?:0\.0*([1-9]\d*))|(?:(\d*(?:\.\d+)?)\s*[Ee]\s*[+\-]?\d+)$)/i);
} else {
m = n.match(/^-?(?:(\d)0*$|(?:([1-9]\d*[1-9])0*$)|([1-9]\d*\.\d+$)|(0\.0+$)|(?:0\.0*([1-9]\d*))|(?:(\d*(?:\.\d+)?)[Ee][+\-]?\d+)$)/i);
m = n.match(/^-?(?:(\d)0*$|(?:([1-9]\d*[1-9])0*$)|([1-9]\d*\.\d+$)|(0\.0+$)|(?:0\.0*([1-9]\d*))|(?:(\d*(?:\.\d+)?)\s*[Ee]\s*[+\-]?\d+)$)/i);
}
if(!m)
return 0;
Expand Down
1 change: 1 addition & 0 deletions tests/jme/jme-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ Numbas.queueScript('jme_tests',['qunit','jme','jme-rules','jme-display','jme-cal
assert.equal(Numbas.math.countSigFigs('1.23E6'),3,"math.countSigFigs('1.23e6')==3");
assert.equal(Numbas.math.countSigFigs('1.23E-6'),3,"math.countSigFigs('1.23e-6')==3");
assert.equal(Numbas.math.countSigFigs('1.20e6',5),3,"math.countSigFigs('1.20e6',5)==3 (the max setting doesn't have any meaning for E notation)");
assert.equal(Numbas.math.countSigFigs('1,20',5),0,"math.countSigFigs('1,20',5)==1 (only plain notation is expected, so the comma used by Europen notation isn't recognised)");
assert.ok(Numbas.math.eq(NaN,NaN),'NaN = NaN');
assert.notOk(Numbas.math.eq({complex:true,re:1,im:1},{complex:true,re:1,im:2}));
assert.notOk(Numbas.math.eq(Infinity,1));
Expand Down
2 changes: 1 addition & 1 deletion tests/marking_scripts.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 84afbc0

Please sign in to comment.