• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2013 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28// Flags: --allow-natives-syntax
29
30[Math.log10, Math.log2].forEach( function(fun) {
31  assertTrue(isNaN(fun(NaN)));
32  assertTrue(isNaN(fun(fun)));
33  assertTrue(isNaN(fun({ toString: function() { return NaN; } })));
34  assertTrue(isNaN(fun({ valueOf: function() { return -1; } })));
35  assertTrue(isNaN(fun({ valueOf: function() { return "abc"; } })));
36  assertTrue(isNaN(fun(-0.1)));
37  assertTrue(isNaN(fun(-1)));
38  assertEquals("-Infinity", String(fun(0)));
39  assertEquals("-Infinity", String(fun(-0)));
40  assertEquals(0, fun(1));
41  assertEquals("Infinity", String(fun(Infinity)));
42});
43
44for (var i = -310; i <= 308; i += 0.5) {
45  assertEquals(i, Math.log10(Math.pow(10, i)));
46  // Square roots are tested below.
47  if (i != -0.5 && i != 0.5) assertEquals(i, Math.log2(Math.pow(2, i)));
48}
49
50// Test denormals.
51assertEquals(-307.77759430519706, Math.log10(1.5 * Math.pow(2, -1023)));
52
53// Issue 4025.  Remove delta once issue 4029 has been fixed.
54assertEqualsDelta(-9.643274665532873e-17, Math.log10(1-Number.EPSILON), 3e-32);
55
56// Test Math.log2(2^k) for -1074 <= k <= 1023.
57var n = -1074;
58// This loop covers n from -1074 to -1043
59for (var lowbits = 1; lowbits <= 0x80000000; lowbits *= 2) {
60  var x = %ConstructDouble(0, lowbits);
61  assertEquals(n, Math.log2(x));
62  n++;
63}
64// This loop covers n from -1042 to -1023
65for (var hibits = 1; hibits <= 0x80000; hibits *= 2) {
66  var x = %ConstructDouble(hibits, 0);
67  assertEquals(n, Math.log2(x));
68  n++;
69}
70// The rest of the normal values of 2^n
71var x = 1;
72for (var n = -1022; n <= 1023; ++n) {
73  var x = Math.pow(2, n);
74  assertEquals(n, Math.log2(x));
75}
76
77// Test special values.
78// Expectation isn't exactly 1/2 because Math.SQRT2 isn't exactly sqrt(2).
79assertEquals(0.5000000000000001, Math.log2(Math.SQRT2));
80
81// Expectation isn't exactly -1/2 because Math.SQRT1_2 isn't exactly sqrt(1/2).
82assertEquals(-0.4999999999999999, Math.log2(Math.SQRT1_2));
83
84assertEquals(3.321928094887362, Math.log2(10));
85assertEquals(6.643856189774724, Math.log2(100));
86
87// Test relationships
88x = 1;
89for (var k = 0; k < 1000; ++k) {
90  var y = Math.abs(Math.log2(x) + Math.log2(1/x));
91  assertEqualsDelta(0, y, 1.5e-14);
92  x *= 1.1;
93}
94
95x = Math.pow(2, -100);
96for (var k = 0; k < 1000; ++k) {
97  var y = Math.log2(x);
98  var expected = Math.log(x) / Math.LN2;
99  var err = Math.abs(y - expected) / expected;
100  assertEqualsDelta(0, err, 1e-15);
101  x *= 1.1;
102}
103