Home | History | Annotate | Download | only in mjsunit
      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 // Flags: --allow-natives-syntax
     29 // Tests the special cases specified by ES 15.8.2.13
     30 
     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));
     39 
     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));
     46 
     47   assertEquals(1, Math.pow(NaN, +0));
     48   assertEquals(1, Math.pow(NaN, -0));
     49 
     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));
     57 
     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));
     62 
     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));
     68 
     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));
     73 
     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));
     78 
     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));
     83 
     84   assertEquals(Infinity, Math.pow(Infinity, 0.1));
     85   assertEquals(Infinity, Math.pow(Infinity, 2));
     86 
     87   assertEquals(+Infinity, 1/Math.pow(Infinity, -0.1));
     88   assertEquals(+Infinity, 1/Math.pow(Infinity, -2));
     89 
     90   assertEquals(-Infinity, Math.pow(-Infinity, 3));
     91   assertEquals(-Infinity, Math.pow(-Infinity, 13));
     92 
     93   assertEquals(Infinity, Math.pow(-Infinity, 3.1));
     94   assertEquals(Infinity, Math.pow(-Infinity, 2));
     95 
     96   assertEquals(-Infinity, 1/Math.pow(-Infinity, -3));
     97   assertEquals(-Infinity, 1/Math.pow(-Infinity, -13));
     98 
     99   assertEquals(+Infinity, 1/Math.pow(-Infinity, -3.1));
    100   assertEquals(+Infinity, 1/Math.pow(-Infinity, -2));
    101 
    102   assertEquals(+Infinity, 1/Math.pow(+0, 1.1));
    103   assertEquals(+Infinity, 1/Math.pow(+0, 2));
    104 
    105   assertEquals(Infinity, Math.pow(+0, -1.1));
    106   assertEquals(Infinity, Math.pow(+0, -2));
    107 
    108   assertEquals(-Infinity, 1/Math.pow(-0, 3));
    109   assertEquals(-Infinity, 1/Math.pow(-0, 13));
    110 
    111   assertEquals(+Infinity, 1/Math.pow(-0, 3.1));
    112   assertEquals(+Infinity, 1/Math.pow(-0, 2));
    113 
    114   assertEquals(-Infinity, Math.pow(-0, -3));
    115   assertEquals(-Infinity, Math.pow(-0, -13));
    116 
    117   assertEquals(Infinity, Math.pow(-0, -3.1));
    118   assertEquals(Infinity, Math.pow(-0, -2));
    119 
    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));
    128 
    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));
    133 
    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));
    138 
    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));
    143 
    144   // Test detecting and converting integer value as double.
    145   assertEquals(8, Math.pow(2, Math.sqrt(9)));
    146 
    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();