From af586dde644f285d60f471b67ff5bcba1e3f4d33 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Mon, 8 Jan 2024 19:20:31 +1300 Subject: [PATCH] LibJS: Add a remainder() function to represent remainder(x, y) This is just the same as calling x % y - or fmod, and is implemented for symmetry with the 'modulo' function. --- .../LibJS/Runtime/AbstractOperations.h | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/AbstractOperations.h index 119804563d..f4bc039fab 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.h @@ -316,4 +316,25 @@ auto modulo(Crypto::BigInteger auto const& x, Crypto::BigInteger auto const& y) return result; } +// remainder(x, y), https://tc39.es/proposal-temporal/#eqn-remainder +template +auto remainder(T x, U y) +{ + // The mathematical function remainder(x, y) produces the mathematical value whose sign is the sign of x and whose magnitude is abs(x) modulo y. + VERIFY(y != 0); + if constexpr (IsFloatingPoint || IsFloatingPoint) { + if constexpr (IsFloatingPoint) + VERIFY(isfinite(y)); + return fmod(x, y); + } else { + return x % y; + } +} + +auto remainder(Crypto::BigInteger auto const& x, Crypto::BigInteger auto const& y) +{ + VERIFY(!y.is_zero()); + return x.divided_by(y).remainder; +} + }