Permalink
Browse files

sin, cos, tan, radsin, radcos, radtan with tests

  • Loading branch information...
1 parent dc6d305 commit 699da0a32bc9d623998a26d5e4b0b3fb900ecf36 @ajlopez committed Mar 25, 2012
Showing with 60 additions and 1 deletion.
  1. +35 −1 lib/ajlogo.js
  2. +25 −0 test/test.js
View
@@ -604,7 +604,41 @@
topcontext.setProcedure('log10', function(x) {
return Math.log(x) / Math.log(10);
- });
+ });
+
+ topcontext.setProcedure('radsin', function(x) {
+ return Math.sin(x);
+ });
+
+ topcontext.setProcedure('radcos', function(x) {
+ return Math.cos(x);
+ });
+
+ topcontext.setProcedure('radtan', function(x) {
+ return Math.tan(x);
+ });
+
+ function deg2rad(d) {
+ if (d == 90)
+ return Math.PI / 2;
+ if (d == 180)
+ return Math.PI;
+
+ return d / 180 * Math.PI;
+ }
+ function rad2deg(r) { return r * 180 / Math.PI; }
+
+ topcontext.setProcedure('sin', function(x) {
+ return Math.sin(deg2rad(x));
+ });
+
+ topcontext.setProcedure('cos', function(x) {
+ return Math.cos(deg2rad(x));
+ });
+
+ topcontext.setProcedure('tan', function(x) {
+ return Math.tan(deg2rad(x));
+ });
topcontext.setProcedure('word', function(x, y) {
return x+ ' ' + y;
View
@@ -180,6 +180,12 @@ assert.ok(ctx.getProcedure('sqrt'));
assert.ok(ctx.getProcedure('exp'));
assert.ok(ctx.getProcedure('log10'));
assert.ok(ctx.getProcedure('ln'));
+assert.ok(ctx.getProcedure('sin'));
+assert.ok(ctx.getProcedure('cos'));
+assert.ok(ctx.getProcedure('tan'));
+assert.ok(ctx.getProcedure('radsin'));
+assert.ok(ctx.getProcedure('radcos'));
+assert.ok(ctx.getProcedure('radtan'));
result = ajlogo.compileText('make "three 3');
(new ajlogo.CompositeExpression(result)).evaluate(ctx);
@@ -363,5 +369,24 @@ assert.equal(2, ajlogo.evaluateText('log10 100'));
assert.equal(Math.E, ajlogo.evaluateText('exp 1'));
assert.equal(Math.log(10), ajlogo.evaluateText('ln 10'));
+assert.equal(0, ajlogo.evaluateText('sin 0'));
+assert.equal(0, ajlogo.evaluateText('radsin 0'));
+assert.equal(1, ajlogo.evaluateText('sin 90'));
+assert.equal(1, ajlogo.evaluateText('radsin ' + (Math.PI/2)));
+
+assert.equal(1, ajlogo.evaluateText('cos 0'));
+assert.equal(1, ajlogo.evaluateText('radcos 0'));
+
+result = ajlogo.evaluateText('cos 90');
+assert.ok( 0 <= result && result <= 1e-10);
+result = ajlogo.evaluateText('radcos ' + (Math.PI/2));
+assert.ok( 0 <= result && result <= 1e-10);
+
+result = ajlogo.evaluateText('tan 45');
+assert.ok( 0.99999999 <= result && result <= 1.000001);
+assert.equal(0, ajlogo.evaluateText('tan 0'));
+result = ajlogo.evaluateText('radtan '+ (Math.PI/4));
+assert.ok( 0.99999999 <= result && result <= 1.000001);
+assert.equal(0, ajlogo.evaluateText('radtan 0'));

0 comments on commit 699da0a

Please sign in to comment.