blob: fa3f46807a10169ec36b99297ae24cf0fd080e1c [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2013 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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
Emily Bernierd0a1eb72015-03-24 16:35:39 -040028// Flags: --allow-natives-syntax
29
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030[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});
Steve Blocka7e24c12009-10-30 11:49:00 +000043
Emily Bernierd0a1eb72015-03-24 16:35:39 -040044for (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// Test Math.log2(2^k) for -1074 <= k <= 1023.
54var n = -1074;
55// This loop covers n from -1074 to -1043
56for (var lowbits = 1; lowbits <= 0x80000000; lowbits *= 2) {
57 var x = %_ConstructDouble(0, lowbits);
58 assertEquals(n, Math.log2(x));
59 n++;
60}
61// This loop covers n from -1042 to -1023
62for (var hibits = 1; hibits <= 0x80000; hibits *= 2) {
63 var x = %_ConstructDouble(hibits, 0);
64 assertEquals(n, Math.log2(x));
65 n++;
66}
67// The rest of the normal values of 2^n
68var x = 1;
69for (var n = -1022; n <= 1023; ++n) {
70 var x = Math.pow(2, n);
71 assertEquals(n, Math.log2(x));
72}
73
74// Test special values.
75// Expectation isn't exactly 1/2 because Math.SQRT2 isn't exactly sqrt(2).
76assertEquals(0.5000000000000001, Math.log2(Math.SQRT2));
77
78// Expectation isn't exactly -1/2 because Math.SQRT1_2 isn't exactly sqrt(1/2).
79assertEquals(-0.4999999999999999, Math.log2(Math.SQRT1_2));
80
81assertEquals(3.321928094887362, Math.log2(10));
82assertEquals(6.643856189774724, Math.log2(100));
83
84// Test relationships
85x = 1;
86for (var k = 0; k < 1000; ++k) {
87 var y = Math.abs(Math.log2(x) + Math.log2(1/x));
88 assertEqualsDelta(0, y, 1.5e-14);
89 x *= 1.1;
90}
91
92x = Math.pow(2, -100);
93for (var k = 0; k < 1000; ++k) {
94 var y = Math.log2(x);
95 var expected = Math.log(x) / Math.LN2;
96 var err = Math.abs(y - expected) / expected;
97 assertEqualsDelta(0, err, 1e-15);
98 x *= 1.1;
Steve Block6ded16b2010-05-10 14:33:55 +010099}