diff --git a/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Libraries/LibJS/Runtime/CommonPropertyNames.h index 69df590559..c79625caee 100644 --- a/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/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) \ diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp index 024d242665..177229a069 100644 --- a/Libraries/LibJS/Runtime/MathObject.cpp +++ b/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); diff --git a/Libraries/LibJS/Runtime/MathObject.h b/Libraries/LibJS/Runtime/MathObject.h index 54e2544f4f..cf84b28344 100644 --- a/Libraries/LibJS/Runtime/MathObject.h +++ b/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); diff --git a/Libraries/LibJS/Tests/builtins/Math/Math.atan.js b/Libraries/LibJS/Tests/builtins/Math/Math.atan.js new file mode 100644 index 0000000000..ff526fa367 --- /dev/null +++ b/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); +});