blob: fb5f8a1f9068e322ba7842115fbb476b5662ee2c [file] [log] [blame]
Steve Block8defd9f2010-07-08 12:39:36 +01001// 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 Murdoch3ef787d2012-04-12 10:51:47 +010028// Flags: --allow-natives-syntax
Steve Block8defd9f2010-07-08 12:39:36 +010029// Tests the special cases specified by ES 15.8.2.13
30
Ben Murdoch3ef787d2012-04-12 10:51:47 +010031function 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 Block8defd9f2010-07-08 12:39:36 +010039
Ben Murdoch3ef787d2012-04-12 10:51:47 +010040 // 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 Block8defd9f2010-07-08 12:39:36 +010046
Ben Murdoch3ef787d2012-04-12 10:51:47 +010047 assertEquals(1, Math.pow(NaN, +0));
48 assertEquals(1, Math.pow(NaN, -0));
Steve Block8defd9f2010-07-08 12:39:36 +010049
Ben Murdoch3ef787d2012-04-12 10:51:47 +010050 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 Block8defd9f2010-07-08 12:39:36 +010057
Ben Murdoch3ef787d2012-04-12 10:51:47 +010058 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 Block8defd9f2010-07-08 12:39:36 +010062
Ben Murdoch3ef787d2012-04-12 10:51:47 +010063 // 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 Block8defd9f2010-07-08 12:39:36 +010068
Ben Murdoch3ef787d2012-04-12 10:51:47 +010069 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 Block8defd9f2010-07-08 12:39:36 +010073
Ben Murdoch3ef787d2012-04-12 10:51:47 +010074 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 Block8defd9f2010-07-08 12:39:36 +010078
Ben Murdoch3ef787d2012-04-12 10:51:47 +010079 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 Block8defd9f2010-07-08 12:39:36 +010083
Ben Murdoch3ef787d2012-04-12 10:51:47 +010084 assertEquals(Infinity, Math.pow(Infinity, 0.1));
85 assertEquals(Infinity, Math.pow(Infinity, 2));
Steve Block8defd9f2010-07-08 12:39:36 +010086
Ben Murdoch3ef787d2012-04-12 10:51:47 +010087 assertEquals(+Infinity, 1/Math.pow(Infinity, -0.1));
88 assertEquals(+Infinity, 1/Math.pow(Infinity, -2));
Steve Block8defd9f2010-07-08 12:39:36 +010089
Ben Murdoch3ef787d2012-04-12 10:51:47 +010090 assertEquals(-Infinity, Math.pow(-Infinity, 3));
91 assertEquals(-Infinity, Math.pow(-Infinity, 13));
Steve Block8defd9f2010-07-08 12:39:36 +010092
Ben Murdoch3ef787d2012-04-12 10:51:47 +010093 assertEquals(Infinity, Math.pow(-Infinity, 3.1));
94 assertEquals(Infinity, Math.pow(-Infinity, 2));
Steve Block8defd9f2010-07-08 12:39:36 +010095
Ben Murdoch3ef787d2012-04-12 10:51:47 +010096 assertEquals(-Infinity, 1/Math.pow(-Infinity, -3));
97 assertEquals(-Infinity, 1/Math.pow(-Infinity, -13));
Steve Block8defd9f2010-07-08 12:39:36 +010098
Ben Murdoch3ef787d2012-04-12 10:51:47 +010099 assertEquals(+Infinity, 1/Math.pow(-Infinity, -3.1));
100 assertEquals(+Infinity, 1/Math.pow(-Infinity, -2));
Steve Block8defd9f2010-07-08 12:39:36 +0100101
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100102 assertEquals(+Infinity, 1/Math.pow(+0, 1.1));
103 assertEquals(+Infinity, 1/Math.pow(+0, 2));
Steve Block8defd9f2010-07-08 12:39:36 +0100104
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100105 assertEquals(Infinity, Math.pow(+0, -1.1));
106 assertEquals(Infinity, Math.pow(+0, -2));
Steve Block8defd9f2010-07-08 12:39:36 +0100107
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100108 assertEquals(-Infinity, 1/Math.pow(-0, 3));
109 assertEquals(-Infinity, 1/Math.pow(-0, 13));
Steve Block8defd9f2010-07-08 12:39:36 +0100110
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100111 assertEquals(+Infinity, 1/Math.pow(-0, 3.1));
112 assertEquals(+Infinity, 1/Math.pow(-0, 2));
Steve Block8defd9f2010-07-08 12:39:36 +0100113
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100114 assertEquals(-Infinity, Math.pow(-0, -3));
115 assertEquals(-Infinity, Math.pow(-0, -13));
Steve Block8defd9f2010-07-08 12:39:36 +0100116
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100117 assertEquals(Infinity, Math.pow(-0, -3.1));
118 assertEquals(Infinity, Math.pow(-0, -2));
Steve Block8defd9f2010-07-08 12:39:36 +0100119
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100120 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 Block8defd9f2010-07-08 12:39:36 +0100128
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100129 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 Block1e0659c2011-05-24 12:43:12 +0100133
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100134 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 Block1e0659c2011-05-24 12:43:12 +0100138
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100139 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 Block1e0659c2011-05-24 12:43:12 +0100143
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100144 // Test detecting and converting integer value as double.
145 assertEquals(8, Math.pow(2, Math.sqrt(9)));
Steve Block1e0659c2011-05-24 12:43:12 +0100146
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100147 // 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
170test();
171test();
172%OptimizeFunctionOnNextCall(test);
173test();