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
45function 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
72assertEquals(Infinity, Math.cosh(-Infinity));
73assertEquals(Infinity, Math.cosh(Infinity));
74assertEquals(Infinity, Math.cosh("-Infinity"));
75assertEquals(Infinity, Math.cosh("Infinity"));
76
77
78assertEquals(-Infinity, Math.atanh(-1));
79assertEquals(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
88assertEquals(0, Math.sinh(0));
89assertEquals(-Infinity, 1/Math.sinh(-0));
90assertEquals(1, Math.tanh(Infinity));
91assertEquals(-1, Math.tanh(-Infinity));
92assertEquals(1, Math.cosh(0));
93assertEquals(1, Math.cosh(-0));
94
95assertEquals(0, Math.acosh(1));
96assertEquals("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.
105assertEqualsDelta(74.20321057778875, Math.sinh(5), 1E-12);
106assertEqualsDelta(-74.20321057778875, Math.sinh(-5), 1E-12);
107
108assertEqualsDelta(1.1276259652063807, Math.cosh(0.5), 1E-12);
109assertEqualsDelta(74.20994852478785, Math.cosh(5), 1E-12);
110assertEqualsDelta(1.1276259652063807, Math.cosh(-0.5), 1E-12);
111assertEqualsDelta(74.20994852478785, Math.cosh(-5), 1E-12);
112
113assertEqualsDelta(0.4621171572600, Math.tanh(0.5), 1E-12);
114assertEqualsDelta(0.9999092042625, Math.tanh(5), 1E-12);
115assertEqualsDelta(-0.4621171572600, Math.tanh(-0.5), 1E-12);
116assertEqualsDelta(-0.9999092042625, Math.tanh(-5), 1E-12);
117
118assertEqualsDelta(0.4812118250596, Math.asinh(0.5), 1E-12);
119assertEqualsDelta(2.3124383412727, Math.asinh(5), 1E-12);
120assertEqualsDelta(-0.4812118250596, Math.asinh(-0.5), 1E-12);
121assertEqualsDelta(-2.3124383412727, Math.asinh(-5), 1E-12);
122
123assertEqualsDelta(0.9624236501192, Math.acosh(1.5), 1E-12);
124assertEqualsDelta(2.2924316695612, Math.acosh(5), 1E-12);
125assertEqualsDelta(0.4435682543851, Math.acosh(1.1), 1E-12);
126assertEqualsDelta(1.3169578969248, Math.acosh(2), 1E-12);
127
128assertEqualsDelta(0.5493061443341, Math.atanh(0.5), 1E-12);
129assertEqualsDelta(0.1003353477311, Math.atanh(0.1), 1E-12);
130assertEqualsDelta(-0.5493061443341, Math.atanh(-0.5), 1E-12);
131assertEqualsDelta(-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
144assertEquals(Math.pow(2, -29), Math.sinh(Math.pow(2, -29)));
145assertEquals(-Math.pow(2, -29), Math.sinh(-Math.pow(2, -29)));
146// Case |x| < 1
147assertEquals(0.5210953054937474, Math.sinh(0.5));
148assertEquals(-0.5210953054937474, Math.sinh(-0.5));
149// sinh(10*log(2)) = 1048575/2048, case |x| < 22
150assertEquals(1048575/2048, Math.sinh(10*Math.LN2));
151assertEquals(-1048575/2048, Math.sinh(-10*Math.LN2));
152// Case |x| < 22
153assertEquals(11013.232874703393, Math.sinh(10));
154assertEquals(-11013.232874703393, Math.sinh(-10));
155// Case |x| in [22, log(maxdouble)]
156assertEquals(2.1474836479999983e9, Math.sinh(32*Math.LN2));
157assertEquals(-2.1474836479999983e9, Math.sinh(-32*Math.LN2));
158// Case |x| in [22, log(maxdouble)]
159assertEquals(1.3440585709080678e43, Math.sinh(100));
160assertEquals(-1.3440585709080678e43, Math.sinh(-100));
161// No overflow, case |x| in [log(maxdouble), threshold]
162assertEquals(1.7976931348621744e308, Math.sinh(710.4758600739439));
163assertEquals(-1.7976931348621744e308, Math.sinh(-710.4758600739439));
164// Overflow, case |x| > threshold
165assertEquals(Infinity, Math.sinh(710.475860073944));
166assertEquals(-Infinity, Math.sinh(-710.475860073944));
167assertEquals(Infinity, Math.sinh(1000));
168assertEquals(-Infinity, Math.sinh(-1000));
169
170// Implementation-specific tests for cosh.
171// Case |x| < 2^-55
172assertEquals(1, Math.cosh(Math.pow(2, -56)));
173assertEquals(1, Math.cosh(-Math.pow(2, -56)));
174// Case |x| < 1/2*log(2). cosh(Math.LN2/4) = (sqrt(2)+1)/2^(5/4)
175assertEquals(1.0150517651282178, Math.cosh(Math.LN2/4));
176assertEquals(1.0150517651282178, Math.cosh(-Math.LN2/4));
177// Case 1/2*log(2) < |x| < 22. cosh(10*Math.LN2) = 1048577/2048
178assertEquals(512.00048828125, Math.cosh(10*Math.LN2));
179assertEquals(512.00048828125, Math.cosh(-10*Math.LN2));
180// Case 22 <= |x| < log(maxdouble)
181assertEquals(2.1474836479999983e9, Math.cosh(32*Math.LN2));
182assertEquals(2.1474836479999983e9, Math.cosh(-32*Math.LN2));
183// Case log(maxdouble) <= |x| <= overflowthreshold
184assertEquals(1.7976931348621744e308, Math.cosh(710.4758600739439));
185assertEquals(1.7976931348621744e308, Math.cosh(-710.4758600739439));
186// Overflow.
187assertEquals(Infinity, Math.cosh(710.475860073944));
188assertEquals(Infinity, Math.cosh(-710.475860073944));
189
190// Implementation-specific tests for tanh.
191// Case |x| < 2^-55
192var two_56 = Math.pow(2, -56);
193assertEquals(two_56, Math.tanh(two_56));
194assertEquals(-two_56, Math.tanh(-two_56));
195// Case |x| < 1
196assertEquals(0.6, Math.tanh(Math.LN2));
197assertEquals(-0.6, Math.tanh(-Math.LN2));
198// Case  1 < |x| < 22
199assertEquals(15/17, Math.tanh(2 * Math.LN2));
200assertEquals(-15/17, Math.tanh(-2 * Math.LN2));
201// Case |x| > 22
202assertEquals(1, Math.tanh(100));
203assertEquals(-1, Math.tanh(-100));
204// Test against overflow
205assertEquals(1, Math.tanh(1e300));
206assertEquals(-1, Math.tanh(-1e300));
207