Skip to content

Commit 4b6b95e

Browse files
committed
extend isnan and withintolerance to complex numbers
1 parent 83d6fb3 commit 4b6b95e

File tree

8 files changed

+48
-2
lines changed

8 files changed

+48
-2
lines changed

runtime/scripts/jme-builtins.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,9 @@ newBuiltin('countsigfigs', [TString], TNum, function(s) {
10261026
return math.countSigFigs(util.cleanNumber(s));
10271027
});
10281028
newBuiltin('isnan', [TNum], TBool, function(n) {
1029+
if(n.complex) {
1030+
return isNaN(n.re) || isNaN(n.im);
1031+
}
10291032
return isNaN(n);
10301033
});
10311034
newBuiltin('matchnumber', [TString, sig.listof(sig.type('string'))], TList, function(s, styles) {

runtime/scripts/jme.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,8 @@ var jme = Numbas.jme = /** @lends Numbas.jme */ {
694694
}
695695
} else if(v instanceof math.ComplexDecimal) {
696696
return new jme.types.TDecimal(v);
697+
} else if(typeof v == 'object' && v && v.complex && v.hasOwnProperty('re') && v.hasOwnProperty('im')) {
698+
return new jme.types.TNum(v);
697699
} else if(v instanceof Decimal) {
698700
return new jme.types.TDecimal(v);
699701
} else if(v instanceof math.Fraction) {

runtime/scripts/math.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,11 @@ var math = Numbas.math = /** @lends Numbas.math */ {
13171317
* @returns {boolean}
13181318
*/
13191319
withinTolerance: function(a, b, tolerance) {
1320+
if(a.complex || b.complex) {
1321+
a = a.complex ? a : math.complex(a,0);
1322+
b = b.complex ? b : math.complex(b,0);
1323+
return math.withinTolerance(a.re, b.re, tolerance) && math.withinTolerance(a.im, b.im, tolerance);
1324+
}
13201325
if(tolerance == 0) {
13211326
return math.eq(a, b);
13221327
} else {

tests/jme-runtime.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3510,6 +3510,11 @@ var math = Numbas.math = /** @lends Numbas.math */ {
35103510
* @returns {boolean}
35113511
*/
35123512
withinTolerance: function(a, b, tolerance) {
3513+
if(a.complex || b.complex) {
3514+
a = a.complex ? a : math.complex(a,0);
3515+
b = b.complex ? b : math.complex(b,0);
3516+
return math.withinTolerance(a.re, b.re, tolerance) && math.withinTolerance(a.im, b.im, tolerance);
3517+
}
35133518
if(tolerance == 0) {
35143519
return math.eq(a, b);
35153520
} else {
@@ -9509,6 +9514,8 @@ var jme = Numbas.jme = /** @lends Numbas.jme */ {
95099514
}
95109515
} else if(v instanceof math.ComplexDecimal) {
95119516
return new jme.types.TDecimal(v);
9517+
} else if(typeof v == 'object' && v && v.complex && v.hasOwnProperty('re') && v.hasOwnProperty('im')) {
9518+
return new jme.types.TNum(v);
95129519
} else if(v instanceof Decimal) {
95139520
return new jme.types.TDecimal(v);
95149521
} else if(v instanceof math.Fraction) {
@@ -11181,6 +11188,9 @@ Scope.prototype = /** @lends Numbas.jme.Scope.prototype */ {
1118111188
*/
1118211189
getFunction: function(name) {
1118311190
name = jme.normaliseName(name, this);
11191+
if(jme.funcSynonyms[name]) {
11192+
name = jme.funcSynonyms[name];
11193+
}
1118411194
if(!this._resolved_functions[name]) {
1118511195
var scope = this;
1118611196
var o = [];
@@ -15658,6 +15668,9 @@ newBuiltin('countsigfigs', [TString], TNum, function(s) {
1565815668
return math.countSigFigs(util.cleanNumber(s));
1565915669
});
1566015670
newBuiltin('isnan', [TNum], TBool, function(n) {
15671+
if(n.complex) {
15672+
return isNaN(n.re) || isNaN(n.im);
15673+
}
1566115674
return isNaN(n);
1566215675
});
1566315676
newBuiltin('matchnumber', [TString, sig.listof(sig.type('string'))], TList, function(s, styles) {

tests/jme/doc-tests.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,14 @@ export default
699699
{
700700
"in": "withintolerance(pi,22/7,0.1)",
701701
"out": "true"
702+
},
703+
{
704+
"in": "withintolerance(1 + 0.11i, 1 + 1/10 i, 0.02)",
705+
"out": "true"
706+
},
707+
{
708+
"in": "withintolerance(1 + 0.11i, 2 + 1/10 i, 0.02)",
709+
"out": "false"
702710
}
703711
]
704712
},

tests/jme/jme-tests.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,10 @@ Numbas.queueScript('jme_tests',['qunit','jme','jme-rules','jme-display','jme-cal
529529
n.precision = -30;
530530
var dn = jme.castToType(n, 'decimal').value;
531531
assert.equal(dn+'', '314159265358979334144', 'Can convert a number with negative decimal places precision to a decimal, without throwing an error.');
532+
533+
assert.notOk(evaluate('isnan(i)').value, 'i is not NaN');
534+
assert.ok(evaluate('isnan(nan * i)').value, 'nan*i is NaN');
535+
assert.ok(evaluate('isnan(nan + i)').value, 'nan + i is NaN');
532536
});
533537

534538
QUnit.test('jme.enumerate_signatures', function(assert) {

tests/locales.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5932,7 +5932,7 @@ Numbas.locale = {
59325932
"suspend.you can resume": "Du kan fortsette eksamen neste gang du starter denne aktiviteten.",
59335933
"suspend.resume": "Fortsett",
59345934
"result.exit": "Avslutt eksamen",
5935-
"result.print": "Skriv ut denne oversikten",
5935+
"result.print": "Skriv ut detaljert resultat",
59365936
"result.exam summary": "Eksamen oversikt",
59375937
"result.performance summary": "Resultatsammendrag",
59385938
"result.exam start": "Eksamen start:",
@@ -6446,7 +6446,8 @@ Numbas.locale = {
64466446
"worksheet.left": "Left",
64476447
"worksheet.bottom": "Bottom",
64486448
"worksheet.right": "Right"
6449-
}}
6449+
}
6450+
}
64506451
,
64516452
"nl-nl": {translation:
64526453
{

tests/numbas-runtime.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3511,6 +3511,11 @@ var math = Numbas.math = /** @lends Numbas.math */ {
35113511
* @returns {boolean}
35123512
*/
35133513
withinTolerance: function(a, b, tolerance) {
3514+
if(a.complex || b.complex) {
3515+
a = a.complex ? a : math.complex(a,0);
3516+
b = b.complex ? b : math.complex(b,0);
3517+
return math.withinTolerance(a.re, b.re, tolerance) && math.withinTolerance(a.im, b.im, tolerance);
3518+
}
35143519
if(tolerance == 0) {
35153520
return math.eq(a, b);
35163521
} else {
@@ -8845,6 +8850,8 @@ var jme = Numbas.jme = /** @lends Numbas.jme */ {
88458850
}
88468851
} else if(v instanceof math.ComplexDecimal) {
88478852
return new jme.types.TDecimal(v);
8853+
} else if(typeof v == 'object' && v && v.complex && v.hasOwnProperty('re') && v.hasOwnProperty('im')) {
8854+
return new jme.types.TNum(v);
88488855
} else if(v instanceof Decimal) {
88498856
return new jme.types.TDecimal(v);
88508857
} else if(v instanceof math.Fraction) {
@@ -14997,6 +15004,9 @@ newBuiltin('countsigfigs', [TString], TNum, function(s) {
1499715004
return math.countSigFigs(util.cleanNumber(s));
1499815005
});
1499915006
newBuiltin('isnan', [TNum], TBool, function(n) {
15007+
if(n.complex) {
15008+
return isNaN(n.re) || isNaN(n.im);
15009+
}
1500015010
return isNaN(n);
1500115011
});
1500215012
newBuiltin('matchnumber', [TString, sig.listof(sig.type('string'))], TList, function(s, styles) {

0 commit comments

Comments
 (0)