From b5de03a808cb01eec91fb4273e10da3989a746e6 Mon Sep 17 00:00:00 2001 From: Peter Gal Date: Wed, 22 Jul 2015 16:08:29 +0200 Subject: [PATCH] Improve Infinity handling in Math.min/max methods. In Math.max case: if we already found an infinity value update the result only when the previous value was a negative infinity. In Math.min case: if we already found an infinity value update the result only when the previous value was a positive infinity. JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com --- .../ecma/builtin-objects/ecma-builtin-math.cpp | 18 ++++++++++-------- tests/jerry/math-max.js | 10 ++++++++++ tests/jerry/math-min.js | 10 ++++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-math.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-math.cpp index 7dbfa6c09..1317f78fb 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-math.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-math.cpp @@ -362,11 +362,12 @@ ecma_builtin_math_object_max (ecma_value_t this_arg __attr_unused___, /**< 'this ret_num = arg_num; } } - else if (ecma_number_is_infinity (ret_num)) /* ret_num is negative infinity */ + else if (ecma_number_is_infinity (ret_num)) { - JERRY_ASSERT (ecma_number_is_negative (ret_num)); - - ret_num = arg_num; + if (ecma_number_is_negative (ret_num)) + { + ret_num = arg_num; + } } else { @@ -443,11 +444,12 @@ ecma_builtin_math_object_min (ecma_value_t this_arg __attr_unused___, /**< 'this ret_num = arg_num; } } - else if (ecma_number_is_infinity (ret_num)) /* ret_num is positive infinity */ + else if (ecma_number_is_infinity (ret_num)) { - JERRY_ASSERT (!ecma_number_is_negative (ret_num)); - - ret_num = arg_num; + if (!ecma_number_is_negative (ret_num)) + { + ret_num = arg_num; + } } else { diff --git a/tests/jerry/math-max.js b/tests/jerry/math-max.js index 8cf8df0ff..0317e5bed 100644 --- a/tests/jerry/math-max.js +++ b/tests/jerry/math-max.js @@ -27,3 +27,13 @@ assert(Math['max'] () === -Infinity); assert(Math['max'] (0.0, -0.0) === 0.0); assert(Math['max'] (-0.0, 0.0) === 0.0); + +assert(Math['max'] (2, Infinity) === Infinity); +assert(Math['max'] (Infinity, 2) === Infinity); +assert(Math['max'] (2, -Infinity) === 2); +assert(Math['max'] (-Infinity, 2) === 2); + +assert(Math['max'] (-2, Infinity) === Infinity); +assert(Math['max'] (Infinity, -2) === Infinity); +assert(Math['max'] (-2, -Infinity) === -2); +assert(Math['max'] (-Infinity, -2) === -2); diff --git a/tests/jerry/math-min.js b/tests/jerry/math-min.js index 78cfbd5ab..34899c042 100644 --- a/tests/jerry/math-min.js +++ b/tests/jerry/math-min.js @@ -27,3 +27,13 @@ assert(Math['min'] () === Infinity); assert(Math['min'] (0.0, -0.0) === -0.0); assert(Math['min'] (-0.0, 0.0) === -0.0); + +assert(Math['min'] (2, -Infinity) === -Infinity); +assert(Math['min'] (-Infinity, 2) === -Infinity); +assert(Math['min'] (2, Infinity) === 2); +assert(Math['min'] (Infinity, 2) === 2); + +assert(Math['min'] (-2, Infinity) === -2); +assert(Math['min'] (Infinity, -2) === -2); +assert(Math['min'] (-2, -Infinity) === -Infinity); +assert(Math['min'] (-Infinity, -2) === -Infinity);