Пример #1
0
JSBool
js_math_min(JSContext *cx, uintN argc, jsval *vp)
{
    jsdouble x, z = js_PositiveInfinity;
    jsval *argv;
    uintN i;

    if (argc == 0) {
        *vp = cx->runtime->positiveInfinityValue;
        return JS_TRUE;
    }
    argv = vp + 2;
    for (i = 0; i < argc; i++) {
        x = js_ValueToNumber(cx, &argv[i]);
        if (JSVAL_IS_NULL(argv[i]))
            return JS_FALSE;
        if (JSDOUBLE_IS_NaN(x)) {
            *vp = cx->runtime->NaNValue;
            return JS_TRUE;
        }
        if (x == 0 && x == z) {
            if (js_copysign(1.0, x) == -1)
                z = x;
        } else {
            z = (x < z) ? x : z;
        }
    }
    return js_NewNumberInRootedValue(cx, z, vp);
}
Пример #2
0
static JSBool
math_pow(JSContext *cx, uintN argc, jsval *vp)
{
    jsdouble x, y, z;

    if (argc <= 1) {
        *vp = cx->runtime->NaNValue;
        return JS_TRUE;
    }
    x = js_ValueToNumber(cx, &vp[2]);
    if (JSVAL_IS_NULL(vp[2]))
        return JS_FALSE;
    y = js_ValueToNumber(cx, &vp[3]);
    if (JSVAL_IS_NULL(vp[3]))
        return JS_FALSE;
    /*
     * Because C99 and ECMA specify different behavior for pow(),
     * we need to wrap the libm call to make it ECMA compliant.
     */
    if (!JSDOUBLE_IS_FINITE(y) && (x == 1.0 || x == -1.0)) {
        *vp = cx->runtime->NaNValue;
        return JS_TRUE;
    }
    /* pow(x, +-0) is always 1, even for x = NaN. */
    if (y == 0) {
        *vp = JSVAL_ONE;
        return JS_TRUE;
    }
    z = pow(x, y);
    return js_NewNumberInRootedValue(cx, z, vp);
}
Пример #3
0
static JSBool
math_exp(JSContext *cx, uintN argc, jsval *vp)
{
    jsdouble x, z;

    if (argc == 0) {
        *vp = cx->runtime->NaNValue;
        return JS_TRUE;
    }
    x = js_ValueToNumber(cx, &vp[2]);
    if (JSVAL_IS_NULL(vp[2]))
        return JS_FALSE;
#ifdef _WIN32
    if (!JSDOUBLE_IS_NaN(x)) {
        if (x == js_PositiveInfinity) {
            *vp = cx->runtime->positiveInfinityValue;
            return JS_TRUE;
        }
        if (x == js_NegativeInfinity) {
            *vp = JSVAL_ZERO;
            return JS_TRUE;
        }
    }
#endif
    z = exp(x);
    return js_NewNumberInRootedValue(cx, z, vp);
}
Пример #4
0
static JSBool
math_tan(JSContext *cx, uintN argc, jsval *vp)
{
    jsdouble x, z;

    if (argc == 0) {
        *vp = cx->runtime->NaNValue;
        return JS_TRUE;
    }
    x = js_ValueToNumber(cx, &vp[2]);
    if (JSVAL_IS_NULL(vp[2]))
        return JS_FALSE;
    z = tan(x);
    return js_NewNumberInRootedValue(cx, z, vp);
}
Пример #5
0
JSBool
js_math_round(JSContext *cx, uintN argc, jsval *vp)
{
    jsdouble x, z;

    if (argc == 0) {
        *vp = cx->runtime->NaNValue;
        return JS_TRUE;
    }
    x = js_ValueToNumber(cx, &vp[2]);
    if (JSVAL_IS_NULL(vp[2]))
        return JS_FALSE;
    z = js_copysign(floor(x + 0.5), x);
    return js_NewNumberInRootedValue(cx, z, vp);
}
Пример #6
0
JSBool
js_math_floor(JSContext *cx, uintN argc, jsval *vp)
{
    jsdouble x, z;

    if (argc == 0) {
        *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
        return JS_TRUE;
    }
    x = js_ValueToNumber(cx, &vp[2]);
    if (JSVAL_IS_NULL(vp[2]))
        return JS_FALSE;
    z = floor(x);
    return js_NewNumberInRootedValue(cx, z, vp);
}
Пример #7
0
static JSBool
math_atan2(JSContext *cx, uintN argc, jsval *vp)
{
    jsdouble x, y;

    if (argc <= 1) {
        *vp = cx->runtime->NaNValue;
        return JS_TRUE;
    }
    x = js_ValueToNumber(cx, &vp[2]);
    if (JSVAL_IS_NULL(vp[2]))
        return JS_FALSE;
    y = js_ValueToNumber(cx, &vp[3]);
    if (JSVAL_IS_NULL(vp[3]))
        return JS_FALSE;
    return js_NewNumberInRootedValue(cx, math_atan2_kernel (x, y), vp);
}
Пример #8
0
static JSBool
math_log(JSContext *cx, uintN argc, jsval *vp)
{
    jsdouble x, z;

    if (argc == 0) {
        *vp = cx->runtime->NaNValue;
        return JS_TRUE;
    }
    x = js_ValueToNumber(cx, &vp[2]);
    if (JSVAL_IS_NULL(vp[2]))
        return JS_FALSE;
#if defined(SOLARIS) && defined(__GNUC__)
    if (x < 0) {
        *vp = cx->runtime->NaNValue;
        return JS_TRUE;
    }
#endif
    z = log(x);
    return js_NewNumberInRootedValue(cx, z, vp);
}
Пример #9
0
static JSBool
math_asin(JSContext *cx, uintN argc, jsval *vp)
{
    jsdouble x, z;

    if (argc == 0) {
        *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
        return JS_TRUE;
    }
    x = js_ValueToNumber(cx, &vp[2]);
    if (JSVAL_IS_NULL(vp[2]))
        return JS_FALSE;
#if defined(SOLARIS) && defined(__GNUC__)
    if (x < -1 || 1 < x) {
        *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
        return JS_TRUE;
    }
#endif
    z = asin(x);
    return js_NewNumberInRootedValue(cx, z, vp);
}
Пример #10
0
static JSBool
math_random(JSContext *cx, uintN argc, jsval *vp)
{
    jsdouble z = random_nextDouble(cx);
    return js_NewNumberInRootedValue(cx, z, vp);
}