Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 699da0a32bc9d623998a26d5e4b0b3fb900ecf36 1 parent dc6d305
ajlopez authored March 25, 2012
36  lib/ajlogo.js
@@ -604,7 +604,41 @@
604 604
 	
605 605
 	topcontext.setProcedure('log10', function(x) {
606 606
 		return Math.log(x) / Math.log(10);
607  
-	});
  607
+	});	
  608
+	
  609
+	topcontext.setProcedure('radsin', function(x) {
  610
+		return Math.sin(x);
  611
+	});	
  612
+
  613
+	topcontext.setProcedure('radcos', function(x) {
  614
+		return Math.cos(x);
  615
+	});	
  616
+
  617
+	topcontext.setProcedure('radtan', function(x) {
  618
+		return Math.tan(x);
  619
+	});	
  620
+
  621
+	function deg2rad(d) { 
  622
+		if (d == 90)
  623
+			return Math.PI / 2;
  624
+		if (d == 180)
  625
+			return Math.PI;
  626
+			
  627
+		return d / 180 * Math.PI;
  628
+	}
  629
+	function rad2deg(r) { return r * 180 / Math.PI; }
  630
+	
  631
+	topcontext.setProcedure('sin', function(x) {
  632
+		return Math.sin(deg2rad(x));
  633
+	});	
  634
+
  635
+	topcontext.setProcedure('cos', function(x) {
  636
+		return Math.cos(deg2rad(x));
  637
+	});	
  638
+
  639
+	topcontext.setProcedure('tan', function(x) {
  640
+		return Math.tan(deg2rad(x));
  641
+	});	
608 642
 	
609 643
 	topcontext.setProcedure('word', function(x, y) {
610 644
 		return x+ ' ' + y;
25  test/test.js
@@ -180,6 +180,12 @@ assert.ok(ctx.getProcedure('sqrt'));
180 180
 assert.ok(ctx.getProcedure('exp'));
181 181
 assert.ok(ctx.getProcedure('log10'));
182 182
 assert.ok(ctx.getProcedure('ln'));
  183
+assert.ok(ctx.getProcedure('sin'));
  184
+assert.ok(ctx.getProcedure('cos'));
  185
+assert.ok(ctx.getProcedure('tan'));
  186
+assert.ok(ctx.getProcedure('radsin'));
  187
+assert.ok(ctx.getProcedure('radcos'));
  188
+assert.ok(ctx.getProcedure('radtan'));
183 189
 
184 190
 result = ajlogo.compileText('make "three 3');
185 191
 (new ajlogo.CompositeExpression(result)).evaluate(ctx);
@@ -363,5 +369,24 @@ assert.equal(2, ajlogo.evaluateText('log10 100'));
363 369
 assert.equal(Math.E, ajlogo.evaluateText('exp 1'));
364 370
 assert.equal(Math.log(10), ajlogo.evaluateText('ln 10'));
365 371
 
  372
+assert.equal(0, ajlogo.evaluateText('sin 0'));
  373
+assert.equal(0, ajlogo.evaluateText('radsin 0'));
366 374
 
  375
+assert.equal(1, ajlogo.evaluateText('sin 90'));
  376
+assert.equal(1, ajlogo.evaluateText('radsin ' + (Math.PI/2)));
  377
+
  378
+assert.equal(1, ajlogo.evaluateText('cos 0'));
  379
+assert.equal(1, ajlogo.evaluateText('radcos 0'));
  380
+
  381
+result = ajlogo.evaluateText('cos 90');
  382
+assert.ok( 0 <= result && result <= 1e-10);
  383
+result = ajlogo.evaluateText('radcos ' + (Math.PI/2));
  384
+assert.ok( 0 <= result && result <= 1e-10);
  385
+
  386
+result = ajlogo.evaluateText('tan 45');
  387
+assert.ok( 0.99999999 <= result && result <= 1.000001);
  388
+assert.equal(0, ajlogo.evaluateText('tan 0'));
  389
+result = ajlogo.evaluateText('radtan '+ (Math.PI/4));
  390
+assert.ok( 0.99999999 <= result && result <= 1.000001);
  391
+assert.equal(0, ajlogo.evaluateText('radtan 0'));
367 392
 

0 notes on commit 699da0a

Please sign in to comment.
Something went wrong with that request. Please try again.