1 // 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 7 assertTrue(isNaN(Math.expm1(NaN))); 8 assertTrue(isNaN(Math.expm1(function() {}))); 9 assertTrue(isNaN(Math.expm1({ toString: function() { return NaN; } }))); 10 assertTrue(isNaN(Math.expm1({ valueOf: function() { return "abc"; } }))); 11 assertEquals(Infinity, 1/Math.expm1(0)); 12 assertEquals(-Infinity, 1/Math.expm1(-0)); 13 assertEquals(Infinity, Math.expm1(Infinity)); 14 assertEquals(-1, Math.expm1(-Infinity)); 15 16 17 // Sanity check: 18 // Math.expm1(x) stays reasonably close to Math.exp(x) - 1 for large values. 19 for (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 + ... 30 function 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. 40 for (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. 48 assertEquals(Infinity, Math.expm1(709.8)); 49 // Test largest double value. 50 assertEquals(Infinity, Math.exp(1.7976931348623157e308)); 51 // Cover various code paths. 52 assertEquals(-1, Math.expm1(-56 * Math.LN2)); 53 assertEquals(-1, Math.expm1(-50)); 54 // Test most negative double value. 55 assertEquals(-1, Math.expm1(-1.7976931348623157e308)); 56 // Test argument reduction. 57 // Cases for 0.5*log(2) < |x| < 1.5*log(2). 58 assertEquals(Math.E - 1, Math.expm1(1)); 59 assertEquals(1/Math.E - 1, Math.expm1(-1)); 60 // Cases for 1.5*log(2) < |x|. 61 assertEquals(6.38905609893065, Math.expm1(2)); 62 assertEquals(-0.8646647167633873, Math.expm1(-2)); 63 // Cases where Math.expm1(x) = x. 64 assertEquals(0, Math.expm1(0)); 65 assertEquals(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. 68 assertEquals(0.18920711500272105, Math.expm1(0.25 * Math.LN2)); 69 // Test branch for k = -1. 70 assertEquals(-0.5, Math.expm1(-Math.LN2)); 71 // Test branch for k = 1. 72 assertEquals(1, Math.expm1(Math.LN2)); 73 // Test branch for k <= -2 || k > 56. k = -3. 74 assertEquals(1.4411518807585582e17, Math.expm1(57 * Math.LN2)); 75 // Test last branch for k < 20, k = 19. 76 assertEquals(524286.99999999994, Math.expm1(19 * Math.LN2)); 77 // Test the else branch, k = 20. 78 assertEquals(1048575, Math.expm1(20 * Math.LN2)); 79