summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/MathObject.cpp
diff options
context:
space:
mode:
authordavidot <davidot@serenityos.org>2022-11-28 12:06:50 +0100
committerAndreas Kling <kling@serenityos.org>2022-11-28 13:10:21 +0100
commit4306462a95ee1851de67c8729114fe6a7d653740 (patch)
treeab9bce463b3a5f65ad62603144e9e15da3838bf2 /Userland/Libraries/LibJS/Runtime/MathObject.cpp
parenteda90b54d4b15771938b61f867e1823888e0411c (diff)
downloadserenity-4306462a95ee1851de67c8729114fe6a7d653740.zip
LibJS: Add spec comments and check for edge cases in Math.log10
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/MathObject.cpp')
-rw-r--r--Userland/Libraries/LibJS/Runtime/MathObject.cpp23
1 files changed, 20 insertions, 3 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/MathObject.cpp b/Userland/Libraries/LibJS/Runtime/MathObject.cpp
index c0b506e0b9..9baec14c3b 100644
--- a/Userland/Libraries/LibJS/Runtime/MathObject.cpp
+++ b/Userland/Libraries/LibJS/Runtime/MathObject.cpp
@@ -574,10 +574,27 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::log2)
// 21.3.2.22 Math.log10 ( x ), https://tc39.es/ecma262/#sec-math.log10
JS_DEFINE_NATIVE_FUNCTION(MathObject::log10)
{
- auto value = TRY(vm.argument(0).to_number(vm)).as_double();
- if (value < 0)
+ // 1. Let n be ? ToNumber(x).
+ auto number = TRY(vm.argument(0).to_number(vm));
+
+ // 2. If n is NaN or n is +∞𝔽, return n.
+ if (number.is_nan() || number.is_positive_infinity())
+ return number;
+
+ // 3. If n is 1𝔽, return +0𝔽.
+ if (number.as_double() == 1.)
+ return Value(0);
+
+ // 4. If n is +0𝔽 or n is -0𝔽, return -∞𝔽.
+ if (number.is_positive_zero() || number.is_negative_zero())
+ return js_negative_infinity();
+
+ // 5. If n < -0𝔽, return NaN.
+ if (number.as_double() < -0.)
return js_nan();
- return Value(::log10(value));
+
+ // 6. Return an implementation-approximated Number value representing the result of the base 10 logarithm of ℝ(n).
+ return Value(::log10(number.as_double()));
}
// 21.3.2.31 Math.sinh ( x ), https://tc39.es/ecma262/#sec-math.sinh