summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-05-18 15:21:37 +0100
committerAndreas Kling <kling@serenityos.org>2020-05-18 17:57:28 +0200
commit452dbbc463625eee1800d542b6613b4ef875bb80 (patch)
tree6232e424821d56e2d35ac46f17ab276a733fa8ea /Libraries
parente375766f9865144994caac17cf7d19371a3c7fb1 (diff)
downloadserenity-452dbbc463625eee1800d542b6613b4ef875bb80.zip
LibJS: Add Math.expm1()
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibJS/Runtime/MathObject.cpp11
-rw-r--r--Libraries/LibJS/Runtime/MathObject.h1
-rw-r--r--Libraries/LibJS/Tests/Math.expm1.js19
3 files changed, 31 insertions, 0 deletions
diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp
index e4f6072a58..2b173bcc70 100644
--- a/Libraries/LibJS/Runtime/MathObject.cpp
+++ b/Libraries/LibJS/Runtime/MathObject.cpp
@@ -51,6 +51,7 @@ MathObject::MathObject()
put_native_function("tan", tan, 1, attr);
put_native_function("pow", pow, 2, attr);
put_native_function("exp", exp, 1, attr);
+ put_native_function("expm1", expm1, 1, attr);
put_native_function("sign", sign, 1, attr);
put("E", Value(M_E), 0);
@@ -218,6 +219,16 @@ Value MathObject::exp(Interpreter& interpreter)
return Value(::pow(M_E, number.as_double()));
}
+Value MathObject::expm1(Interpreter& interpreter)
+{
+ auto number = interpreter.argument(0).to_number(interpreter);
+ if (interpreter.exception())
+ return {};
+ if (number.is_nan())
+ return js_nan();
+ return Value(::pow(M_E, number.as_double()) - 1);
+}
+
Value MathObject::sign(Interpreter& interpreter)
{
auto number = interpreter.argument(0).to_number(interpreter);
diff --git a/Libraries/LibJS/Runtime/MathObject.h b/Libraries/LibJS/Runtime/MathObject.h
index 31d9b7fd03..e9902e6d53 100644
--- a/Libraries/LibJS/Runtime/MathObject.h
+++ b/Libraries/LibJS/Runtime/MathObject.h
@@ -52,6 +52,7 @@ private:
static Value tan(Interpreter&);
static Value pow(Interpreter&);
static Value exp(Interpreter&);
+ static Value expm1(Interpreter&);
static Value sign(Interpreter&);
};
diff --git a/Libraries/LibJS/Tests/Math.expm1.js b/Libraries/LibJS/Tests/Math.expm1.js
new file mode 100644
index 0000000000..45f3c2be54
--- /dev/null
+++ b/Libraries/LibJS/Tests/Math.expm1.js
@@ -0,0 +1,19 @@
+load("test-common.js");
+
+try {
+ assert(Math.expm1.length === 1);
+
+ assert(Math.expm1(0) === 0);
+ assert(isClose(Math.expm1(-2), -0.864664));
+ assert(isClose(Math.expm1(-1), -0.632120));
+ assert(isClose(Math.expm1(1), 1.718281));
+ assert(isClose(Math.expm1(2), 6.389056));
+
+ assert(isNaN(Math.expm1()));
+ assert(isNaN(Math.expm1(undefined)));
+ assert(isNaN(Math.expm1("foo")));
+
+ console.log("PASS");
+} catch (e) {
+ console.log("FAIL: " + e);
+}