Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Math.sumPrecise #697

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -43275,6 +43275,64 @@ static JSValue js_math_clz32(JSContext *ctx, JSValue this_val,
return js_int32(r);
}

static JSValue js_math_sumPrecise(JSContext *ctx, JSValue this_val,
int argc, JSValue *argv)
{
JSValue iter, next, item, ret;
bf_t a, b;
BOOL done;
double d;
int r;

iter = JS_GetIterator(ctx, argv[0], /*async*/FALSE);
if (JS_IsException(iter))
return JS_EXCEPTION;
bf_init(ctx->bf_ctx, &a);
bf_init(ctx->bf_ctx, &b);
ret = JS_EXCEPTION;
next = JS_GetProperty(ctx, iter, JS_ATOM_next);
if (JS_IsException(next))
goto fail;
bf_set_zero(&a, /*is_neg*/TRUE);
for (;;) {
item = JS_IteratorNext(ctx, iter, next, 0, NULL, &done);
if (JS_IsException(item))
goto fail;
if (done)
break; // item == JS_UNDEFINED
switch (JS_VALUE_GET_TAG(item)) {
default:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: don't we usually put default at the end?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but this way the "good" cases are closer to the place where d is used. Easier to read, IMO.

(I'm assuming that wasn't a blocking comment but LMK if it was.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking, go ahead!

JS_FreeValue(ctx, item);
JS_ThrowTypeError(ctx, "not a number");
goto fail;
case JS_TAG_INT:
d = JS_VALUE_GET_INT(item);
break;
case JS_TAG_FLOAT64:
d = JS_VALUE_GET_FLOAT64(item);
break;
}
if (bf_set_float64(&b, d))
goto oom;
// Infinity + -Infinity results in BF_ST_INVALID_OP, sets |a| to nan
if ((r = bf_add(&a, &a, &b, BF_PREC_INF, BF_RNDN)))
if (r != BF_ST_INVALID_OP)
goto oom;
}
bf_get_float64(&a, &d, BF_RNDN); // return value deliberately ignored
ret = js_float64(d);
fail:
JS_IteratorClose(ctx, iter, JS_IsException(ret));
JS_FreeValue(ctx, iter);
JS_FreeValue(ctx, next);
bf_delete(&a);
bf_delete(&b);
return ret;
oom:
JS_ThrowOutOfMemory(ctx);
goto fail;
}

/* xorshift* random number generator by Marsaglia */
static uint64_t xorshift64star(uint64_t *pstate)
{
Expand Down Expand Up @@ -43376,6 +43434,7 @@ static const JSCFunctionListEntry js_math_funcs[] = {
JS_CFUNC_SPECIAL_DEF("fround", 1, f_f, js_math_fround ),
JS_CFUNC_DEF("imul", 2, js_math_imul ),
JS_CFUNC_DEF("clz32", 1, js_math_clz32 ),
JS_CFUNC_DEF("sumPrecise", 1, js_math_sumPrecise ),
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Math", JS_PROP_CONFIGURABLE ),
JS_PROP_DOUBLE_DEF("E", 2.718281828459045, 0 ),
JS_PROP_DOUBLE_DEF("LN10", 2.302585092994046, 0 ),
Expand Down
2 changes: 1 addition & 1 deletion test262.conf
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ legacy-regexp=skip
let
logical-assignment-operators
Map
Math.sumPrecise=skip
Math.sumPrecise
new.target
numeric-separator-literal
object-rest
Expand Down
Loading