]> git.kaiwu.me - njs.git/commitdiff
Math.pow() method fix.
authorValentin Bartenev <vbart@nginx.com>
Wed, 7 Dec 2016 22:52:41 +0000 (01:52 +0300)
committerValentin Bartenev <vbart@nginx.com>
Wed, 7 Dec 2016 22:52:41 +0000 (01:52 +0300)
njs/njs_math.c
njs/test/njs_unit_test.c

index a0c57fe81d059b15187d50998363824edffb4151..cb3f9932f486e91fa5769cac8361a22448e33a05 100644 (file)
@@ -578,7 +578,17 @@ njs_object_math_pow(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
         base = args[1].data.u.number;
         exponent = args[2].data.u.number;
 
-        num = pow(base, exponent);
+        /*
+         * Accordig to ECMA-262 the result of Math.pow(+/-1, +/-Infinity)
+         * should be NaN.
+         */
+
+        if (fabs(base) != 1 || !isinf(exponent)) {
+            num = pow(base, exponent);
+
+        } else {
+            num = NAN;
+        }
 
     } else {
         num = NAN;
index dc80130e4387699ed4206393204e5ece51cff74a..4ebb7c5f24486ea0b1bd6c59e3fb64b82192a599 100644 (file)
@@ -6215,6 +6215,66 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("Math.pow()"),
       nxt_string("NaN") },
 
+    { nxt_string("Math.pow('a', -0)"),
+      nxt_string("1") },
+
+    { nxt_string("Math.pow(1.1, Infinity)"),
+      nxt_string("Infinity") },
+
+    { nxt_string("Math.pow(-1.1, -Infinity)"),
+      nxt_string("0") },
+
+    { nxt_string("Math.pow(-1, Infinity)"),
+      nxt_string("NaN") },
+
+    { nxt_string("Math.pow(1, -Infinity)"),
+      nxt_string("NaN") },
+
+    { nxt_string("Math.pow(-0.9, Infinity)"),
+      nxt_string("0") },
+
+    { nxt_string("Math.pow(0.9, -Infinity)"),
+      nxt_string("Infinity") },
+
+    { nxt_string("Math.pow('Infinity', 0.1)"),
+      nxt_string("Infinity") },
+
+    { nxt_string("Math.pow(Infinity, '-0.1')"),
+      nxt_string("0") },
+
+    { nxt_string("Math.pow(-Infinity, 3)"),
+      nxt_string("-Infinity") },
+
+    { nxt_string("Math.pow('-Infinity', '3.1')"),
+      nxt_string("Infinity") },
+
+    { nxt_string("Math.pow(-Infinity, '-3')"),
+      nxt_string("-0") },
+
+    { nxt_string("Math.pow('-Infinity', -2)"),
+      nxt_string("0") },
+
+    { nxt_string("Math.pow('0', 0.1)"),
+      nxt_string("0") },
+
+    { nxt_string("Math.pow(0, '-0.1')"),
+      nxt_string("Infinity") },
+
+    { nxt_string("Math.pow(-0, 3)"),
+      nxt_string("-0") },
+
+    { nxt_string("Math.pow('-0', '3.1')"),
+      nxt_string("0") },
+
+    { nxt_string("Math.pow(-0, '-3')"),
+      nxt_string("-Infinity") },
+
+    { nxt_string("Math.pow('-0', -2)"),
+      nxt_string("Infinity") },
+
+    { nxt_string("Math.pow(-3, 0.1)"),
+      nxt_string("NaN") },
+
     { nxt_string("var a = Math.random(); a >= 0 && a < 1"),
       nxt_string("true") },