Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 // We sum up in the reverse order for higher precision, as we expect the terms | |
| 60 // to grow smaller for x reasonably close to 0. | |
| 61 function precision_sum(array) { | |
| 62 var result = 0; | |
| 63 while (array.length > 0) { | |
| 64 var e = array.pop(); | |
| 65 print(e) | |
|
Jakob Kummerow
2013/10/30 08:04:44
nit: debugging leftover
| |
| 66 result += e; | |
| 67 } | |
| 68 return result; | |
| 69 } | |
| 70 | |
| 71 function sin(x) { | |
| 72 var sign = 1; | |
| 73 var x2 = x*x; | |
| 74 var terms = []; | |
| 75 for (var i = 1; i < accuracy; i += 2) { | |
| 76 terms.push(sign * x / factorial[i]); | |
| 77 x *= x2; | |
| 78 sign *= -1; | |
| 79 } | |
| 80 return precision_sum(terms); | |
| 81 } | |
| 82 | |
| 83 function cos(x) { | |
| 84 var sign = -1; | |
| 85 var x2 = x*x; | |
| 86 x = x2; | |
| 87 var terms = [1]; | |
| 88 for (var i = 2; i < accuracy; i += 2) { | |
| 89 terms.push(sign * x / factorial[i]); | |
| 90 x *= x2; | |
| 91 sign *= -1; | |
| 92 } | |
| 93 return precision_sum(terms); | |
| 94 } | |
| 95 | |
| 96 function abs_error(fun, ref, x) { | |
| 97 return Math.abs(ref(x) - fun(x)); | |
| 98 } | |
| 99 | |
| 100 var test_inputs = []; | |
| 101 for (var i = -10000; i < 10000; i += 177) test_inputs.push(i/1257); | |
| 102 var epsilon = 0.000001; | |
| 103 | |
| 104 test_inputs.push(0); | |
| 105 test_inputs.push(0 + epsilon); | |
| 106 test_inputs.push(0 - epsilon); | |
| 107 test_inputs.push(Math.PI/2); | |
| 108 test_inputs.push(Math.PI/2 + epsilon); | |
| 109 test_inputs.push(Math.PI/2 - epsilon); | |
| 110 test_inputs.push(Math.PI); | |
| 111 test_inputs.push(Math.PI + epsilon); | |
| 112 test_inputs.push(Math.PI - epsilon); | |
| 113 test_inputs.push(- 2*Math.PI); | |
| 114 test_inputs.push(- 2*Math.PI + epsilon); | |
| 115 test_inputs.push(- 2*Math.PI - epsilon); | |
| 116 | |
| 117 var squares = []; | |
| 118 for (var i = 0; i < test_inputs.length; i++) { | |
| 119 var x = test_inputs[i]; | |
| 120 var err_sin = abs_error(Math.sin, sin, x); | |
| 121 var err_cos = abs_error(Math.cos, cos, x) | |
| 122 assertTrue(err_sin < 1E-13); | |
| 123 assertTrue(err_cos < 1E-13); | |
| 124 squares.push(err_sin*err_sin + err_cos*err_cos); | |
| 125 } | |
| 126 | |
| 127 // Sum squares up by adding them pairwise, to avoid losing precision. | |
| 128 while (squares.length > 1) { | |
| 129 var reduced = []; | |
| 130 if (squares.length % 2 == 1) reduced.push(squares.pop()); | |
| 131 // Remaining number of elements is even. | |
| 132 while(squares.length > 1) reduced.push(squares.pop() + squares.pop()); | |
| 133 squares = reduced; | |
| 134 } | |
| 135 | |
| 136 var err_rms = Math.sqrt(squares[0] / test_inputs.length / 2); | |
| 137 assertTrue(err_rms < 1E-14); | |
| 138 | |
| 139 assertEquals(-1, Math.cos({ valueOf: function() { return Math.PI; } })); | |
| 140 assertEquals(0, Math.sin("0x00000")); | |
| 141 assertTrue(isNaN(Math.sin(Infinity))); | |
| 142 assertTrue(isNaN(Math.cos("-Infinity"))); | |
| 143 assertEquals("Infinity", String(Math.tan(Math.PI/2))); | |
| 144 assertEquals("-Infinity", String(Math.tan(-Math.PI/2))); | |
| OLD | NEW |