Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Flags: --no-fast-math | 5 // Flags: --no-fast-math |
| 6 | 6 |
| 7 assertTrue(isNaN(Math.expm1(NaN))); | 7 assertTrue(isNaN(Math.expm1(NaN))); |
| 8 assertTrue(isNaN(Math.expm1(function() {}))); | 8 assertTrue(isNaN(Math.expm1(function() {}))); |
| 9 assertTrue(isNaN(Math.expm1({ toString: function() { return NaN; } }))); | 9 assertTrue(isNaN(Math.expm1({ toString: function() { return NaN; } }))); |
| 10 assertTrue(isNaN(Math.expm1({ valueOf: function() { return "abc"; } }))); | 10 assertTrue(isNaN(Math.expm1({ valueOf: function() { return "abc"; } }))); |
| 11 assertEquals("Infinity", String(1/Math.expm1(0))); | 11 assertEquals(Infinity, 1/Math.expm1(0)); |
| 12 assertEquals("-Infinity", String(1/Math.expm1(-0))); | 12 assertEquals(-Infinity, 1/Math.expm1(-0)); |
| 13 assertEquals("Infinity", String(Math.expm1(Infinity))); | 13 assertEquals(Infinity, Math.expm1(Infinity)); |
| 14 assertEquals(-1, Math.expm1(-Infinity)); | 14 assertEquals(-1, Math.expm1(-Infinity)); |
| 15 | 15 |
| 16 for (var x = 0.1; x < 700; x += 0.1) { | 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) { | |
| 17 var expected = Math.exp(x) - 1; | 20 var expected = Math.exp(x) - 1; |
| 18 assertEqualsDelta(expected, Math.expm1(x), expected * 1E-14); | 21 assertEqualsDelta(expected, Math.expm1(x), expected * 1E-15); |
| 19 expected = Math.exp(-x) - 1; | 22 expected = Math.exp(-x) - 1; |
| 20 assertEqualsDelta(expected, Math.expm1(-x), -expected * 1E-14); | 23 assertEqualsDelta(expected, Math.expm1(-x), -expected * 1E-15); |
| 21 } | 24 } |
|
Raymond Toy
2014/08/13 16:25:17
For an additional sanity check, why not use the ma
Yang
2014/08/20 14:18:24
I like the suggestion. However, n*Math.LN2 is less
Raymond Toy
2014/08/20 16:18:20
It has the great advantage that the test is indepe
| |
| 22 | 25 |
| 23 // Values close to 0: | 26 // Approximation for values close to 0: |
| 24 // Use six terms of Taylor expansion at 0 for exp(x) as test expectation: | 27 // Use six terms of Taylor expansion at 0 for exp(x) as test expectation: |
| 25 // exp(x) - 1 == exp(0) + exp(0) * x + x * x / 2 + ... - 1 | 28 // exp(x) - 1 == exp(0) + exp(0) * x + x * x / 2 + ... - 1 |
| 26 // == x + x * x / 2 + x * x * x / 6 + ... | 29 // == x + x * x / 2 + x * x * x / 6 + ... |
| 27 function expm1(x) { | 30 function expm1(x) { |
| 28 return x * (1 + x * (1/2 + x * ( | 31 return x * (1 + x * (1/2 + x * ( |
| 29 1/6 + x * (1/24 + x * ( | 32 1/6 + x * (1/24 + x * ( |
| 30 1/120 + x * (1/720 + x * ( | 33 1/120 + x * (1/720 + x * ( |
| 31 1/5040 + x * (1/40320 + x*( | 34 1/5040 + x * (1/40320 + x*( |
| 32 1/362880 + x * (1/3628800)))))))))); | 35 1/362880 + x * (1/3628800)))))))))); |
| 33 } | 36 } |
| 34 | 37 |
| 38 // Sanity check: | |
| 39 // Math.expm1(x) stays reasonabliy close to the Taylor series for small values. | |
| 35 for (var x = 1E-1; x > 1E-300; x *= 0.8) { | 40 for (var x = 1E-1; x > 1E-300; x *= 0.8) { |
| 36 var expected = expm1(x); | 41 var expected = expm1(x); |
| 37 assertEqualsDelta(expected, Math.expm1(x), expected * 1E-14); | 42 assertEqualsDelta(expected, Math.expm1(x), expected * 1E-15); |
| 38 } | 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)); | |
| OLD | NEW |