summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorstelar7 <dudedbz@gmail.com>2020-06-21 11:34:00 +0200
committerAndreas Kling <kling@serenityos.org>2020-06-22 10:33:50 +0200
commit9e18005c642674e5e9990eda292c058f001af745 (patch)
tree66868a728d0f6c8b45f9bf72a27e6e217d6635ef /Libraries
parentb97da17b904873b8af339894213ccc6d4e787688 (diff)
downloadserenity-9e18005c642674e5e9990eda292c058f001af745.zip
LibJS: expose some more math functions
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibJS/Runtime/MathObject.cpp55
-rw-r--r--Libraries/LibJS/Runtime/MathObject.h5
-rw-r--r--Libraries/LibJS/Tests/Math.acosh.js13
-rw-r--r--Libraries/LibJS/Tests/Math.asinh.js10
-rw-r--r--Libraries/LibJS/Tests/Math.atanh.js14
-rw-r--r--Libraries/LibJS/Tests/Math.cbrt.js16
-rw-r--r--Libraries/LibJS/Tests/Math.log1p.js13
7 files changed, 124 insertions, 2 deletions
diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp
index 7cf2432130..86759e63a9 100644
--- a/Libraries/LibJS/Runtime/MathObject.cpp
+++ b/Libraries/LibJS/Runtime/MathObject.cpp
@@ -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()));
+}
+
}
diff --git a/Libraries/LibJS/Runtime/MathObject.h b/Libraries/LibJS/Runtime/MathObject.h
index 2455ead29c..3663235607 100644
--- a/Libraries/LibJS/Runtime/MathObject.h
+++ b/Libraries/LibJS/Runtime/MathObject.h
@@ -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);
};
}
diff --git a/Libraries/LibJS/Tests/Math.acosh.js b/Libraries/LibJS/Tests/Math.acosh.js
new file mode 100644
index 0000000000..73c8e43c8a
--- /dev/null
+++ b/Libraries/LibJS/Tests/Math.acosh.js
@@ -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);
+}
diff --git a/Libraries/LibJS/Tests/Math.asinh.js b/Libraries/LibJS/Tests/Math.asinh.js
new file mode 100644
index 0000000000..98c3ef297d
--- /dev/null
+++ b/Libraries/LibJS/Tests/Math.asinh.js
@@ -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);
+}
diff --git a/Libraries/LibJS/Tests/Math.atanh.js b/Libraries/LibJS/Tests/Math.atanh.js
new file mode 100644
index 0000000000..c09003859c
--- /dev/null
+++ b/Libraries/LibJS/Tests/Math.atanh.js
@@ -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);
+}
diff --git a/Libraries/LibJS/Tests/Math.cbrt.js b/Libraries/LibJS/Tests/Math.cbrt.js
new file mode 100644
index 0000000000..a274f1c088
--- /dev/null
+++ b/Libraries/LibJS/Tests/Math.cbrt.js
@@ -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);
+}
diff --git a/Libraries/LibJS/Tests/Math.log1p.js b/Libraries/LibJS/Tests/Math.log1p.js
new file mode 100644
index 0000000000..6b167bb880
--- /dev/null
+++ b/Libraries/LibJS/Tests/Math.log1p.js
@@ -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);
+}