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