1 // Copyright 2013 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: --harmony-maths 29 30 assertTrue(isNaN(Math.hypot({}))); 31 assertTrue(isNaN(Math.hypot(undefined, 1))); 32 assertTrue(isNaN(Math.hypot(1, undefined))); 33 assertTrue(isNaN(Math.hypot(Math.hypot, 1))); 34 assertEquals(1, Math.hypot(1)); 35 assertEquals(Math.PI, Math.hypot(Math.PI)); 36 assertEquals(5, Math.hypot(3, 4)); 37 assertEquals(13, Math.hypot(3, 4, 12)); 38 assertEquals(15, Math.hypot(" 2 ", 39 "0x5", 40 { valueOf: function() { return "0xe"; } })); 41 assertEquals(17, Math.hypot({ valueOf: function() { return 1; } }, 42 { toString: function() { return 12; } }, 43 { toString: function() { return "12"; } })); 44 45 // Check function length. 46 assertEquals(2, Math.hypot.length); 47 48 // Check that 0 is returned for no arguments. 49 assertEquals(0, Math.hypot()); 50 51 // Check that Infinity is returned if any of the arguments is +/-Infinity. 52 assertEquals("Infinity", String(Math.hypot(NaN, Infinity))); 53 assertEquals("Infinity", String(Math.hypot(1, -Infinity, 2))); 54 55 // Check that NaN is returned if any argument is NaN and none is +/-Infinity/ 56 assertTrue(isNaN(Math.hypot(1, 2, NaN))); 57 assertTrue(isNaN(Math.hypot(NaN, NaN, 4))); 58 59 // Check that +0 is returned if all arguments are +/-0. 60 assertEquals("Infinity", String(1/Math.hypot(-0))); 61 assertEquals("Infinity", String(1/Math.hypot(0))); 62 assertEquals("Infinity", String(1/Math.hypot(-0, -0))); 63 assertEquals("Infinity", String(1/Math.hypot(-0, 0))); 64 65 // Check that we avoid overflows and underflows. 66 assertEqualsDelta(5E300, Math.hypot(3E300, 4E300), 1E285); 67 assertEqualsDelta(17E-300, Math.hypot(8E-300, 15E-300), 1E-315); 68 assertEqualsDelta(19E300, Math.hypot(6E300, 6E300, 17E300), 1E285); 69 70 // Check that we sufficiently account for rounding errors when summing up. 71 // For this, we calculate a simple fractal square that recurses in the 72 // fourth quarter. 73 var fractals = []; 74 var edge_length = Math.E * 1E20; 75 76 var fractal_length = edge_length; 77 while(fractal_length >= 1) { 78 fractal_length *= 0.5; 79 fractals.push(fractal_length); 80 fractals.push(fractal_length); 81 fractals.push(fractal_length); 82 } 83 84 fractals.push(fractal_length); 85 assertEqualsDelta(edge_length, Math.hypot.apply(Math, fractals), 1E-15); 86 fractals.reverse(); 87 assertEqualsDelta(edge_length, Math.hypot.apply(Math, fractals), 1E-15); 88 // Also shuffle the array. 89 var c = 0; 90 function random_sort(a, b) { c++; return (c & 3) - 1.5; } 91 fractals.sort(random_sort); 92 assertEqualsDelta(edge_length, Math.hypot.apply(Math, fractals), 1E-15); 93 fractals.sort(random_sort); 94 assertEqualsDelta(edge_length, Math.hypot.apply(Math, fractals), 1E-15); 95