shithub: libmujs

Download patch

ref: a88e9d4c5eb94971d1948e765f16001cdc34bba6
parent: e107fcf8ca813cd64ffe8b28a81011d5e9b7fc7e
author: Szabolcs Nagy <nsz@port70.net>
date: Thu Oct 16 17:44:30 EDT 2014

Fix Math.atan2 and Math.pow argument evaluation order

Math.pow has subtly different semantics than ISO C pow for the
non-finite powers of +-1, so that's handled separately.
(ECMA requires NAN result, in ISO C Annex F pow(1,any) == 1 and
pow(-1,+-inf) == 1)

--- a/jsmath.c
+++ b/jsmath.c
@@ -24,7 +24,9 @@
 
 static void Math_atan2(js_State *J)
 {
-	js_pushnumber(J, atan2(js_tonumber(J, 1), js_tonumber(J, 2)));
+	double y = js_tonumber(J, 1);
+	double x = js_tonumber(J, 2);
+	js_pushnumber(J, atan2(y, x));
 }
 
 static void Math_ceil(js_State *J)
@@ -54,7 +56,12 @@
 
 static void Math_pow(js_State *J)
 {
-	js_pushnumber(J, pow(js_tonumber(J, 1), js_tonumber(J, 2)));
+	double x = js_tonumber(J, 1);
+	double y = js_tonumber(J, 2);
+	if (!isfinite(y) && fabs(x) == 1)
+		js_pushnumber(J, NAN);
+	else
+		js_pushnumber(J, pow(x,y));
 }
 
 static void Math_random(js_State *J)