Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(282)

Side by Side Diff: test/mjsunit/es6/math-log2-log10.js

Issue 786823003: Implement Math.log2 via ported extract from fdlibm. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: addressed comments Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 // Flags: --allow-natives-syntax
29
28 [Math.log10, Math.log2].forEach( function(fun) { 30 [Math.log10, Math.log2].forEach( function(fun) {
29 assertTrue(isNaN(fun(NaN))); 31 assertTrue(isNaN(fun(NaN)));
30 assertTrue(isNaN(fun(fun))); 32 assertTrue(isNaN(fun(fun)));
31 assertTrue(isNaN(fun({ toString: function() { return NaN; } }))); 33 assertTrue(isNaN(fun({ toString: function() { return NaN; } })));
32 assertTrue(isNaN(fun({ valueOf: function() { return -1; } }))); 34 assertTrue(isNaN(fun({ valueOf: function() { return -1; } })));
33 assertTrue(isNaN(fun({ valueOf: function() { return "abc"; } }))); 35 assertTrue(isNaN(fun({ valueOf: function() { return "abc"; } })));
34 assertTrue(isNaN(fun(-0.1))); 36 assertTrue(isNaN(fun(-0.1)));
35 assertTrue(isNaN(fun(-1))); 37 assertTrue(isNaN(fun(-1)));
36 assertEquals("-Infinity", String(fun(0))); 38 assertEquals("-Infinity", String(fun(0)));
37 assertEquals("-Infinity", String(fun(-0))); 39 assertEquals("-Infinity", String(fun(-0)));
38 assertEquals(0, fun(1)); 40 assertEquals(0, fun(1));
39 assertEquals("Infinity", String(fun(Infinity))); 41 assertEquals("Infinity", String(fun(Infinity)));
40 }); 42 });
41 43
42 for (var i = -310; i <= 308; i += 0.5) { 44 for (var i = -310; i <= 308; i += 0.5) {
43 assertEquals(i, Math.log10(Math.pow(10, i))); 45 assertEquals(i, Math.log10(Math.pow(10, i)));
44 assertEqualsDelta(i, Math.log2(Math.pow(2, i)), 1E-13); 46 // Square roots are tested below.
47 if (i != -0.5 && i != 0.5) assertEquals(i, Math.log2(Math.pow(2, i)));
45 } 48 }
46 49
47 // Test denormals. 50 // Test denormals.
48 assertEquals(-307.77759430519706, Math.log10(1.5 * Math.pow(2, -1023))); 51 assertEquals(-307.77759430519706, Math.log10(1.5 * Math.pow(2, -1023)));
52
53 // Test Math.log2(2^k) for -1074 <= k <= 1023.
54 var n = -1074;
55 // This loop covers n from -1074 to -1043
56 for (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
62 for (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
68 var x = 1;
69 for (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).
76 assertEquals(0.5000000000000001, Math.log2(Math.SQRT2));
77
78 // Expectation isn't exactly -1/2 because Math.SQRT1_2 isn't exactly sqrt(1/2).
79 assertEquals(-0.4999999999999999, Math.log2(Math.SQRT1_2));
80
81 assertEquals(3.321928094887362, Math.log2(10));
82 assertEquals(6.643856189774724, Math.log2(100));
83
84 // Test relationships
85 x = 1;
86 for (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
92 x = Math.pow(2, -100);
93 for (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;
99 }
OLDNEW
« src/third_party/fdlibm/fdlibm.js ('K') | « src/third_party/fdlibm/fdlibm.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698