diff options
author | davidot <davidot@serenityos.org> | 2022-08-20 16:24:44 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-08-20 23:53:55 +0100 |
commit | b79f03182d98a10ee234ae33fe68d4a8f3d95406 (patch) | |
tree | 89f04647325763d40584f7fa41dfce1ac82c3876 /Userland/Libraries/LibJS/Runtime | |
parent | 379baa984d6864cd8c8976d708e3f39d621b46e5 (diff) | |
download | serenity-b79f03182d98a10ee234ae33fe68d4a8f3d95406.zip |
LibJS: Add special cases for Math.cosh and add spec comments
Although this already works in most cases in non-kvm serenity cases the
cosh and other math function tend to return incorrect values for
Infinity. This makes sure that whatever the underlying cosh function
returns Math.cosh conforms to the spec.
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/MathObject.cpp | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/MathObject.cpp b/Userland/Libraries/LibJS/Runtime/MathObject.cpp index b76da01547..291472e0fd 100644 --- a/Userland/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/MathObject.cpp @@ -498,9 +498,22 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::sinh) // 21.3.2.13 Math.cosh ( x ), https://tc39.es/ecma262/#sec-math.cosh JS_DEFINE_NATIVE_FUNCTION(MathObject::cosh) { + // 1. Let n be ? ToNumber(x). auto number = TRY(vm.argument(0).to_number(global_object)); + + // 2. If n is NaN, return NaN. if (number.is_nan()) return js_nan(); + + // 3. If n is +∞𝔽 or n is -∞𝔽, return +∞𝔽. + if (number.is_positive_infinity() || number.is_negative_infinity()) + return js_infinity(); + + // 4. If n is +0𝔽 or n is -0𝔽, return 1𝔽. + if (number.is_positive_zero() || number.is_negative_zero()) + return Value(1); + + // 5. Return an implementation-approximated Number value representing the result of the hyperbolic cosine of ℝ(n). return Value(::cosh(number.as_double())); } |