From: Valentin Bartenev Date: Wed, 7 Dec 2016 22:52:41 +0000 (+0300) Subject: Math.pow() method fix. X-Git-Tag: 0.1.6~2 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=4df9893256aa6cca90cb0ea14884e9baf16ebf3d;p=njs.git Math.pow() method fix. --- 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") },