Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | // TODO(3468): we rely on a precise Math.exp. |
| 29 | // Flags: --no-fast-math |
| 30 | |
| 31 | [Math.sinh, Math.cosh, Math.tanh, Math.asinh, Math.acosh, Math.atanh]. |
| 32 | forEach(function(fun) { |
| 33 | assertTrue(isNaN(fun(NaN))); |
| 34 | assertTrue(isNaN(fun("abc"))); |
| 35 | assertTrue(isNaN(fun({}))); |
| 36 | assertEquals(fun(0), fun([])); |
| 37 | assertTrue(isNaN(fun([1, 1]))); |
| 38 | assertEquals(fun(1.11), fun({ toString: function() { return "1.11"; } })); |
| 39 | assertEquals(fun(-3.1), fun({ toString: function() { return -3.1; } })); |
| 40 | assertEquals(fun(-1.1), fun({ valueOf: function() { return "-1.1"; } })); |
| 41 | assertEquals(fun(3.11), fun({ valueOf: function() { return 3.11; } })); |
| 42 | }); |
| 43 | |
| 44 | |
| 45 | function test_id(fun, rev, value) { |
| 46 | assertEqualsDelta(1, rev(fun(value))/value, 1E-7); |
| 47 | } |
| 48 | |
| 49 | [Math.PI, 2, 5, 1E-5, 0.3].forEach(function(x) { |
| 50 | test_id(Math.sinh, Math.asinh, x); |
| 51 | test_id(Math.sinh, Math.asinh, -x); |
| 52 | test_id(Math.cosh, Math.acosh, x); |
| 53 | test_id(Math.tanh, Math.atanh, x); |
| 54 | test_id(Math.tanh, Math.atanh, -x); |
| 55 | }); |
| 56 | |
| 57 | |
| 58 | [Math.sinh, Math.asinh, Math.tanh, Math.atanh].forEach(function(fun) { |
| 59 | assertEquals("-Infinity", String(1/fun(-0))); |
| 60 | assertEquals("Infinity", String(1/fun(0))); |
| 61 | }); |
| 62 | |
| 63 | |
| 64 | [Math.sinh, Math.asinh].forEach(function(fun) { |
| 65 | assertEquals("-Infinity", String(fun(-Infinity))); |
| 66 | assertEquals("Infinity", String(fun(Infinity))); |
| 67 | assertEquals("-Infinity", String(fun("-Infinity"))); |
| 68 | assertEquals("Infinity", String(fun("Infinity"))); |
| 69 | }); |
| 70 | |
| 71 | |
| 72 | assertEquals(Infinity, Math.cosh(-Infinity)); |
| 73 | assertEquals(Infinity, Math.cosh(Infinity)); |
| 74 | assertEquals(Infinity, Math.cosh("-Infinity")); |
| 75 | assertEquals(Infinity, Math.cosh("Infinity")); |
| 76 | |
| 77 | |
| 78 | assertEquals(-Infinity, Math.atanh(-1)); |
| 79 | assertEquals(Infinity, Math.atanh(1)); |
| 80 | |
| 81 | // Math.atanh(x) is NaN for |x| > 1 and NaN |
| 82 | [1.000000000001, Math.PI, 10000000, 2, Infinity, NaN].forEach(function(x) { |
| 83 | assertTrue(isNaN(Math.atanh(-x))); |
| 84 | assertTrue(isNaN(Math.atanh(x))); |
| 85 | }); |
| 86 | |
| 87 | |
| 88 | assertEquals(0, Math.sinh(0)); |
| 89 | assertEquals(-Infinity, 1/Math.sinh(-0)); |
| 90 | assertEquals(1, Math.tanh(Infinity)); |
| 91 | assertEquals(-1, Math.tanh(-Infinity)); |
| 92 | assertEquals(1, Math.cosh(0)); |
| 93 | assertEquals(1, Math.cosh(-0)); |
| 94 | |
| 95 | assertEquals(0, Math.acosh(1)); |
| 96 | assertEquals("Infinity", String(Math.acosh(Infinity))); |
| 97 | |
| 98 | // Math.acosh(x) is NaN for x < 1 |
| 99 | [0.99999999999, 0.2, -1000, 0, -0].forEach(function(x) { |
| 100 | assertTrue(isNaN(Math.acosh(x))); |
| 101 | }); |
| 102 | |
| 103 | |
| 104 | // Some random samples. |
| 105 | assertEqualsDelta(74.20321057778875, Math.sinh(5), 1E-12); |
| 106 | assertEqualsDelta(-74.20321057778875, Math.sinh(-5), 1E-12); |
| 107 | |
| 108 | assertEqualsDelta(1.1276259652063807, Math.cosh(0.5), 1E-12); |
| 109 | assertEqualsDelta(74.20994852478785, Math.cosh(5), 1E-12); |
| 110 | assertEqualsDelta(1.1276259652063807, Math.cosh(-0.5), 1E-12); |
| 111 | assertEqualsDelta(74.20994852478785, Math.cosh(-5), 1E-12); |
| 112 | |
| 113 | assertEqualsDelta(0.4621171572600, Math.tanh(0.5), 1E-12); |
| 114 | assertEqualsDelta(0.9999092042625, Math.tanh(5), 1E-12); |
| 115 | assertEqualsDelta(-0.4621171572600, Math.tanh(-0.5), 1E-12); |
| 116 | assertEqualsDelta(-0.9999092042625, Math.tanh(-5), 1E-12); |
| 117 | |
| 118 | assertEqualsDelta(0.4812118250596, Math.asinh(0.5), 1E-12); |
| 119 | assertEqualsDelta(2.3124383412727, Math.asinh(5), 1E-12); |
| 120 | assertEqualsDelta(-0.4812118250596, Math.asinh(-0.5), 1E-12); |
| 121 | assertEqualsDelta(-2.3124383412727, Math.asinh(-5), 1E-12); |
| 122 | |
| 123 | assertEqualsDelta(0.9624236501192, Math.acosh(1.5), 1E-12); |
| 124 | assertEqualsDelta(2.2924316695612, Math.acosh(5), 1E-12); |
| 125 | assertEqualsDelta(0.4435682543851, Math.acosh(1.1), 1E-12); |
| 126 | assertEqualsDelta(1.3169578969248, Math.acosh(2), 1E-12); |
| 127 | |
| 128 | assertEqualsDelta(0.5493061443341, Math.atanh(0.5), 1E-12); |
| 129 | assertEqualsDelta(0.1003353477311, Math.atanh(0.1), 1E-12); |
| 130 | assertEqualsDelta(-0.5493061443341, Math.atanh(-0.5), 1E-12); |
| 131 | assertEqualsDelta(-0.1003353477311, Math.atanh(-0.1), 1E-12); |
| 132 | |
| 133 | [0, 1E-50, 1E-10, 1E10, 1E50, 1E100, 1E150].forEach(function(x) { |
| 134 | assertEqualsDelta(Math.asinh(x), -Math.asinh(-x), 1E-12); |
| 135 | }); |
| 136 | |
| 137 | [1-(1E-16), 0, 1E-10, 1E-50].forEach(function(x) { |
| 138 | assertEqualsDelta(Math.atanh(x), -Math.atanh(-x), 1E-12); |
| 139 | }); |
| 140 | |
| 141 | |
| 142 | // Implementation-specific tests for sinh. |
| 143 | // Case |x| < 2^-28 |
| 144 | assertEquals(Math.pow(2, -29), Math.sinh(Math.pow(2, -29))); |
| 145 | assertEquals(-Math.pow(2, -29), Math.sinh(-Math.pow(2, -29))); |
| 146 | // Case |x| < 1 |
| 147 | assertEquals(0.5210953054937474, Math.sinh(0.5)); |
| 148 | assertEquals(-0.5210953054937474, Math.sinh(-0.5)); |
| 149 | // sinh(10*log(2)) = 1048575/2048, case |x| < 22 |
| 150 | assertEquals(1048575/2048, Math.sinh(10*Math.LN2)); |
| 151 | assertEquals(-1048575/2048, Math.sinh(-10*Math.LN2)); |
| 152 | // Case |x| < 22 |
| 153 | assertEquals(11013.232874703393, Math.sinh(10)); |
| 154 | assertEquals(-11013.232874703393, Math.sinh(-10)); |
| 155 | // Case |x| in [22, log(maxdouble)] |
| 156 | assertEquals(2.1474836479999983e9, Math.sinh(32*Math.LN2)); |
| 157 | assertEquals(-2.1474836479999983e9, Math.sinh(-32*Math.LN2)); |
| 158 | // Case |x| in [22, log(maxdouble)] |
| 159 | assertEquals(1.3440585709080678e43, Math.sinh(100)); |
| 160 | assertEquals(-1.3440585709080678e43, Math.sinh(-100)); |
| 161 | // No overflow, case |x| in [log(maxdouble), threshold] |
| 162 | assertEquals(1.7976931348621744e308, Math.sinh(710.4758600739439)); |
| 163 | assertEquals(-1.7976931348621744e308, Math.sinh(-710.4758600739439)); |
| 164 | // Overflow, case |x| > threshold |
| 165 | assertEquals(Infinity, Math.sinh(710.475860073944)); |
| 166 | assertEquals(-Infinity, Math.sinh(-710.475860073944)); |
| 167 | assertEquals(Infinity, Math.sinh(1000)); |
| 168 | assertEquals(-Infinity, Math.sinh(-1000)); |
| 169 | |
| 170 | // Implementation-specific tests for cosh. |
| 171 | // Case |x| < 2^-55 |
| 172 | assertEquals(1, Math.cosh(Math.pow(2, -56))); |
| 173 | assertEquals(1, Math.cosh(-Math.pow(2, -56))); |
| 174 | // Case |x| < 1/2*log(2). cosh(Math.LN2/4) = (sqrt(2)+1)/2^(5/4) |
| 175 | assertEquals(1.0150517651282178, Math.cosh(Math.LN2/4)); |
| 176 | assertEquals(1.0150517651282178, Math.cosh(-Math.LN2/4)); |
| 177 | // Case 1/2*log(2) < |x| < 22. cosh(10*Math.LN2) = 1048577/2048 |
| 178 | assertEquals(512.00048828125, Math.cosh(10*Math.LN2)); |
| 179 | assertEquals(512.00048828125, Math.cosh(-10*Math.LN2)); |
| 180 | // Case 22 <= |x| < log(maxdouble) |
| 181 | assertEquals(2.1474836479999983e9, Math.cosh(32*Math.LN2)); |
| 182 | assertEquals(2.1474836479999983e9, Math.cosh(-32*Math.LN2)); |
| 183 | // Case log(maxdouble) <= |x| <= overflowthreshold |
| 184 | assertEquals(1.7976931348621744e308, Math.cosh(710.4758600739439)); |
| 185 | assertEquals(1.7976931348621744e308, Math.cosh(-710.4758600739439)); |
| 186 | // Overflow. |
| 187 | assertEquals(Infinity, Math.cosh(710.475860073944)); |
| 188 | assertEquals(Infinity, Math.cosh(-710.475860073944)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 189 | |
| 190 | // Implementation-specific tests for tanh. |
| 191 | // Case |x| < 2^-55 |
| 192 | var two_56 = Math.pow(2, -56); |
| 193 | assertEquals(two_56, Math.tanh(two_56)); |
| 194 | assertEquals(-two_56, Math.tanh(-two_56)); |
| 195 | // Case |x| < 1 |
| 196 | assertEquals(0.6, Math.tanh(Math.LN2)); |
| 197 | assertEquals(-0.6, Math.tanh(-Math.LN2)); |
| 198 | // Case 1 < |x| < 22 |
| 199 | assertEquals(15/17, Math.tanh(2 * Math.LN2)); |
| 200 | assertEquals(-15/17, Math.tanh(-2 * Math.LN2)); |
| 201 | // Case |x| > 22 |
| 202 | assertEquals(1, Math.tanh(100)); |
| 203 | assertEquals(-1, Math.tanh(-100)); |
| 204 | // Test against overflow |
| 205 | assertEquals(1, Math.tanh(1e300)); |
| 206 | assertEquals(-1, Math.tanh(-1e300)); |