math-pow.js revision 85b71799222b55eb5dd74ea26efe0c64ab655c8c
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
28// Tests the special cases specified by ES 15.8.2.13
29
30// Simple sanity check
31assertEquals(4, Math.pow(2, 2));
32assertEquals(2147483648, Math.pow(2, 31));
33assertEquals(0.25, Math.pow(2, -2));
34assertEquals(0.0625, Math.pow(2, -4));
35assertEquals(1, Math.pow(1, 100));
36assertEquals(0, Math.pow(0, 1000));
37
38// Spec tests
39assertEquals(NaN, Math.pow(2, NaN));
40assertEquals(NaN, Math.pow(+0, NaN));
41assertEquals(NaN, Math.pow(-0, NaN));
42assertEquals(NaN, Math.pow(Infinity, NaN));
43assertEquals(NaN, Math.pow(-Infinity, NaN));
44
45assertEquals(1, Math.pow(NaN, +0));
46assertEquals(1, Math.pow(NaN, -0));
47
48assertEquals(NaN, Math.pow(NaN, NaN));
49assertEquals(NaN, Math.pow(NaN, 2.2));
50assertEquals(NaN, Math.pow(NaN, 1));
51assertEquals(NaN, Math.pow(NaN, -1));
52assertEquals(NaN, Math.pow(NaN, -2.2));
53assertEquals(NaN, Math.pow(NaN, Infinity));
54assertEquals(NaN, Math.pow(NaN, -Infinity));
55
56assertEquals(Infinity, Math.pow(1.1, Infinity));
57assertEquals(Infinity, Math.pow(-1.1, Infinity));
58assertEquals(Infinity, Math.pow(2, Infinity));
59assertEquals(Infinity, Math.pow(-2, Infinity));
60
61// Because +0 == -0, we need to compare 1/{+,-}0 to {+,-}Infinity
62assertEquals(+Infinity, 1/Math.pow(1.1, -Infinity));
63assertEquals(+Infinity, 1/Math.pow(-1.1, -Infinity));
64assertEquals(+Infinity, 1/Math.pow(2, -Infinity));
65assertEquals(+Infinity, 1/Math.pow(-2, -Infinity));
66
67assertEquals(NaN, Math.pow(1, Infinity));
68assertEquals(NaN, Math.pow(1, -Infinity));
69assertEquals(NaN, Math.pow(-1, Infinity));
70assertEquals(NaN, Math.pow(-1, -Infinity));
71
72assertEquals(+0, Math.pow(0.1, Infinity));
73assertEquals(+0, Math.pow(-0.1, Infinity));
74assertEquals(+0, Math.pow(0.999, Infinity));
75assertEquals(+0, Math.pow(-0.999, Infinity));
76
77assertEquals(Infinity, Math.pow(0.1, -Infinity));
78assertEquals(Infinity, Math.pow(-0.1, -Infinity));
79assertEquals(Infinity, Math.pow(0.999, -Infinity));
80assertEquals(Infinity, Math.pow(-0.999, -Infinity));
81
82assertEquals(Infinity, Math.pow(Infinity, 0.1));
83assertEquals(Infinity, Math.pow(Infinity, 2));
84
85assertEquals(+Infinity, 1/Math.pow(Infinity, -0.1));
86assertEquals(+Infinity, 1/Math.pow(Infinity, -2));
87
88assertEquals(-Infinity, Math.pow(-Infinity, 3));
89assertEquals(-Infinity, Math.pow(-Infinity, 13));
90
91assertEquals(Infinity, Math.pow(-Infinity, 3.1));
92assertEquals(Infinity, Math.pow(-Infinity, 2));
93
94assertEquals(-Infinity, 1/Math.pow(-Infinity, -3));
95assertEquals(-Infinity, 1/Math.pow(-Infinity, -13));
96
97assertEquals(+Infinity, 1/Math.pow(-Infinity, -3.1));
98assertEquals(+Infinity, 1/Math.pow(-Infinity, -2));
99
100assertEquals(+Infinity, 1/Math.pow(+0, 1.1));
101assertEquals(+Infinity, 1/Math.pow(+0, 2));
102
103assertEquals(Infinity, Math.pow(+0, -1.1));
104assertEquals(Infinity, Math.pow(+0, -2));
105
106assertEquals(-Infinity, 1/Math.pow(-0, 3));
107assertEquals(-Infinity, 1/Math.pow(-0, 13));
108
109assertEquals(+Infinity, 1/Math.pow(-0, 3.1));
110assertEquals(+Infinity, 1/Math.pow(-0, 2));
111
112assertEquals(-Infinity, Math.pow(-0, -3));
113assertEquals(-Infinity, Math.pow(-0, -13));
114
115assertEquals(Infinity, Math.pow(-0, -3.1));
116assertEquals(Infinity, Math.pow(-0, -2));
117
118assertEquals(NaN, Math.pow(-0.00001, 1.1));
119assertEquals(NaN, Math.pow(-0.00001, -1.1));
120assertEquals(NaN, Math.pow(-1.1, 1.1));
121assertEquals(NaN, Math.pow(-1.1, -1.1));
122assertEquals(NaN, Math.pow(-2, 1.1));
123assertEquals(NaN, Math.pow(-2, -1.1));
124assertEquals(NaN, Math.pow(-1000, 1.1));
125assertEquals(NaN, Math.pow(-1000, -1.1));
126
127assertEquals(+Infinity, 1/Math.pow(-0, 0.5));
128assertEquals(+Infinity, 1/Math.pow(-0, 0.6));
129assertEquals(-Infinity, 1/Math.pow(-0, 1));
130assertEquals(-Infinity, 1/Math.pow(-0, 10000000001));
131
132assertEquals(+Infinity, Math.pow(-0, -0.5));
133assertEquals(+Infinity, Math.pow(-0, -0.6));
134assertEquals(-Infinity, Math.pow(-0, -1));
135assertEquals(-Infinity, Math.pow(-0, -10000000001));
136
137
138
139// Tests from Sputnik S8.5_A13_T1.
140assertTrue((1*((Math.pow(2,53))-1)*(Math.pow(2,-1074))) === 4.4501477170144023e-308);
141assertTrue((1*(Math.pow(2,52))*(Math.pow(2,-1074))) === 2.2250738585072014e-308);
142assertTrue((-1*(Math.pow(2,52))*(Math.pow(2,-1074))) === -2.2250738585072014e-308);
143