math-log1p.js 2.42 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

assertTrue(isNaN(Math.log1p(NaN)));
assertTrue(isNaN(Math.log1p(function() {})));
assertTrue(isNaN(Math.log1p({ toString: function() { return NaN; } })));
assertTrue(isNaN(Math.log1p({ valueOf: function() { return "abc"; } })));
9 10 11 12
assertEquals(Infinity, 1/Math.log1p(0));
assertEquals(-Infinity, 1/Math.log1p(-0));
assertEquals(Infinity, Math.log1p(Infinity));
assertEquals(-Infinity, Math.log1p(-1));
13 14 15
assertTrue(isNaN(Math.log1p(-2)));
assertTrue(isNaN(Math.log1p(-Infinity)));

16
for (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 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
}

// Values close to 0:
// Use Taylor expansion at 1 for log(x) as test expectation:
// log1p(x) == log(x + 1) == 0 + x / 1 - x^2 / 2 + x^3 / 3 - ...
function log1p(x) {
  var terms = [];
  var prod = x;
  for (var i = 1; i <= 20; i++) {
    terms.push(prod / i);
    prod *= -x;
  }
  var sum = 0;
  while (terms.length > 0) sum += terms.pop();
  return sum;
}

for (var x = 1E-1; x > 1E-300; x *= 0.8) {
  var expected = log1p(x);
38
  assertEqualsDelta(expected, Math.log1p(x), expected * 1E-16);
39
}
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

// Issue 3481.
assertEquals(6.9756137364252422e-03,
             Math.log1p(8070450532247929/Math.pow(2,60)));

// Tests related to the fdlibm implementation.
// Test largest double value.
assertEquals(709.782712893384, Math.log1p(1.7976931348623157e308));
// Test small values.
assertEquals(Math.pow(2, -55), Math.log1p(Math.pow(2, -55)));
assertEquals(9.313225741817976e-10, Math.log1p(Math.pow(2, -30)));
// Cover various code paths.
// -.2929 < x < .41422, k = 0
assertEquals(-0.2876820724517809, Math.log1p(-0.25));
assertEquals(0.22314355131420976, Math.log1p(0.25));
// 0.41422 < x < 9.007e15
assertEquals(2.3978952727983707, Math.log1p(10));
// x > 9.007e15
assertEquals(36.841361487904734, Math.log1p(10e15));
// Normalize u.
assertEquals(37.08337388996168, Math.log1p(12738099905822720));
// Normalize u/2.
assertEquals(37.08336444902049, Math.log1p(12737979646738432));
// |f| = 0, k != 0
assertEquals(1.3862943611198906, Math.log1p(3));
// |f| != 0, k != 0
assertEquals(1.3862945995384413, Math.log1p(3 + Math.pow(2,-20)));
// final if-clause: k = 0
assertEquals(0.5596157879354227, Math.log1p(0.75));
// final if-clause: k != 0
assertEquals(0.8109302162163288, Math.log1p(1.25));