diff options
author | Linus Groh <mail@linusgroh.de> | 2020-04-05 21:21:33 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-06 10:58:16 +0200 |
commit | f5dacfbb5bf52dd879e61d415756365d58f42e3e (patch) | |
tree | 08c34c1286a9ed32a224e3340a51bf7596695823 /Libraries | |
parent | 04a36b247bcecb5b072af453bd3bce0e33afb6a8 (diff) | |
download | serenity-f5dacfbb5bf52dd879e61d415756365d58f42e3e.zip |
LibJS: Add Math.{cos,sin,tan}()
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/Runtime/MathObject.cpp | 27 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/MathObject.h | 3 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/Math.cos.js | 16 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/Math.sin.js | 17 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/Math.tan.js | 16 |
5 files changed, 79 insertions, 0 deletions
diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp index 19a2e50fb4..c5b0cf2eac 100644 --- a/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Libraries/LibJS/Runtime/MathObject.cpp @@ -43,6 +43,9 @@ MathObject::MathObject() put_native_function("max", max, 2); put_native_function("min", min, 2); put_native_function("trunc", trunc, 1); + put_native_function("sin", sin, 1); + put_native_function("cos", cos, 1); + put_native_function("tan", tan, 1); put("E", Value(M_E)); put("LN2", Value(M_LN2)); @@ -152,4 +155,28 @@ Value MathObject::trunc(Interpreter& interpreter) return MathObject::floor(interpreter); } +Value MathObject::sin(Interpreter& interpreter) +{ + auto number = interpreter.argument(0).to_number(); + if (number.is_nan()) + return js_nan(); + return Value(::sin(number.as_double())); +} + +Value MathObject::cos(Interpreter& interpreter) +{ + auto number = interpreter.argument(0).to_number(); + if (number.is_nan()) + return js_nan(); + return Value(::cos(number.as_double())); +} + +Value MathObject::tan(Interpreter& interpreter) +{ + auto number = interpreter.argument(0).to_number(); + if (number.is_nan()) + return js_nan(); + return Value(::tan(number.as_double())); +} + } diff --git a/Libraries/LibJS/Runtime/MathObject.h b/Libraries/LibJS/Runtime/MathObject.h index 3ab551d341..9a80d6689c 100644 --- a/Libraries/LibJS/Runtime/MathObject.h +++ b/Libraries/LibJS/Runtime/MathObject.h @@ -47,6 +47,9 @@ private: static Value max(Interpreter&); static Value min(Interpreter&); static Value trunc(Interpreter&); + static Value sin(Interpreter&); + static Value cos(Interpreter&); + static Value tan(Interpreter&); }; } diff --git a/Libraries/LibJS/Tests/Math.cos.js b/Libraries/LibJS/Tests/Math.cos.js new file mode 100644 index 0000000000..2af3d7dbb8 --- /dev/null +++ b/Libraries/LibJS/Tests/Math.cos.js @@ -0,0 +1,16 @@ +try { + assert(Math.cos(0) === 1); + assert(Math.cos(null) === 1); + assert(Math.cos('') === 1); + assert(Math.cos([]) === 1); + assert(Math.cos(Math.PI) === -1); + assert(isNaN(Math.cos())); + assert(isNaN(Math.cos(undefined))); + assert(isNaN(Math.cos([1, 2, 3]))); + assert(isNaN(Math.cos({}))); + assert(isNaN(Math.cos("foo"))); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/Math.sin.js b/Libraries/LibJS/Tests/Math.sin.js new file mode 100644 index 0000000000..bd93e66890 --- /dev/null +++ b/Libraries/LibJS/Tests/Math.sin.js @@ -0,0 +1,17 @@ +try { + assert(Math.sin(0) === 0); + assert(Math.sin(null) === 0); + assert(Math.sin('') === 0); + assert(Math.sin([]) === 0); + assert(Math.sin(Math.PI * 3 / 2) === -1); + assert(Math.sin(Math.PI / 2) === 1); + assert(isNaN(Math.sin())); + assert(isNaN(Math.sin(undefined))); + assert(isNaN(Math.sin([1, 2, 3]))); + assert(isNaN(Math.sin({}))); + assert(isNaN(Math.sin("foo"))); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/Math.tan.js b/Libraries/LibJS/Tests/Math.tan.js new file mode 100644 index 0000000000..07b9ff73f4 --- /dev/null +++ b/Libraries/LibJS/Tests/Math.tan.js @@ -0,0 +1,16 @@ +try { + assert(Math.tan(0) === 0); + assert(Math.tan(null) === 0); + assert(Math.tan('') === 0); + assert(Math.tan([]) === 0); + assert(Math.ceil(Math.tan(Math.PI / 4)) === 1); + assert(isNaN(Math.tan())); + assert(isNaN(Math.tan(undefined))); + assert(isNaN(Math.tan([1, 2, 3]))); + assert(isNaN(Math.tan({}))); + assert(isNaN(Math.tan("foo"))); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} |