1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:07:35 +00:00

LibJS: expose some more math functions

This commit is contained in:
stelar7 2020-06-21 11:34:00 +02:00 committed by Andreas Kling
parent b97da17b90
commit 9e18005c64
7 changed files with 124 additions and 2 deletions

View file

@ -60,6 +60,11 @@ void MathObject::initialize(Interpreter& interpreter, GlobalObject& global_objec
define_native_function("expm1", expm1, 1, attr);
define_native_function("sign", sign, 1, attr);
define_native_function("clz32", clz32, 1, attr);
define_native_function("acosh", acosh, 1, attr);
define_native_function("asinh", asinh, 1, attr);
define_native_function("atanh", atanh, 1, attr);
define_native_function("log1p", log1p, 1, attr);
define_native_function("cbrt", cbrt, 1, attr);
define_property("E", Value(M_E), 0);
define_property("LN2", Value(M_LN2), 0);
@ -223,7 +228,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::exp)
return {};
if (number.is_nan())
return js_nan();
return Value(::pow(M_E, number.as_double()));
return Value(::exp(number.as_double()));
}
JS_DEFINE_NATIVE_FUNCTION(MathObject::expm1)
@ -233,7 +238,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::expm1)
return {};
if (number.is_nan())
return js_nan();
return Value(::pow(M_E, number.as_double()) - 1);
return Value(::expm1(number.as_double()));
}
JS_DEFINE_NATIVE_FUNCTION(MathObject::sign)
@ -262,4 +267,50 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::clz32)
return Value(__builtin_clz((unsigned)number.as_double()));
}
JS_DEFINE_NATIVE_FUNCTION(MathObject::acosh)
{
auto number = interpreter.argument(0).to_number(interpreter);
if (interpreter.exception())
return {};
if (number.as_double() < 1)
return JS::js_nan();
return Value(::acosh(number.as_double()));
}
JS_DEFINE_NATIVE_FUNCTION(MathObject::asinh)
{
auto number = interpreter.argument(0).to_number(interpreter);
if (interpreter.exception())
return {};
return Value(::asinh(number.as_double()));
}
JS_DEFINE_NATIVE_FUNCTION(MathObject::atanh)
{
auto number = interpreter.argument(0).to_number(interpreter);
if (interpreter.exception())
return {};
if (number.as_double() > 1 || number.as_double() < -1)
return JS::js_nan();
return Value(::atanh(number.as_double()));
}
JS_DEFINE_NATIVE_FUNCTION(MathObject::log1p)
{
auto number = interpreter.argument(0).to_number(interpreter);
if (interpreter.exception())
return {};
if (number.as_double() < -1)
return JS::js_nan();
return Value(::log1p(number.as_double()));
}
JS_DEFINE_NATIVE_FUNCTION(MathObject::cbrt)
{
auto number = interpreter.argument(0).to_number(interpreter);
if (interpreter.exception())
return {};
return Value(::cbrt(number.as_double()));
}
}

View file

@ -56,6 +56,11 @@ private:
JS_DECLARE_NATIVE_FUNCTION(expm1);
JS_DECLARE_NATIVE_FUNCTION(sign);
JS_DECLARE_NATIVE_FUNCTION(clz32);
JS_DECLARE_NATIVE_FUNCTION(acosh);
JS_DECLARE_NATIVE_FUNCTION(asinh);
JS_DECLARE_NATIVE_FUNCTION(atanh);
JS_DECLARE_NATIVE_FUNCTION(log1p);
JS_DECLARE_NATIVE_FUNCTION(cbrt);
};
}

View file

@ -0,0 +1,13 @@
load("test-common.js");
try {
assert(isNaN(Math.acosh(-1)));
assert(isNaN(Math.acosh(0)));
assert(isNaN(Math.acosh(0.5)));
assert(isClose(Math.acosh(1), 0));
assert(isClose(Math.acosh(2), 1.316957));
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -0,0 +1,10 @@
load("test-common.js");
try {
assert(isClose(Math.asinh(0), 0));
assert(isClose(Math.asinh(1), 0.881373));
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -0,0 +1,14 @@
load("test-common.js");
try {
assert(isNaN(Math.atanh(-2)));
assert(Math.atanh(-1) === -Infinity);
assert(Math.atanh(0) === 0);
assert(isClose(Math.atanh(0.5), 0.549306));
assert(Math.atanh(1) === Infinity);
assert(isNaN(Math.atanh(2)));
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -0,0 +1,16 @@
load("test-common.js");
try {
assert(isNaN(Math.cbrt(NaN)));
assert(Math.cbrt(-1) === -1);
assert(Math.cbrt(-0) === -0);
assert(Math.cbrt(-Infinity) === -Infinity);
assert(Math.cbrt(1) === 1);
assert(Math.cbrt(Infinity) === Infinity);
assert(Math.cbrt(null) === 0);
assert(isClose(Math.cbrt(2), 1.259921));
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -0,0 +1,13 @@
load("test-common.js");
try {
assert(isNaN(Math.log1p(-2)));
assert(Math.log1p(-1) === -Infinity);
assert(Math.log1p(0) === 0);
assert(isClose(Math.log1p(1), 0.693147));
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}