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