ref: 336097c57fcf5275ab84feab383ecf7773c7ec85
parent: f32bcea8f48f10357aca1e89a9068c22899c8f3e
author: Tor Andersson <tor.andersson@gmail.com>
date: Fri Jan 20 09:23:51 EST 2017
Reimplement Math.round using floor().
--- a/jsi.h
+++ b/jsi.h
@@ -39,7 +39,6 @@
}
#endif
#if _MSC_VER <= 1700 /* <= MSVC 2012 */
-#define round(x) floor((x) < 0 ? (x) - 0.5 : (x) + 0.5)
#define isnan(x) _isnan(x)
#define isinf(x) (!_finite(x))
#define isfinite(x) _finite(x)
--- a/jsmath.c
+++ b/jsmath.c
@@ -69,14 +69,20 @@
js_pushnumber(J, rand() / (RAND_MAX + 1.0));
}
+static double do_round(double x)
+{
+ if (isnan(x)) return x;
+ if (isinf(x)) return x;
+ if (x == 0) return x;
+ if (x > 0 && x < 0.5) return 0;
+ if (x < 0 && x >= -0.5) return -0;
+ return floor(x + 0.5);
+}
+
static void Math_round(js_State *J)
{
double x = js_tonumber(J, 1);
- double r = round(x);
- if (r - x == -0.5)
- js_pushnumber(J, x == -0.5 ? -0.0 : r + 1.0);
- else
- js_pushnumber(J, r);
+ js_pushnumber(J, do_round(x));
}
static void Math_sin(js_State *J)