mirror of
https://github.com/ccxvii/mujs.git
synced 2026-02-06 01:41:37 +08:00
Fix bugs in Number.prototype.toFixed and toExponential.
toPrecision does not behave correctly: it doesn't pad the number with trailing zeroes to reach the desired precision.
This commit is contained in:
33
jsnumber.c
33
jsnumber.c
@@ -116,12 +116,6 @@ static void Np_toString(js_State *J)
|
||||
static void numtostr(js_State *J, const char *fmt, int w, double n)
|
||||
{
|
||||
char buf[32], *e;
|
||||
if (isnan(n)) js_pushliteral(J, "NaN");
|
||||
else if (isinf(n)) js_pushliteral(J, n < 0 ? "-Infinity" : "Infinity");
|
||||
else if (n == 0) js_pushliteral(J, "0");
|
||||
else {
|
||||
if (w < 1) w = 1;
|
||||
if (w > 17) w = 17;
|
||||
sprintf(buf, fmt, w, n);
|
||||
e = strchr(buf, 'e');
|
||||
if (e) {
|
||||
@@ -130,21 +124,36 @@ static void numtostr(js_State *J, const char *fmt, int w, double n)
|
||||
}
|
||||
js_pushstring(J, buf);
|
||||
}
|
||||
}
|
||||
|
||||
static void Np_toFixed(js_State *J)
|
||||
{
|
||||
js_Object *self = js_toobject(J, 0);
|
||||
int width = js_tointeger(J, 1);
|
||||
char buf[32];
|
||||
double x;
|
||||
if (self->type != JS_CNUMBER) js_typeerror(J, "not a number");
|
||||
numtostr(J, "%.*f", width, self->u.number);
|
||||
if (width < 0) js_rangeerror(J, "precision %d out of range", width);
|
||||
if (width > 20) js_rangeerror(J, "precision %d out of range", width);
|
||||
x = self->u.number;
|
||||
if (isnan(x) || isinf(x) || x <= -1e21 || x >= 1e21)
|
||||
js_pushstring(J, jsV_numbertostring(J, buf, x));
|
||||
else
|
||||
numtostr(J, "%.*f", width, x);
|
||||
}
|
||||
|
||||
static void Np_toExponential(js_State *J)
|
||||
{
|
||||
js_Object *self = js_toobject(J, 0);
|
||||
int width = js_tointeger(J, 1);
|
||||
char buf[32];
|
||||
double x;
|
||||
if (self->type != JS_CNUMBER) js_typeerror(J, "not a number");
|
||||
if (width < 0) js_rangeerror(J, "precision %d out of range", width);
|
||||
if (width > 20) js_rangeerror(J, "precision %d out of range", width);
|
||||
x = self->u.number;
|
||||
if (isnan(x) || isinf(x))
|
||||
js_pushstring(J, jsV_numbertostring(J, buf, x));
|
||||
else
|
||||
numtostr(J, "%.*e", width, self->u.number);
|
||||
}
|
||||
|
||||
@@ -152,7 +161,15 @@ static void Np_toPrecision(js_State *J)
|
||||
{
|
||||
js_Object *self = js_toobject(J, 0);
|
||||
int width = js_tointeger(J, 1);
|
||||
char buf[32];
|
||||
double x;
|
||||
if (self->type != JS_CNUMBER) js_typeerror(J, "not a number");
|
||||
if (width < 1) js_rangeerror(J, "precision %d out of range", width);
|
||||
if (width > 21) js_rangeerror(J, "precision %d out of range", width);
|
||||
x = self->u.number;
|
||||
if (isnan(x) || isinf(x))
|
||||
js_pushstring(J, jsV_numbertostring(J, buf, x));
|
||||
else
|
||||
numtostr(J, "%.*g", width, self->u.number);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user