[njs] Math.pow() method fix.
Valentin Bartenev
vbart at nginx.com
Thu Dec 8 17:03:53 UTC 2016
details: http://hg.nginx.org/njs/rev/db0d8e2a4928
branches:
changeset: 280:db0d8e2a4928
user: Valentin Bartenev <vbart at nginx.com>
date: Thu Dec 08 01:52:41 2016 +0300
description:
Math.pow() method fix.
diffstat:
njs/njs_math.c | 12 ++++++++-
njs/test/njs_unit_test.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 71 insertions(+), 1 deletions(-)
diffs (92 lines):
diff -r 6a60530c7b6b -r db0d8e2a4928 njs/njs_math.c
--- a/njs/njs_math.c Thu Dec 08 19:50:03 2016 +0300
+++ b/njs/njs_math.c Thu Dec 08 01:52:41 2016 +0300
@@ -578,7 +578,17 @@ njs_object_math_pow(njs_vm_t *vm, njs_va
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;
diff -r 6a60530c7b6b -r db0d8e2a4928 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Thu Dec 08 19:50:03 2016 +0300
+++ b/njs/test/njs_unit_test.c Thu Dec 08 01:52:41 2016 +0300
@@ -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") },
More information about the nginx-devel
mailing list