From 4df9893256aa6cca90cb0ea14884e9baf16ebf3d Mon Sep 17 00:00:00 2001 From: Valentin Bartenev Date: Thu, 8 Dec 2016 01:52:41 +0300 Subject: [PATCH] Math.pow() method fix. --- njs/njs_math.c | 12 +++++++- njs/test/njs_unit_test.c | 60 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/njs/njs_math.c b/njs/njs_math.c index a0c57fe8..cb3f9932 100644 --- a/njs/njs_math.c +++ b/njs/njs_math.c @@ -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; diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index dc80130e..4ebb7c5f 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -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") }, -- 2.47.3