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: --harmony-maths 6 7 assertTrue(isNaN(Math.log1p(NaN))); 8 assertTrue(isNaN(Math.log1p(function() {}))); 9 assertTrue(isNaN(Math.log1p({ toString: function() { return NaN; } }))); 10 assertTrue(isNaN(Math.log1p({ valueOf: function() { return "abc"; } }))); 11 assertEquals("Infinity", String(1/Math.log1p(0))); 12 assertEquals("-Infinity", String(1/Math.log1p(-0))); 13 assertEquals("Infinity", String(Math.log1p(Infinity))); 14 assertEquals("-Infinity", String(Math.log1p(-1))); 15 assertTrue(isNaN(Math.log1p(-2))); 16 assertTrue(isNaN(Math.log1p(-Infinity))); 17 18 for (var x = 1E300; x > 1E-1; x *= 0.8) { 19 var expected = Math.log(x + 1); 20 assertEqualsDelta(expected, Math.log1p(x), expected * 1E-14); 21 } 22 23 // Values close to 0: 24 // Use Taylor expansion at 1 for log(x) as test expectation: 25 // log1p(x) == log(x + 1) == 0 + x / 1 - x^2 / 2 + x^3 / 3 - ... 26 function log1p(x) { 27 var terms = []; 28 var prod = x; 29 for (var i = 1; i <= 20; i++) { 30 terms.push(prod / i); 31 prod *= -x; 32 } 33 var sum = 0; 34 while (terms.length > 0) sum += terms.pop(); 35 return sum; 36 } 37 38 for (var x = 1E-1; x > 1E-300; x *= 0.8) { 39 var expected = log1p(x); 40 assertEqualsDelta(expected, Math.log1p(x), expected * 1E-14); 41 } 42