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