blob: ea17a79daf0ca70e0f43577d1127a0e62743466b [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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000053// Issue 4025. Remove delta once issue 4029 has been fixed.
54assertEqualsDelta(-9.643274665532873e-17, Math.log10(1-Number.EPSILON), 3e-32);
55
Emily Bernierd0a1eb72015-03-24 16:35:39 -040056// 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) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010060 var x = %ConstructDouble(0, lowbits);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040061 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) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010066 var x = %ConstructDouble(hibits, 0);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040067 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;
Steve Block6ded16b2010-05-10 14:33:55 +0100102}