math-log2-log10.js 3.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28 29
// Flags: --allow-natives-syntax

30 31 32 33 34 35 36 37 38 39 40 41 42 43
[Math.log10, Math.log2].forEach( function(fun) {
  assertTrue(isNaN(fun(NaN)));
  assertTrue(isNaN(fun(fun)));
  assertTrue(isNaN(fun({ toString: function() { return NaN; } })));
  assertTrue(isNaN(fun({ valueOf: function() { return -1; } })));
  assertTrue(isNaN(fun({ valueOf: function() { return "abc"; } })));
  assertTrue(isNaN(fun(-0.1)));
  assertTrue(isNaN(fun(-1)));
  assertEquals("-Infinity", String(fun(0)));
  assertEquals("-Infinity", String(fun(-0)));
  assertEquals(0, fun(1));
  assertEquals("Infinity", String(fun(Infinity)));
});

44 45
for (var i = -310; i <= 308; i += 0.5) {
  assertEquals(i, Math.log10(Math.pow(10, i)));
46
  // Square roots are tested below.
47 48 49
  if (i != -0.5 && i != 0.5 ) {
    assertEqualsDelta(i, Math.log2(Math.pow(2, i)), Number.EPSILON);
  }
50
}
51 52 53

// Test denormals.
assertEquals(-307.77759430519706, Math.log10(1.5 * Math.pow(2, -1023)));
54

55 56 57
// Issue 4025.  Remove delta once issue 4029 has been fixed.
assertEqualsDelta(-9.643274665532873e-17, Math.log10(1-Number.EPSILON), 3e-32);

58 59 60 61
// Test Math.log2(2^k) for -1074 <= k <= 1023.
var n = -1074;
// This loop covers n from -1074 to -1043
for (var lowbits = 1; lowbits <= 0x80000000; lowbits *= 2) {
62
  var x = %ConstructDouble(0, lowbits);
63 64 65 66 67
  assertEquals(n, Math.log2(x));
  n++;
}
// This loop covers n from -1042 to -1023
for (var hibits = 1; hibits <= 0x80000; hibits *= 2) {
68
  var x = %ConstructDouble(hibits, 0);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  assertEquals(n, Math.log2(x));
  n++;
}
// The rest of the normal values of 2^n
var x = 1;
for (var n = -1022; n <= 1023; ++n) {
  var x = Math.pow(2, n);
  assertEquals(n, Math.log2(x));
}

// Test special values.
// Expectation isn't exactly 1/2 because Math.SQRT2 isn't exactly sqrt(2).
assertEquals(0.5000000000000001, Math.log2(Math.SQRT2));

// Expectation isn't exactly -1/2 because Math.SQRT1_2 isn't exactly sqrt(1/2).
assertEquals(-0.4999999999999999, Math.log2(Math.SQRT1_2));

assertEquals(3.321928094887362, Math.log2(10));
assertEquals(6.643856189774724, Math.log2(100));

// Test relationships
x = 1;
for (var k = 0; k < 1000; ++k) {
  var y = Math.abs(Math.log2(x) + Math.log2(1/x));
  assertEqualsDelta(0, y, 1.5e-14);
  x *= 1.1;
}

x = Math.pow(2, -100);
for (var k = 0; k < 1000; ++k) {
  var y = Math.log2(x);
  var expected = Math.log(x) / Math.LN2;
  var err = Math.abs(y - expected) / expected;
  assertEqualsDelta(0, err, 1e-15);
  x *= 1.1;
}