Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2013 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 28 | // Flags: --allow-natives-syntax |
| 29 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 | }); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 43 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 44 | for (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. |
| 51 | assertEquals(-307.77759430519706, Math.log10(1.5 * Math.pow(2, -1023))); |
| 52 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 53 | // Issue 4025. Remove delta once issue 4029 has been fixed. |
| 54 | assertEqualsDelta(-9.643274665532873e-17, Math.log10(1-Number.EPSILON), 3e-32); |
| 55 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 56 | // Test Math.log2(2^k) for -1074 <= k <= 1023. |
| 57 | var n = -1074; |
| 58 | // This loop covers n from -1074 to -1043 |
| 59 | for (var lowbits = 1; lowbits <= 0x80000000; lowbits *= 2) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 60 | var x = %ConstructDouble(0, lowbits); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 61 | assertEquals(n, Math.log2(x)); |
| 62 | n++; |
| 63 | } |
| 64 | // This loop covers n from -1042 to -1023 |
| 65 | for (var hibits = 1; hibits <= 0x80000; hibits *= 2) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 66 | var x = %ConstructDouble(hibits, 0); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 67 | assertEquals(n, Math.log2(x)); |
| 68 | n++; |
| 69 | } |
| 70 | // The rest of the normal values of 2^n |
| 71 | var x = 1; |
| 72 | for (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). |
| 79 | assertEquals(0.5000000000000001, Math.log2(Math.SQRT2)); |
| 80 | |
| 81 | // Expectation isn't exactly -1/2 because Math.SQRT1_2 isn't exactly sqrt(1/2). |
| 82 | assertEquals(-0.4999999999999999, Math.log2(Math.SQRT1_2)); |
| 83 | |
| 84 | assertEquals(3.321928094887362, Math.log2(10)); |
| 85 | assertEquals(6.643856189774724, Math.log2(100)); |
| 86 | |
| 87 | // Test relationships |
| 88 | x = 1; |
| 89 | for (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 | |
| 95 | x = Math.pow(2, -100); |
| 96 | for (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 Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 102 | } |