Skip to content

Commit

Permalink
correct ln, exp and arg on decimal values
Browse files Browse the repository at this point in the history
fixes #1059.

This commit also makes isclose work with complex values: it only checks
the absolute tolerance.
  • Loading branch information
christianp committed Nov 8, 2023
1 parent ad533d5 commit 8e4851e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
5 changes: 3 additions & 2 deletions runtime/scripts/jme-builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -1016,8 +1016,9 @@ newBuiltin('mod', [TDecimal,TDecimal], TDecimal, function(a,b) {
}
return m;
});
newBuiltin('exp',[TDecimal], TDecimal, function(a) {return a.re.exp(); });
newBuiltin('ln',[TDecimal], TDecimal, function(a) {return a.re.ln(); });
newBuiltin('exp',[TDecimal], TDecimal, function(a) {return a.exp(); });
newBuiltin('ln',[TDecimal], TDecimal, function(a) {return a.ln(); });
newBuiltin('arg', [TDecimal], TDecimal, function(a) { return a.argument(); } );
newBuiltin('countsigfigs',[TDecimal], TInt, function(a) {return a.re.countSigFigs(); });
newBuiltin('round',[TDecimal], TDecimal, function(a) {return a.round(); });
newBuiltin('sin',[TDecimal], TDecimal, function(a) {return a.re.sin(); });
Expand Down
13 changes: 12 additions & 1 deletion runtime/scripts/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,11 @@ var math = Numbas.math = /** @lends Numbas.math */ {
}
rel_tol = rel_tol===undefined ? 1e-15 : rel_tol;
abs_tol = abs_tol===undefined ? 1e-15: abs_tol;

if(a.complex || b.complex) {
return math.abs(math.sub(a,b)) < abs_tol;
}

return Math.abs(a-b) <= Math.max( rel_tol * Math.max(Math.abs(a), Math.abs(b)), abs_tol );
},

Expand Down Expand Up @@ -2616,8 +2621,14 @@ ComplexDecimal.prototype = {
},

ln: function() {
return new ComplexDecimal(this.absoluteValue().re.ln(), this.argument());
return new ComplexDecimal(this.absoluteValue().re.ln(), this.argument().re);
},

exp: function() {
var r = this.re.exp();
return new ComplexDecimal(r.times(Decimal.cos(this.im)), r.times(Decimal.sin(this.im)));
},

isInt: function() {
return this.re.isInt() && this.im.isInt();
},
Expand Down
16 changes: 13 additions & 3 deletions tests/jme-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -2685,6 +2685,9 @@ var math = Numbas.math = /** @lends Numbas.math */ {
}
rel_tol = rel_tol===undefined ? 1e-15 : rel_tol;
abs_tol = abs_tol===undefined ? 1e-15: abs_tol;
if(a.complex || b.complex) {
return math.abs(math.sub(a,b)) < abs_tol;
}
return Math.abs(a-b) <= Math.max( rel_tol * Math.max(Math.abs(a), Math.abs(b)), abs_tol );
},

Expand Down Expand Up @@ -4791,8 +4794,14 @@ ComplexDecimal.prototype = {
},

ln: function() {
return new ComplexDecimal(this.absoluteValue().re.ln(), this.argument());
return new ComplexDecimal(this.absoluteValue().re.ln(), this.argument().re);
},

exp: function() {
var r = this.re.exp();
return new ComplexDecimal(r.times(Decimal.cos(this.im)), r.times(Decimal.sin(this.im)));
},

isInt: function() {
return this.re.isInt() && this.im.isInt();
},
Expand Down Expand Up @@ -14126,8 +14135,9 @@ newBuiltin('mod', [TDecimal,TDecimal], TDecimal, function(a,b) {
}
return m;
});
newBuiltin('exp',[TDecimal], TDecimal, function(a) {return a.re.exp(); });
newBuiltin('ln',[TDecimal], TDecimal, function(a) {return a.re.ln(); });
newBuiltin('exp',[TDecimal], TDecimal, function(a) {return a.exp(); });
newBuiltin('ln',[TDecimal], TDecimal, function(a) {return a.ln(); });
newBuiltin('arg', [TDecimal], TDecimal, function(a) { return a.argument(); } );
newBuiltin('countsigfigs',[TDecimal], TInt, function(a) {return a.re.countSigFigs(); });
newBuiltin('round',[TDecimal], TDecimal, function(a) {return a.round(); });
newBuiltin('sin',[TDecimal], TDecimal, function(a) {return a.re.sin(); });
Expand Down
2 changes: 2 additions & 0 deletions tests/jme/jme-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,8 @@ Numbas.queueScript('jme_tests',['qunit','jme','jme-rules','jme-display','jme-cal
closeEqual(assert, evaluate('exp(5)').value,Math.exp(5),'exp(5)');
closeEqual(assert, evaluate('exp(-2)').value,Math.exp(-2),'exp(-2)');
deepCloseEqual(assert, evaluate('exp(4-i)').value,math.complex(Math.exp(4)*Math.cos(-1),Math.exp(4)*Math.sin(-1)),'exp(4-i)');
assert.ok(evaluate('isclose(ln(dec(-e)), dec(ln(e)) - dec(pi)*i)').value, 'ln(dec(-e))');
assert.ok(evaluate('isclose(exp(dec(pi/2)*i), i)').value, 'exp(dec(pi/2)*i)');

closeEqual(assert, evaluate('fact(0)').value,1,'fact(0)');
closeEqual(assert, evaluate('fact(1)').value,1,'fact(1)');
Expand Down
18 changes: 14 additions & 4 deletions tests/numbas-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -2686,6 +2686,9 @@ var math = Numbas.math = /** @lends Numbas.math */ {
}
rel_tol = rel_tol===undefined ? 1e-15 : rel_tol;
abs_tol = abs_tol===undefined ? 1e-15: abs_tol;
if(a.complex || b.complex) {
return math.abs(math.sub(a,b)) < abs_tol;
}
return Math.abs(a-b) <= Math.max( rel_tol * Math.max(Math.abs(a), Math.abs(b)), abs_tol );
},

Expand Down Expand Up @@ -4792,8 +4795,14 @@ ComplexDecimal.prototype = {
},

ln: function() {
return new ComplexDecimal(this.absoluteValue().re.ln(), this.argument());
return new ComplexDecimal(this.absoluteValue().re.ln(), this.argument().re);
},

exp: function() {
var r = this.re.exp();
return new ComplexDecimal(r.times(Decimal.cos(this.im)), r.times(Decimal.sin(this.im)));
},

isInt: function() {
return this.re.isInt() && this.im.isInt();
},
Expand Down Expand Up @@ -14114,8 +14123,9 @@ newBuiltin('mod', [TDecimal,TDecimal], TDecimal, function(a,b) {
}
return m;
});
newBuiltin('exp',[TDecimal], TDecimal, function(a) {return a.re.exp(); });
newBuiltin('ln',[TDecimal], TDecimal, function(a) {return a.re.ln(); });
newBuiltin('exp',[TDecimal], TDecimal, function(a) {return a.exp(); });
newBuiltin('ln',[TDecimal], TDecimal, function(a) {return a.ln(); });
newBuiltin('arg', [TDecimal], TDecimal, function(a) { return a.argument(); } );
newBuiltin('countsigfigs',[TDecimal], TInt, function(a) {return a.re.countSigFigs(); });
newBuiltin('round',[TDecimal], TDecimal, function(a) {return a.round(); });
newBuiltin('sin',[TDecimal], TDecimal, function(a) {return a.re.sin(); });
Expand Down Expand Up @@ -28884,7 +28894,7 @@ Numbas.queueScript('answer-widgets',['knockout','util','jme','jme-display'],func
return '';
}
if(util.isNumber(n, vm.allowFractions, vm.allowedNotationStyles)) {
return n;
return n + '';
}
return Numbas.math.niceNumber(n,{style: vm.allowedNotationStyles[0]}) || '';
}
Expand Down

0 comments on commit 8e4851e

Please sign in to comment.