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 // Flags: --harmony-maths 29 30 [Math.sinh, Math.cosh, Math.tanh, Math.asinh, Math.acosh, Math.atanh]. 31 forEach(function(fun) { 32 assertTrue(isNaN(fun(NaN))); 33 assertTrue(isNaN(fun("abc"))); 34 assertTrue(isNaN(fun({}))); 35 assertEquals(fun(0), fun([])); 36 assertTrue(isNaN(fun([1, 1]))); 37 assertEquals(fun(1.11), fun({ toString: function() { return "1.11"; } })); 38 assertEquals(fun(-3.1), fun({ toString: function() { return -3.1; } })); 39 assertEquals(fun(-1.1), fun({ valueOf: function() { return "-1.1"; } })); 40 assertEquals(fun(3.11), fun({ valueOf: function() { return 3.11; } })); 41 }); 42 43 44 function test_id(fun, rev, value) { 45 assertEqualsDelta(1, rev(fun(value))/value, 1E-7); 46 } 47 48 [Math.PI, 2, 5, 1E-5, 0.3].forEach(function(x) { 49 test_id(Math.sinh, Math.asinh, x); 50 test_id(Math.sinh, Math.asinh, -x); 51 test_id(Math.cosh, Math.acosh, x); 52 test_id(Math.tanh, Math.atanh, x); 53 test_id(Math.tanh, Math.atanh, -x); 54 }); 55 56 57 [Math.sinh, Math.asinh, Math.tanh, Math.atanh].forEach(function(fun) { 58 assertEquals("-Infinity", String(1/fun(-0))); 59 assertEquals("Infinity", String(1/fun(0))); 60 }); 61 62 63 [Math.sinh, Math.asinh].forEach(function(fun) { 64 assertEquals("-Infinity", String(fun(-Infinity))); 65 assertEquals("Infinity", String(fun(Infinity))); 66 assertEquals("-Infinity", String(fun("-Infinity"))); 67 assertEquals("Infinity", String(fun("Infinity"))); 68 }); 69 70 71 assertEquals("Infinity", String(Math.cosh(-Infinity))); 72 assertEquals("Infinity", String(Math.cosh(Infinity))); 73 assertEquals("Infinity", String(Math.cosh("-Infinity"))); 74 assertEquals("Infinity", String(Math.cosh("Infinity"))); 75 76 77 assertEquals("-Infinity", String(Math.atanh(-1))); 78 assertEquals("Infinity", String(Math.atanh(1))); 79 80 // Math.atanh(x) is NaN for |x| > 1 and NaN 81 [1.000000000001, Math.PI, 10000000, 2, Infinity, NaN].forEach(function(x) { 82 assertTrue(isNaN(Math.atanh(-x))); 83 assertTrue(isNaN(Math.atanh(x))); 84 }); 85 86 87 assertEquals(1, Math.tanh(Infinity)); 88 assertEquals(-1, Math.tanh(-Infinity)); 89 assertEquals(1, Math.cosh(0)); 90 assertEquals(1, Math.cosh(-0)); 91 92 assertEquals(0, Math.acosh(1)); 93 assertEquals("Infinity", String(Math.acosh(Infinity))); 94 95 // Math.acosh(x) is NaN for x < 1 96 [0.99999999999, 0.2, -1000, 0, -0].forEach(function(x) { 97 assertTrue(isNaN(Math.acosh(x))); 98 }); 99 100 101 // Some random samples. 102 assertEqualsDelta(0.5210953054937, Math.sinh(0.5), 1E-12); 103 assertEqualsDelta(74.203210577788, Math.sinh(5), 1E-12); 104 assertEqualsDelta(-0.5210953054937, Math.sinh(-0.5), 1E-12); 105 assertEqualsDelta(-74.203210577788, Math.sinh(-5), 1E-12); 106 107 assertEqualsDelta(1.1276259652063, Math.cosh(0.5), 1E-12); 108 assertEqualsDelta(74.209948524787, Math.cosh(5), 1E-12); 109 assertEqualsDelta(1.1276259652063, Math.cosh(-0.5), 1E-12); 110 assertEqualsDelta(74.209948524787, Math.cosh(-5), 1E-12); 111 112 assertEqualsDelta(0.4621171572600, Math.tanh(0.5), 1E-12); 113 assertEqualsDelta(0.9999092042625, Math.tanh(5), 1E-12); 114 assertEqualsDelta(-0.4621171572600, Math.tanh(-0.5), 1E-12); 115 assertEqualsDelta(-0.9999092042625, Math.tanh(-5), 1E-12); 116 117 assertEqualsDelta(0.4812118250596, Math.asinh(0.5), 1E-12); 118 assertEqualsDelta(2.3124383412727, Math.asinh(5), 1E-12); 119 assertEqualsDelta(-0.4812118250596, Math.asinh(-0.5), 1E-12); 120 assertEqualsDelta(-2.3124383412727, Math.asinh(-5), 1E-12); 121 122 assertEqualsDelta(0.9624236501192, Math.acosh(1.5), 1E-12); 123 assertEqualsDelta(2.2924316695612, Math.acosh(5), 1E-12); 124 assertEqualsDelta(0.4435682543851, Math.acosh(1.1), 1E-12); 125 assertEqualsDelta(1.3169578969248, Math.acosh(2), 1E-12); 126 127 assertEqualsDelta(0.5493061443341, Math.atanh(0.5), 1E-12); 128 assertEqualsDelta(0.1003353477311, Math.atanh(0.1), 1E-12); 129 assertEqualsDelta(-0.5493061443341, Math.atanh(-0.5), 1E-12); 130 assertEqualsDelta(-0.1003353477311, Math.atanh(-0.1), 1E-12); 131 132 [0, 1E-50, 1E-10, 1E10, 1E50, 1E100, 1E150].forEach(function(x) { 133 assertEqualsDelta(Math.asinh(x), -Math.asinh(-x), 1E-12); 134 }); 135 136 [1-(1E-16), 0, 1E-10, 1E-50].forEach(function(x) { 137 assertEqualsDelta(Math.atanh(x), -Math.atanh(-x), 1E-12); 138 }); 139