| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1 | // Copyright 2010 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 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 28 | // Flags: --allow-natives-syntax |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 29 | // Tests the special cases specified by ES 15.8.2.13 |
| 30 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 31 | function test() { |
| 32 | // Simple sanity check |
| 33 | assertEquals(4, Math.pow(2, 2)); |
| 34 | assertEquals(2147483648, Math.pow(2, 31)); |
| 35 | assertEquals(0.25, Math.pow(2, -2)); |
| 36 | assertEquals(0.0625, Math.pow(2, -4)); |
| 37 | assertEquals(1, Math.pow(1, 100)); |
| 38 | assertEquals(0, Math.pow(0, 1000)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 39 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 40 | // Spec tests |
| 41 | assertEquals(NaN, Math.pow(2, NaN)); |
| 42 | assertEquals(NaN, Math.pow(+0, NaN)); |
| 43 | assertEquals(NaN, Math.pow(-0, NaN)); |
| 44 | assertEquals(NaN, Math.pow(Infinity, NaN)); |
| 45 | assertEquals(NaN, Math.pow(-Infinity, NaN)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 46 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 47 | assertEquals(1, Math.pow(NaN, +0)); |
| 48 | assertEquals(1, Math.pow(NaN, -0)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 49 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 50 | assertEquals(NaN, Math.pow(NaN, NaN)); |
| 51 | assertEquals(NaN, Math.pow(NaN, 2.2)); |
| 52 | assertEquals(NaN, Math.pow(NaN, 1)); |
| 53 | assertEquals(NaN, Math.pow(NaN, -1)); |
| 54 | assertEquals(NaN, Math.pow(NaN, -2.2)); |
| 55 | assertEquals(NaN, Math.pow(NaN, Infinity)); |
| 56 | assertEquals(NaN, Math.pow(NaN, -Infinity)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 57 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 58 | assertEquals(Infinity, Math.pow(1.1, Infinity)); |
| 59 | assertEquals(Infinity, Math.pow(-1.1, Infinity)); |
| 60 | assertEquals(Infinity, Math.pow(2, Infinity)); |
| 61 | assertEquals(Infinity, Math.pow(-2, Infinity)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 62 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 63 | // Because +0 == -0, we need to compare 1/{+,-}0 to {+,-}Infinity |
| 64 | assertEquals(+Infinity, 1/Math.pow(1.1, -Infinity)); |
| 65 | assertEquals(+Infinity, 1/Math.pow(-1.1, -Infinity)); |
| 66 | assertEquals(+Infinity, 1/Math.pow(2, -Infinity)); |
| 67 | assertEquals(+Infinity, 1/Math.pow(-2, -Infinity)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 68 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 69 | assertEquals(NaN, Math.pow(1, Infinity)); |
| 70 | assertEquals(NaN, Math.pow(1, -Infinity)); |
| 71 | assertEquals(NaN, Math.pow(-1, Infinity)); |
| 72 | assertEquals(NaN, Math.pow(-1, -Infinity)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 73 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 74 | assertEquals(+0, Math.pow(0.1, Infinity)); |
| 75 | assertEquals(+0, Math.pow(-0.1, Infinity)); |
| 76 | assertEquals(+0, Math.pow(0.999, Infinity)); |
| 77 | assertEquals(+0, Math.pow(-0.999, Infinity)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 78 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 79 | assertEquals(Infinity, Math.pow(0.1, -Infinity)); |
| 80 | assertEquals(Infinity, Math.pow(-0.1, -Infinity)); |
| 81 | assertEquals(Infinity, Math.pow(0.999, -Infinity)); |
| 82 | assertEquals(Infinity, Math.pow(-0.999, -Infinity)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 83 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 84 | assertEquals(Infinity, Math.pow(Infinity, 0.1)); |
| 85 | assertEquals(Infinity, Math.pow(Infinity, 2)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 86 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 87 | assertEquals(+Infinity, 1/Math.pow(Infinity, -0.1)); |
| 88 | assertEquals(+Infinity, 1/Math.pow(Infinity, -2)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 89 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 90 | assertEquals(-Infinity, Math.pow(-Infinity, 3)); |
| 91 | assertEquals(-Infinity, Math.pow(-Infinity, 13)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 92 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 93 | assertEquals(Infinity, Math.pow(-Infinity, 3.1)); |
| 94 | assertEquals(Infinity, Math.pow(-Infinity, 2)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 95 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 96 | assertEquals(-Infinity, 1/Math.pow(-Infinity, -3)); |
| 97 | assertEquals(-Infinity, 1/Math.pow(-Infinity, -13)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 98 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 99 | assertEquals(+Infinity, 1/Math.pow(-Infinity, -3.1)); |
| 100 | assertEquals(+Infinity, 1/Math.pow(-Infinity, -2)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 101 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 102 | assertEquals(+Infinity, 1/Math.pow(+0, 1.1)); |
| 103 | assertEquals(+Infinity, 1/Math.pow(+0, 2)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 104 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 105 | assertEquals(Infinity, Math.pow(+0, -1.1)); |
| 106 | assertEquals(Infinity, Math.pow(+0, -2)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 107 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 108 | assertEquals(-Infinity, 1/Math.pow(-0, 3)); |
| 109 | assertEquals(-Infinity, 1/Math.pow(-0, 13)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 110 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 111 | assertEquals(+Infinity, 1/Math.pow(-0, 3.1)); |
| 112 | assertEquals(+Infinity, 1/Math.pow(-0, 2)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 113 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 114 | assertEquals(-Infinity, Math.pow(-0, -3)); |
| 115 | assertEquals(-Infinity, Math.pow(-0, -13)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 116 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 117 | assertEquals(Infinity, Math.pow(-0, -3.1)); |
| 118 | assertEquals(Infinity, Math.pow(-0, -2)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 119 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 120 | assertEquals(NaN, Math.pow(-0.00001, 1.1)); |
| 121 | assertEquals(NaN, Math.pow(-0.00001, -1.1)); |
| 122 | assertEquals(NaN, Math.pow(-1.1, 1.1)); |
| 123 | assertEquals(NaN, Math.pow(-1.1, -1.1)); |
| 124 | assertEquals(NaN, Math.pow(-2, 1.1)); |
| 125 | assertEquals(NaN, Math.pow(-2, -1.1)); |
| 126 | assertEquals(NaN, Math.pow(-1000, 1.1)); |
| 127 | assertEquals(NaN, Math.pow(-1000, -1.1)); |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 128 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 129 | assertEquals(+Infinity, 1/Math.pow(-0, 0.5)); |
| 130 | assertEquals(+Infinity, 1/Math.pow(-0, 0.6)); |
| 131 | assertEquals(-Infinity, 1/Math.pow(-0, 1)); |
| 132 | assertEquals(-Infinity, 1/Math.pow(-0, 10000000001)); |
| Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 133 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 134 | assertEquals(+Infinity, Math.pow(-0, -0.5)); |
| 135 | assertEquals(+Infinity, Math.pow(-0, -0.6)); |
| 136 | assertEquals(-Infinity, Math.pow(-0, -1)); |
| 137 | assertEquals(-Infinity, Math.pow(-0, -10000000001)); |
| Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 138 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 139 | assertEquals(4, Math.pow(16, 0.5)); |
| 140 | assertEquals(NaN, Math.pow(-16, 0.5)); |
| 141 | assertEquals(0.25, Math.pow(16, -0.5)); |
| 142 | assertEquals(NaN, Math.pow(-16, -0.5)); |
| Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 143 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 144 | // Test detecting and converting integer value as double. |
| 145 | assertEquals(8, Math.pow(2, Math.sqrt(9))); |
| Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 146 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 147 | // Tests from Mozilla 15.8.2.13. |
| 148 | assertEquals(2, Math.pow.length); |
| 149 | assertEquals(NaN, Math.pow()); |
| 150 | assertEquals(1, Math.pow(null, null)); |
| 151 | assertEquals(NaN, Math.pow(void 0, void 0)); |
| 152 | assertEquals(1, Math.pow(true, false)); |
| 153 | assertEquals(0, Math.pow(false, true)); |
| 154 | assertEquals(Infinity, Math.pow(-Infinity, Infinity)); |
| 155 | assertEquals(0, Math.pow(-Infinity, -Infinity)); |
| 156 | assertEquals(1, Math.pow(0, 0)); |
| 157 | assertEquals(0, Math.pow(0, Infinity)); |
| 158 | assertEquals(NaN, Math.pow(NaN, 0.5)); |
| 159 | assertEquals(NaN, Math.pow(NaN, -0.5)); |
| 160 | |
| 161 | // Tests from Sputnik S8.5_A13_T1. |
| 162 | assertTrue( |
| 163 | (1*((Math.pow(2,53))-1)*(Math.pow(2,-1074))) === 4.4501477170144023e-308); |
| 164 | assertTrue( |
| 165 | (1*(Math.pow(2,52))*(Math.pow(2,-1074))) === 2.2250738585072014e-308); |
| 166 | assertTrue( |
| 167 | (-1*(Math.pow(2,52))*(Math.pow(2,-1074))) === -2.2250738585072014e-308); |
| 168 | } |
| 169 | |
| 170 | test(); |
| 171 | test(); |
| 172 | %OptimizeFunctionOnNextCall(test); |
| 173 | test(); |