mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:27:45 +00:00
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.
This commit is contained in:
parent
cc083310a7
commit
af586dde64
1 changed files with 21 additions and 0 deletions
|
@ -316,4 +316,25 @@ auto modulo(Crypto::BigInteger auto const& x, Crypto::BigInteger auto const& y)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// remainder(x, y), https://tc39.es/proposal-temporal/#eqn-remainder
|
||||||
|
template<Arithmetic T, Arithmetic U>
|
||||||
|
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<T> || IsFloatingPoint<U>) {
|
||||||
|
if constexpr (IsFloatingPoint<U>)
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue