blob: 7cbb1b485f241521e5428d3e11339a5b55a4adb4 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Flags: --no-fast-math
6
7assertTrue(isNaN(Math.expm1(NaN)));
8assertTrue(isNaN(Math.expm1(function() {})));
9assertTrue(isNaN(Math.expm1({ toString: function() { return NaN; } })));
10assertTrue(isNaN(Math.expm1({ valueOf: function() { return "abc"; } })));
11assertEquals(Infinity, 1/Math.expm1(0));
12assertEquals(-Infinity, 1/Math.expm1(-0));
13assertEquals(Infinity, Math.expm1(Infinity));
14assertEquals(-1, Math.expm1(-Infinity));
15
16
17// Sanity check:
18// Math.expm1(x) stays reasonably close to Math.exp(x) - 1 for large values.
19for (var x = 1; x < 700; x += 0.25) {
20 var expected = Math.exp(x) - 1;
21 assertEqualsDelta(expected, Math.expm1(x), expected * 1E-15);
22 expected = Math.exp(-x) - 1;
23 assertEqualsDelta(expected, Math.expm1(-x), -expected * 1E-15);
24}
25
26// Approximation for values close to 0:
27// Use six terms of Taylor expansion at 0 for exp(x) as test expectation:
28// exp(x) - 1 == exp(0) + exp(0) * x + x * x / 2 + ... - 1
29// == x + x * x / 2 + x * x * x / 6 + ...
30function expm1(x) {
31 return x * (1 + x * (1/2 + x * (
32 1/6 + x * (1/24 + x * (
33 1/120 + x * (1/720 + x * (
34 1/5040 + x * (1/40320 + x*(
35 1/362880 + x * (1/3628800))))))))));
36}
37
38// Sanity check:
39// Math.expm1(x) stays reasonabliy close to the Taylor series for small values.
40for (var x = 1E-1; x > 1E-300; x *= 0.8) {
41 var expected = expm1(x);
42 assertEqualsDelta(expected, Math.expm1(x), expected * 1E-15);
43}
44
45
46// Tests related to the fdlibm implementation.
47// Test overflow.
48assertEquals(Infinity, Math.expm1(709.8));
49// Test largest double value.
50assertEquals(Infinity, Math.exp(1.7976931348623157e308));
51// Cover various code paths.
52assertEquals(-1, Math.expm1(-56 * Math.LN2));
53assertEquals(-1, Math.expm1(-50));
54// Test most negative double value.
55assertEquals(-1, Math.expm1(-1.7976931348623157e308));
56// Test argument reduction.
57// Cases for 0.5*log(2) < |x| < 1.5*log(2).
58assertEquals(Math.E - 1, Math.expm1(1));
59assertEquals(1/Math.E - 1, Math.expm1(-1));
60// Cases for 1.5*log(2) < |x|.
61assertEquals(6.38905609893065, Math.expm1(2));
62assertEquals(-0.8646647167633873, Math.expm1(-2));
63// Cases where Math.expm1(x) = x.
64assertEquals(0, Math.expm1(0));
65assertEquals(Math.pow(2,-55), Math.expm1(Math.pow(2,-55)));
66// Tests for the case where argument reduction has x in the primary range.
67// Test branch for k = 0.
68assertEquals(0.18920711500272105, Math.expm1(0.25 * Math.LN2));
69// Test branch for k = -1.
70assertEquals(-0.5, Math.expm1(-Math.LN2));
71// Test branch for k = 1.
72assertEquals(1, Math.expm1(Math.LN2));
73// Test branch for k <= -2 || k > 56. k = -3.
74assertEquals(1.4411518807585582e17, Math.expm1(57 * Math.LN2));
75// Test last branch for k < 20, k = 19.
76assertEquals(524286.99999999994, Math.expm1(19 * Math.LN2));
77// Test the else branch, k = 20.
78assertEquals(1048575, Math.expm1(20 * Math.LN2));