Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(553)

Unified Diff: test/mjsunit/sin-cos.js

Issue 50563003: Implement Math.sin, cos and tan using table lookup and spline interpolation. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/x64/full-codegen-x64.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/mjsunit/sin-cos.js
diff --git a/test/mjsunit/sin-cos.js b/test/mjsunit/sin-cos.js
index e38dfdf814e18c7013b288a4d0c460a7a5355096..b893cce936bd9f66cf73ed0b54c399b32817157b 100644
--- a/test/mjsunit/sin-cos.js
+++ b/test/mjsunit/sin-cos.js
@@ -42,9 +42,101 @@ cosTest();
// By accident, the slow case for sine and cosine were both sine at
// some point. This is a regression test for that issue.
-var x = Math.pow(2, 70);
+var x = Math.pow(2, 30);
assertTrue(Math.sin(x) != Math.cos(x));
// Ensure that sine and log are not the same.
x = 0.5;
assertTrue(Math.sin(x) != Math.log(x));
+
+// Test against approximation by series.
+var factorial = [1];
+var accuracy = 50;
+for (var i = 1; i < accuracy; i++) {
+ factorial[i] = factorial[i-1] * i;
+}
+
+// We sum up in the reverse order for higher precision, as we expect the terms
+// to grow smaller for x reasonably close to 0.
+function precision_sum(array) {
+ var result = 0;
+ while (array.length > 0) {
+ result += array.pop();
+ }
+ return result;
+}
+
+function sin(x) {
+ var sign = 1;
+ var x2 = x*x;
+ var terms = [];
+ for (var i = 1; i < accuracy; i += 2) {
+ terms.push(sign * x / factorial[i]);
+ x *= x2;
+ sign *= -1;
+ }
+ return precision_sum(terms);
+}
+
+function cos(x) {
+ var sign = -1;
+ var x2 = x*x;
+ x = x2;
+ var terms = [1];
+ for (var i = 2; i < accuracy; i += 2) {
+ terms.push(sign * x / factorial[i]);
+ x *= x2;
+ sign *= -1;
+ }
+ return precision_sum(terms);
+}
+
+function abs_error(fun, ref, x) {
+ return Math.abs(ref(x) - fun(x));
+}
+
+var test_inputs = [];
+for (var i = -10000; i < 10000; i += 177) test_inputs.push(i/1257);
+var epsilon = 0.000001;
+
+test_inputs.push(0);
+test_inputs.push(0 + epsilon);
+test_inputs.push(0 - epsilon);
+test_inputs.push(Math.PI/2);
+test_inputs.push(Math.PI/2 + epsilon);
+test_inputs.push(Math.PI/2 - epsilon);
+test_inputs.push(Math.PI);
+test_inputs.push(Math.PI + epsilon);
+test_inputs.push(Math.PI - epsilon);
+test_inputs.push(- 2*Math.PI);
+test_inputs.push(- 2*Math.PI + epsilon);
+test_inputs.push(- 2*Math.PI - epsilon);
+
+var squares = [];
+for (var i = 0; i < test_inputs.length; i++) {
+ var x = test_inputs[i];
+ var err_sin = abs_error(Math.sin, sin, x);
+ var err_cos = abs_error(Math.cos, cos, x)
+ assertTrue(err_sin < 1E-13);
+ assertTrue(err_cos < 1E-13);
+ squares.push(err_sin*err_sin + err_cos*err_cos);
+}
+
+// Sum squares up by adding them pairwise, to avoid losing precision.
+while (squares.length > 1) {
+ var reduced = [];
+ if (squares.length % 2 == 1) reduced.push(squares.pop());
+ // Remaining number of elements is even.
+ while(squares.length > 1) reduced.push(squares.pop() + squares.pop());
+ squares = reduced;
+}
+
+var err_rms = Math.sqrt(squares[0] / test_inputs.length / 2);
+assertTrue(err_rms < 1E-14);
+
+assertEquals(-1, Math.cos({ valueOf: function() { return Math.PI; } }));
+assertEquals(0, Math.sin("0x00000"));
+assertTrue(isNaN(Math.sin(Infinity)));
+assertTrue(isNaN(Math.cos("-Infinity")));
+assertEquals("Infinity", String(Math.tan(Math.PI/2)));
+assertEquals("-Infinity", String(Math.tan(-Math.PI/2)));
« no previous file with comments | « src/x64/full-codegen-x64.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698