Explorar o código

LibJS: Add Math.atan()

Andreas Kling %!s(int64=4) %!d(string=hai) anos
pai
achega
63b748642a

+ 1 - 0
Libraries/LibJS/Runtime/CommonPropertyNames.h

@@ -66,6 +66,7 @@ namespace JS {
     P(asIntN)                                \
     P(asUintN)                               \
     P(asinh)                                 \
+    P(atan)                                  \
     P(atanh)                                 \
     P(bind)                                  \
     P(byteLength)                            \

+ 15 - 0
Libraries/LibJS/Runtime/MathObject.cpp

@@ -62,6 +62,7 @@ void MathObject::initialize(GlobalObject& global_object)
     define_native_function(vm.names.clz32, clz32, 1, attr);
     define_native_function(vm.names.acosh, acosh, 1, attr);
     define_native_function(vm.names.asinh, asinh, 1, attr);
+    define_native_function(vm.names.atan, atan, 1, attr);
     define_native_function(vm.names.atanh, atanh, 1, attr);
     define_native_function(vm.names.log1p, log1p, 1, attr);
     define_native_function(vm.names.cbrt, cbrt, 1, attr);
@@ -290,6 +291,20 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::asinh)
     return Value(::asinh(number.as_double()));
 }
 
+JS_DEFINE_NATIVE_FUNCTION(MathObject::atan)
+{
+    auto number = vm.argument(0).to_number(global_object);
+    if (vm.exception())
+        return {};
+    if (number.is_nan() || number.is_positive_zero() || number.is_negative_zero())
+        return number;
+    if (number.is_positive_infinity())
+        return Value(M_PI_2);
+    if (number.is_negative_infinity())
+        return Value(-M_PI_2);
+    return Value(::atan(number.as_double()));
+}
+
 JS_DEFINE_NATIVE_FUNCTION(MathObject::atanh)
 {
     auto number = vm.argument(0).to_number(global_object);

+ 1 - 0
Libraries/LibJS/Runtime/MathObject.h

@@ -58,6 +58,7 @@ private:
     JS_DECLARE_NATIVE_FUNCTION(clz32);
     JS_DECLARE_NATIVE_FUNCTION(acosh);
     JS_DECLARE_NATIVE_FUNCTION(asinh);
+    JS_DECLARE_NATIVE_FUNCTION(atan);
     JS_DECLARE_NATIVE_FUNCTION(atanh);
     JS_DECLARE_NATIVE_FUNCTION(log1p);
     JS_DECLARE_NATIVE_FUNCTION(cbrt);

+ 12 - 0
Libraries/LibJS/Tests/builtins/Math/Math.atan.js

@@ -0,0 +1,12 @@
+test("basic functionality", () => {
+    expect(Math.atan).toHaveLength(1);
+
+    expect(Math.atan(0)).toBe(0);
+    expect(Math.atan(-0)).toBe(-0);
+    expect(Math.atan(NaN)).toBeNaN();
+    expect(Math.atan(-2)).toBeCloseTo(-1.1071487177940904);
+    expect(Math.atan(2)).toBeCloseTo(1.1071487177940904);
+    expect(Math.atan(Infinity)).toBeCloseTo(Math.PI / 2);
+    expect(Math.atan(-Infinity)).toBeCloseTo(-Math.PI / 2);
+    expect(Math.atan(0.5)).toBeCloseTo(0.4636476090008061);
+});