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

Side by Side 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, 1 month 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 unified diff | Download patch | Annotate | Revision Log
« src/runtime.cc ('K') | « src/x64/full-codegen-x64.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 24 matching lines...) Expand all
35 function cosTest() { 35 function cosTest() {
36 assertEquals(1, Math.cos(0)); 36 assertEquals(1, Math.cos(0));
37 assertEquals(-1, Math.cos(Math.PI)); 37 assertEquals(-1, Math.cos(Math.PI));
38 } 38 }
39 39
40 sinTest(); 40 sinTest();
41 cosTest(); 41 cosTest();
42 42
43 // By accident, the slow case for sine and cosine were both sine at 43 // By accident, the slow case for sine and cosine were both sine at
44 // some point. This is a regression test for that issue. 44 // some point. This is a regression test for that issue.
45 var x = Math.pow(2, 70); 45 var x = Math.pow(2, 30);
46 assertTrue(Math.sin(x) != Math.cos(x)); 46 assertTrue(Math.sin(x) != Math.cos(x));
47 47
48 // Ensure that sine and log are not the same. 48 // Ensure that sine and log are not the same.
49 x = 0.5; 49 x = 0.5;
50 assertTrue(Math.sin(x) != Math.log(x)); 50 assertTrue(Math.sin(x) != Math.log(x));
51
52 // Test against approximation by series.
53 var factorial = [1];
54 var accuracy = 50;
55 for (var i = 1; i < accuracy; i++) {
56 factorial[i] = factorial[i-1] * i;
57 }
58
59 function sum(array) {
60 var result = 0;
61 while (array.length > 0) {
62 result += array.pop();
63 }
64 return result;
65 }
66
67 function sin(x) {
68 var sign = 1;
69 var x2 = x*x;
70 var terms = [];
Jakob Kummerow 2013/10/29 17:17:53 Might wanna add a comment here that the reason for
71 for (var i = 1; i < accuracy; i += 2) {
72 terms.push(sign * x / factorial[i]);
73 x *= x2;
74 sign *= -1;
75 }
76 return sum(terms);
77 }
78
79 function cos(x) {
80 var sign = -1;
81 var x2 = x*x;
82 x = x2;
83 var terms = [1];
84 for (var i = 2; i < accuracy; i += 2) {
85 terms.push(sign * x / factorial[i]);
86 x *= x2;
87 sign *= -1;
88 }
89 return sum(terms);
90 }
91
92 function abs_error(fun, ref, x) {
93 return Math.abs(ref(x) - fun(x));
94 }
95
96 var test_inputs = [];
97 for (var i = -10000; i < 10000; i += 177) test_inputs.push(i/1257);
98 var epsilon = 0.000001;
99
100 test_inputs.push(0);
101 test_inputs.push(0 + epsilon);
102 test_inputs.push(0 - epsilon);
103 test_inputs.push(Math.PI/2);
104 test_inputs.push(Math.PI/2 + epsilon);
105 test_inputs.push(Math.PI/2 - epsilon);
106 test_inputs.push(Math.PI);
107 test_inputs.push(Math.PI + epsilon);
108 test_inputs.push(Math.PI - epsilon);
109 test_inputs.push(- 2*Math.PI);
110 test_inputs.push(- 2*Math.PI + epsilon);
111 test_inputs.push(- 2*Math.PI - epsilon);
112
113 var squares = [];
114 for (var i = 0; i < test_inputs.length; i++) {
115 var x = test_inputs[i];
116 var err_sin = abs_error(Math.sin, sin, x);
117 var err_cos = abs_error(Math.cos, cos, x)
118 assertTrue(err_sin < 1E-13);
119 assertTrue(err_cos < 1E-13);
120 squares.push(err_sin*err_sin + err_cos*err_cos);
121 }
122
123 // Sum squares up by adding them pairwise, to avoid not losing precision.
Jakob Kummerow 2013/10/29 17:17:53 s/not // ;-)
124 while (squares.length > 1) {
125 var reduced = [];
126 if (squares.length % 2 == 1) reduced.push(squares.pop());
127 // Remaining number of elements is even.
128 while(squares.length > 1) reduced.push(squares.pop() + squares.pop());
129 squares = reduced;
130 }
131
132 var err_rms = Math.sqrt(squares[0] / test_inputs.length / 2);
133 assertTrue(err_rms < 1E-14);
134
135 assertEquals(-1, Math.cos({ valueOf: function() { return Math.PI; } }));
136 assertEquals(0, Math.sin("0x00000"));
137 assertTrue(isNaN(Math.sin(Infinity)));
138 assertTrue(isNaN(Math.cos("-Infinity")));
OLDNEW
« src/runtime.cc ('K') | « src/x64/full-codegen-x64.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698