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

Side by Side Diff: test/mjsunit/sin-cos.js

Issue 411263004: Implement trigonometric functions using a fdlibm port. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Added README.v8 Created 6 years, 4 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 unified diff | Download patch | Annotate | Revision Log
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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 } 150 }
151 151
152 var err_rms = Math.sqrt(squares[0] / test_inputs.length / 2); 152 var err_rms = Math.sqrt(squares[0] / test_inputs.length / 2);
153 assertEqualsDelta(0, err_rms, 1E-14); 153 assertEqualsDelta(0, err_rms, 1E-14);
154 154
155 assertEquals(-1, Math.cos({ valueOf: function() { return Math.PI; } })); 155 assertEquals(-1, Math.cos({ valueOf: function() { return Math.PI; } }));
156 assertEquals(0, Math.sin("0x00000")); 156 assertEquals(0, Math.sin("0x00000"));
157 assertEquals(1, Math.cos("0x00000")); 157 assertEquals(1, Math.cos("0x00000"));
158 assertTrue(isNaN(Math.sin(Infinity))); 158 assertTrue(isNaN(Math.sin(Infinity)));
159 assertTrue(isNaN(Math.cos("-Infinity"))); 159 assertTrue(isNaN(Math.cos("-Infinity")));
160 assertEquals("Infinity", String(Math.tan(Math.PI/2))); 160 assertTrue(Math.tan(Math.PI/2) > 1e16);
161 assertEquals("-Infinity", String(Math.tan(-Math.PI/2))); 161 assertTrue(Math.tan(-Math.PI/2) < -1e16);
162 assertEquals("-Infinity", String(1/Math.sin("-0"))); 162 assertEquals("-Infinity", String(1/Math.sin("-0")));
163 163
164 // Assert that the remainder after division by pi is reasonably precise. 164 // Assert that the remainder after division by pi is reasonably precise.
165 function assertError(expected, x, epsilon) { 165 function assertError(expected, x, epsilon) {
166 assertTrue(Math.abs(x - expected) < epsilon); 166 assertTrue(Math.abs(x - expected) < epsilon);
167 } 167 }
168 168
169 assertEqualsDelta(0.9367521275331447, Math.cos(1e06), 1e-15); 169 assertEqualsDelta(0.9367521275331447, Math.cos(1e06), 1e-15);
170 assertEqualsDelta(0.8731196226768560, Math.cos(1e10), 1e-08); 170 assertEqualsDelta(0.8731196226768560, Math.cos(1e10), 1e-08);
171 assertEqualsDelta(0.9367521275331447, Math.cos(-1e06), 1e-15); 171 assertEqualsDelta(0.9367521275331447, Math.cos(-1e06), 1e-15);
172 assertEqualsDelta(0.8731196226768560, Math.cos(-1e10), 1e-08); 172 assertEqualsDelta(0.8731196226768560, Math.cos(-1e10), 1e-08);
173 assertEqualsDelta(-0.3499935021712929, Math.sin(1e06), 1e-15); 173 assertEqualsDelta(-0.3499935021712929, Math.sin(1e06), 1e-15);
174 assertEqualsDelta(-0.4875060250875106, Math.sin(1e10), 1e-08); 174 assertEqualsDelta(-0.4875060250875106, Math.sin(1e10), 1e-08);
175 assertEqualsDelta(0.3499935021712929, Math.sin(-1e06), 1e-15); 175 assertEqualsDelta(0.3499935021712929, Math.sin(-1e06), 1e-15);
176 assertEqualsDelta(0.4875060250875106, Math.sin(-1e10), 1e-08); 176 assertEqualsDelta(0.4875060250875106, Math.sin(-1e10), 1e-08);
177 assertEqualsDelta(0.7796880066069787, Math.sin(1e16), 1e-05); 177 assertEqualsDelta(0.7796880066069787, Math.sin(1e16), 1e-05);
178 assertEqualsDelta(-0.6261681981330861, Math.cos(1e16), 1e-05); 178 assertEqualsDelta(-0.6261681981330861, Math.cos(1e16), 1e-05);
179 179
180 // Assert that remainder calculation terminates. 180 // Assert that remainder calculation terminates.
181 for (var i = -1024; i < 1024; i++) { 181 for (var i = -1024; i < 1024; i++) {
182 assertFalse(isNaN(Math.sin(Math.pow(2, i)))); 182 assertFalse(isNaN(Math.sin(Math.pow(2, i))));
183 } 183 }
184 184
185 assertFalse(isNaN(Math.cos(1.57079632679489700))); 185 assertFalse(isNaN(Math.cos(1.57079632679489700)));
186 assertFalse(isNaN(Math.cos(-1e-100))); 186 assertFalse(isNaN(Math.cos(-1e-100)));
187 assertFalse(isNaN(Math.cos(-1e-323))); 187 assertFalse(isNaN(Math.cos(-1e-323)));
188
189 // Tests for specific values expected from the fdlibm implementation.
190
191 var two_32 = Math.pow(2, -32);
192 var two_28 = Math.pow(2, -28);
193
194 // Tests for Math.sin for |x| < pi/4
195 assertEquals(Infinity, 1/Math.sin(+0.0));
196 assertEquals(-Infinity, 1/Math.sin(-0.0));
197 // sin(x) = x for x < 2^-27
198 assertEquals(two_32, Math.sin(two_32));
199 assertEquals(-two_32, Math.sin(-two_32));
200 // sin(pi/8) = sqrt(sqrt(2)-1)/2^(3/4)
201 assertEquals(0.3826834323650898, Math.sin(Math.PI/8));
202 assertEquals(-0.3826834323650898, -Math.sin(Math.PI/8));
203
204 // Tests for Math.cos for |x| < pi/4
205 // cos(x) = 1 for |x| < 2^-27
206 assertEquals(1, Math.cos(two_32));
207 assertEquals(1, Math.cos(-two_32));
208 // cos(pi/20) = sqrt(sqrt(2)*sqrt(sqrt(5)+5)+4)/2^(3/2)
Raymond Toy 2014/07/30 19:55:04 This is tests the |x| < 0.3 branch in KERNELCOS.
Yang 2014/08/01 07:29:55 Done.
209 assertEquals(0.9876883405951378, Math.cos(Math.PI/20));
210 assertEquals(0.7100335477927638, Math.cos(0.7812504768371582));
211 assertEquals(0.7100338835660797, Math.cos(0.78125));
Raymond Toy 2014/07/30 19:55:04 Lines 210-211 are testing the KERNELCOS test for x
Yang 2014/08/01 07:29:55 Done.
212 // cos(pi/8) = sqrt(sqrt(2)+1)/2^(3/4)
Raymond Toy 2014/07/30 19:55:05 This tests the |x| > 0.3 branch in KERNELCOS
Yang 2014/08/01 07:29:56 Done.
213 assertEquals(0.9238795325112867, Math.cos(Math.PI/8));
214 assertEquals(0.9238795325112867, Math.cos(-Math.PI/8));
215
216 // Tests for Math.tan for |x| < pi/4
217 assertEquals("Infinity", String(1/Math.tan(0.0)));
Raymond Toy 2014/07/30 19:55:05 For the sin test, you used Infinity and +0.0. I th
Yang 2014/08/01 07:29:56 Done.
218 assertEquals("-Infinity", String(1/Math.tan(-0.0)));
219 // tan(x) = x for |x| < 2^-28
220 assertEquals(two_32, Math.tan(two_32));
221 assertEquals(-two_32, Math.tan(-two_32));
222 assertEquals(0.8211418015898941, Math.tan(11/16));
Raymond Toy 2014/07/30 19:55:05 Lines 222-223 tests the case |x| > 0.67434 in KERN
Yang 2014/08/01 07:29:56 Done.
223 assertEquals(-0.8211418015898941, Math.tan(-11/16));
224 assertEquals(0.41421356237309503, Math.tan(Math.PI / 8));
Raymond Toy 2014/07/30 19:55:04 This tests the case in KERNELTAN for |x| < 0.67434
Yang 2014/08/01 07:29:55 Done.
225
226 // Tests for Math.sin.
227 assertEquals(0.479425538604203, Math.sin(0.5));
228 assertEquals(-0.479425538604203, Math.sin(-0.5));
229 assertEquals(1, Math.sin(Math.PI/2));
230 assertEquals(-1, Math.sin(-Math.PI/2));
231 assertEquals(1.2246467991473532e-16, Math.sin(Math.PI));
Raymond Toy 2014/07/30 19:55:04 Verifies Math.sin(Math.PI) is not zero because Mat
Yang 2014/08/01 07:29:56 Done.
232 assertEquals(-7.047032979958965e-14, Math.sin(2200*Math.PI));
233 assertEquals(-0.7071067811865477, Math.sin(7/4 * Math.PI));
234 assertEquals(0.7071067811865474, Math.sin(9/4 * Math.PI));
235 assertEquals(0.7071067811865483, Math.sin(11/4 * Math.PI));
236 assertEquals(-0.7071067811865479, Math.sin(13/4 * Math.PI));
Raymond Toy 2014/07/30 19:55:05 I think lines 233-236 are intended to test the 4 v
Yang 2014/08/01 07:29:56 Done.
237 assertEquals(-3.2103381051568376e-11, Math.sin(1048576/4 * Math.PI));
238
239 // Tests for Math.cos.
240 assertEquals(1, Math.cos(two_28));
241 assertEquals(0.9689124217106447, Math.cos(0.25));
242 assertEquals(0.8775825618903728, Math.cos(0.5));
243 assertEquals(0.7073882691671998, Math.cos(0.785));
Raymond Toy 2014/07/30 19:55:04 Lines 241-243 are more tests on the three possible
Yang 2014/08/01 07:29:55 Done.
244 assertEquals(6.123233995736766e-17, Math.cos(Math.PI/2));
Raymond Toy 2014/07/30 19:55:05 Verifies Math.cos(Math.PI/2) is not 0 because Math
Yang 2014/08/01 07:29:56 Done.
245 assertEquals(0.7071067811865474, Math.cos(7/4 * Math.PI));
246 assertEquals(0.7071067811865477, Math.cos(9/4 * Math.PI));
247 assertEquals(-0.7071067811865467, Math.cos(11/4 * Math.PI));
248 assertEquals(-0.7071067811865471, Math.cos(13/4 * Math.PI));
Raymond Toy 2014/07/30 19:55:04 Lines 245-248 tests the four possible cases in com
Yang 2014/08/01 07:29:55 Done.
249 assertEquals(0.9367521275331447, Math.cos(1000000));
250 assertEquals(-3.435757038074824e-12, Math.cos(1048575/2 * Math.PI));
251
252 // Tests for Math.tan.
253 assertEquals(two_28, Math.tan(two_28));
254 assertEquals(1.633123935319537e16, Math.tan(Math.PI/2));
Raymond Toy 2014/07/30 19:55:05 Verifies Math.tan(Math.PI/2) is not an infinity be
Yang 2014/08/01 07:29:56 Done.
255 assertEquals(0.5463024898437905, Math.tan(0.5));
256 assertEquals(0.8211418015898941, Math.tan(11/16));
Raymond Toy 2014/07/30 19:55:04 255-256 tests (again) the various branches in KERN
Yang 2014/08/01 07:29:56 Done.
257 assertEquals(2.0000000000000027, Math.tan(1.107148717794091));
258 assertEquals(-1.0000000000000004, Math.tan(7/4*Math.PI));
259 assertEquals(0.9999999999999994, Math.tan(9/4*Math.PI));
Raymond Toy 2014/07/30 19:55:04 Lines 258-259 verifies the two cases of tan callin
Yang 2014/08/01 07:29:56 Done.
260 assertEquals(-6.420676210313675e-11, Math.tan(1048576/2*Math.PI));
261 assertEquals(2.910566692924059e11, Math.tan(1048575/2*Math.PI));
262
263 // Test Hayne-Panek reduction.
264 assertEquals(0.377820109360752e0, Math.sin(Math.pow(2, 120)));
265 assertEquals(-0.9258790228548379e0, Math.cos(Math.pow(2, 120)));
266 assertEquals(-0.40806638884180424e0, Math.tan(Math.pow(2, 120)));
267 assertEquals(-0.377820109360752e0, Math.sin(-Math.pow(2, 120)));
268 assertEquals(-0.9258790228548379e0, Math.cos(-Math.pow(2, 120)));
269 assertEquals(0.40806638884180424e0, Math.tan(-Math.pow(2, 120)));
Raymond Toy 2014/07/30 19:55:05 I still think some test from special cases listed
Yang 2014/08/01 07:29:55 Not sure what spec you are referring to here.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698