Harmony: implement math.hypot.
authoryangguo@chromium.org <yangguo@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Mon, 23 Dec 2013 11:13:39 +0000 (11:13 +0000)
committeryangguo@chromium.org <yangguo@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Mon, 23 Dec 2013 11:13:39 +0000 (11:13 +0000)
R=jarin@chromium.org
BUG=v8:2938
LOG=N

Review URL: https://codereview.chromium.org/118303002

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@18407 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/harmony-math.js
test/mjsunit/harmony/math-hypot.js [new file with mode: 0644]

index 652e8ae..d57a104 100644 (file)
@@ -122,6 +122,39 @@ function MathLog2(x) {
 }
 
 
+//ES6 draft 09-27-13, section 20.2.2.17.
+function MathHypot(x, y) {  // Function length is 2.
+  // We may want to introduce fast paths for two arguments and when
+  // normalization to avoid overflow is not necessary.  For now, we
+  // simply assume the general case.
+  var length = %_ArgumentsLength();
+  var args = new InternalArray(length);
+  var max = 0;
+  for (var i = 0; i < length; i++) {
+    var n = %_Arguments(i);
+    if (!IS_NUMBER(n)) n = NonNumberToNumber(n);
+    if (n === INFINITY || n === -INFINITY) return INFINITY;
+    n = MathAbs(n);
+    if (n > max) max = n;
+    args[i] = n;
+  }
+
+  // Kahan summation to avoid rounding errors.
+  // Normalize the numbers to the largest one to avoid overflow.
+  if (max === 0) max = 1;
+  var sum = 0;
+  var compensation = 0;
+  for (var i = 0; i < length; i++) {
+    var n = args[i] / max;
+    var summand = n * n - compensation;
+    var preliminary = sum + summand;
+    compensation = (preliminary - sum) - summand;
+    sum = preliminary;
+  }
+  return MathSqrt(sum) * max;
+}
+
+
 function ExtendMath() {
   %CheckIsBootstrapping();
 
@@ -136,7 +169,8 @@ function ExtendMath() {
     "acosh", MathAcosh,
     "atanh", MathAtanh,
     "log10", MathLog10,
-    "log2", MathLog2
+    "log2", MathLog2,
+    "hypot", MathHypot
   ));
 }
 
diff --git a/test/mjsunit/harmony/math-hypot.js b/test/mjsunit/harmony/math-hypot.js
new file mode 100644 (file)
index 0000000..1052627
--- /dev/null
@@ -0,0 +1,94 @@
+// Copyright 2013 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+//       notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+//       copyright notice, this list of conditions and the following
+//       disclaimer in the documentation and/or other materials provided
+//       with the distribution.
+//     * Neither the name of Google Inc. nor the names of its
+//       contributors may be used to endorse or promote products derived
+//       from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// Flags: --harmony-maths
+
+assertTrue(isNaN(Math.hypot({})));
+assertTrue(isNaN(Math.hypot(undefined, 1)));
+assertTrue(isNaN(Math.hypot(1, undefined)));
+assertTrue(isNaN(Math.hypot(Math.hypot, 1)));
+assertEquals(1, Math.hypot(1));
+assertEquals(Math.PI, Math.hypot(Math.PI));
+assertEquals(5, Math.hypot(3, 4));
+assertEquals(13, Math.hypot(3, 4, 12));
+assertEquals(15, Math.hypot(" 2 ",
+                            "0x5",
+                            { valueOf: function() { return "0xe"; } }));
+assertEquals(17, Math.hypot({ valueOf: function() { return 1; } },
+                            { toString: function() { return 12; } },
+                            { toString: function() { return "12"; } }));
+
+// Check function length.
+assertEquals(2, Math.hypot.length);
+
+// Check that 0 is returned for no arguments.
+assertEquals(0, Math.hypot());
+
+// Check that Infinity is returned if any of the arguments is +/-Infinity.
+assertEquals("Infinity", String(Math.hypot(NaN, Infinity)));
+assertEquals("Infinity", String(Math.hypot(1, -Infinity, 2)));
+
+// Check that NaN is returned if any argument is NaN and none is +/-Infinity/
+assertTrue(isNaN(Math.hypot(1, 2, NaN)));
+assertTrue(isNaN(Math.hypot(NaN, NaN, 4)));
+
+// Check that +0 is returned if all arguments are +/-0.
+assertEquals("Infinity", String(1/Math.hypot(-0)));
+assertEquals("Infinity", String(1/Math.hypot(0)));
+assertEquals("Infinity", String(1/Math.hypot(-0, -0)));
+assertEquals("Infinity", String(1/Math.hypot(-0, 0)));
+
+// Check that we avoid overflows and underflows.
+assertEqualsDelta(5E300, Math.hypot(3E300, 4E300), 1E285);
+assertEqualsDelta(17E-300, Math.hypot(8E-300, 15E-300), 1E-315);
+assertEqualsDelta(19E300, Math.hypot(6E300, 6E300, 17E300), 1E285);
+
+// Check that we sufficiently account for rounding errors when summing up.
+// For this, we calculate a simple fractal square that recurses in the
+// fourth quarter.
+var fractals = [];
+var edge_length = Math.E * 1E20;
+
+var fractal_length = edge_length;
+while(fractal_length >= 1) {
+  fractal_length *= 0.5;
+  fractals.push(fractal_length);
+  fractals.push(fractal_length);
+  fractals.push(fractal_length);
+}
+
+fractals.push(fractal_length);
+assertEqualsDelta(edge_length, Math.hypot.apply(Math, fractals), 1E-15);
+fractals.reverse();
+assertEqualsDelta(edge_length, Math.hypot.apply(Math, fractals), 1E-15);
+// Also shuffle the array.
+var c = 0;
+function random_sort(a, b) { c++; return (c & 3) - 1.5; }
+fractals.sort(random_sort);
+assertEqualsDelta(edge_length, Math.hypot.apply(Math, fractals), 1E-15);
+fractals.sort(random_sort);
+assertEqualsDelta(edge_length, Math.hypot.apply(Math, fractals), 1E-15);