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 assertTrue(isNaN(Math.log1p(NaN))); | 5 assertTrue(isNaN(Math.log1p(NaN))); |
6 assertTrue(isNaN(Math.log1p(function() {}))); | 6 assertTrue(isNaN(Math.log1p(function() {}))); |
7 assertTrue(isNaN(Math.log1p({ toString: function() { return NaN; } }))); | 7 assertTrue(isNaN(Math.log1p({ toString: function() { return NaN; } }))); |
8 assertTrue(isNaN(Math.log1p({ valueOf: function() { return "abc"; } }))); | 8 assertTrue(isNaN(Math.log1p({ valueOf: function() { return "abc"; } }))); |
9 assertEquals("Infinity", String(1/Math.log1p(0))); | 9 assertEquals(Infinity, 1/Math.log1p(0)); |
10 assertEquals("-Infinity", String(1/Math.log1p(-0))); | 10 assertEquals(-Infinity, 1/Math.log1p(-0)); |
11 assertEquals("Infinity", String(Math.log1p(Infinity))); | 11 assertEquals(Infinity, Math.log1p(Infinity)); |
12 assertEquals("-Infinity", String(Math.log1p(-1))); | 12 assertEquals(-Infinity, Math.log1p(-1)); |
13 assertTrue(isNaN(Math.log1p(-2))); | 13 assertTrue(isNaN(Math.log1p(-2))); |
14 assertTrue(isNaN(Math.log1p(-Infinity))); | 14 assertTrue(isNaN(Math.log1p(-Infinity))); |
15 | 15 |
16 for (var x = 1E300; x > 1E-1; x *= 0.8) { | 16 for (var x = 1E300; x > 1E16; x *= 0.8) { |
17 var expected = Math.log(x + 1); | 17 var expected = Math.log(x + 1); |
18 assertEqualsDelta(expected, Math.log1p(x), expected * 1E-14); | 18 assertEqualsDelta(expected, Math.log1p(x), expected * 1E-16); |
19 } | 19 } |
20 | 20 |
21 // Values close to 0: | 21 // Values close to 0: |
22 // Use Taylor expansion at 1 for log(x) as test expectation: | 22 // Use Taylor expansion at 1 for log(x) as test expectation: |
23 // log1p(x) == log(x + 1) == 0 + x / 1 - x^2 / 2 + x^3 / 3 - ... | 23 // log1p(x) == log(x + 1) == 0 + x / 1 - x^2 / 2 + x^3 / 3 - ... |
24 function log1p(x) { | 24 function log1p(x) { |
25 var terms = []; | 25 var terms = []; |
26 var prod = x; | 26 var prod = x; |
27 for (var i = 1; i <= 20; i++) { | 27 for (var i = 1; i <= 20; i++) { |
28 terms.push(prod / i); | 28 terms.push(prod / i); |
29 prod *= -x; | 29 prod *= -x; |
30 } | 30 } |
31 var sum = 0; | 31 var sum = 0; |
32 while (terms.length > 0) sum += terms.pop(); | 32 while (terms.length > 0) sum += terms.pop(); |
33 return sum; | 33 return sum; |
34 } | 34 } |
35 | 35 |
36 for (var x = 1E-1; x > 1E-300; x *= 0.8) { | 36 for (var x = 1E-1; x > 1E-300; x *= 0.8) { |
37 var expected = log1p(x); | 37 var expected = log1p(x); |
38 assertEqualsDelta(expected, Math.log1p(x), expected * 1E-14); | 38 assertEqualsDelta(expected, Math.log1p(x), expected * 1E-16); |
39 } | 39 } |
| 40 |
| 41 // Issue 3481. |
| 42 assertEquals(6.9756137364252422e-03, |
| 43 Math.log1p(8070450532247929/Math.pow(2,60))); |
| 44 |
| 45 // Tests related to the fdlibm implementation. |
| 46 // Test largest double value. |
| 47 assertEquals(709.782712893384, Math.log1p(1.7976931348623157e308)); |
| 48 // Test small values. |
| 49 assertEquals(Math.pow(2, -55), Math.log1p(Math.pow(2, -55))); |
| 50 assertEquals(9.313225741817976e-10, Math.log1p(Math.pow(2, -30))); |
| 51 // Cover various code paths. |
| 52 // -.2929 < x < .41422, k = 0 |
| 53 assertEquals(-0.2876820724517809, Math.log1p(-0.25)); |
| 54 assertEquals(0.22314355131420976, Math.log1p(0.25)); |
| 55 // 0.41422 < x < 9.007e15 |
| 56 assertEquals(2.3978952727983707, Math.log1p(10)); |
| 57 // x > 9.007e15 |
| 58 assertEquals(36.841361487904734, Math.log1p(10e15)); |
| 59 // Normalize u. |
| 60 assertEquals(37.08337388996168, Math.log1p(12738099905822720)); |
| 61 // Normalize u/2. |
| 62 assertEquals(37.08336444902049, Math.log1p(12737979646738432)); |
| 63 // |f| = 0, k != 0 |
| 64 assertEquals(1.3862943611198906, Math.log1p(3)); |
| 65 // |f| != 0, k != 0 |
| 66 assertEquals(1.3862945995384413, Math.log1p(3 + Math.pow(2,-20))); |
| 67 // final if-clause: k = 0 |
| 68 assertEquals(0.5596157879354227, Math.log1p(0.75)); |
| 69 // final if-clause: k != 0 |
| 70 assertEquals(0.8109302162163288, Math.log1p(1.25)); |
OLD | NEW |