mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:37:35 +00:00
LibJS: Support modulo(x, y) with different types
It's a bit annoying having to add '.0' to y given that it's an integral number in most cases. This turns the single template parameter T into T and U to permit that.
This commit is contained in:
parent
1ec917aa23
commit
9c209b8079
5 changed files with 15 additions and 14 deletions
|
@ -77,13 +77,14 @@ ThrowCompletionOr<T*> ordinary_create_from_constructor(GlobalObject& global_obje
|
|||
}
|
||||
|
||||
// x modulo y, https://tc39.es/ecma262/#eqn-modulo
|
||||
template<typename T>
|
||||
T modulo(T x, T y)
|
||||
template<typename T, typename U>
|
||||
auto modulo(T x, U y) requires(IsArithmetic<T>, IsArithmetic<U>)
|
||||
{
|
||||
// The notation “x modulo y” (y must be finite and non-zero) computes a value k of the same sign as y (or zero) such that abs(k) < abs(y) and x - k = q × y for some integer q.
|
||||
VERIFY(y != 0);
|
||||
if constexpr (IsFloatingPoint<T>) {
|
||||
VERIFY(isfinite(y));
|
||||
if constexpr (IsFloatingPoint<T> || IsFloatingPoint<U>) {
|
||||
if constexpr (IsFloatingPoint<U>)
|
||||
VERIFY(isfinite(y));
|
||||
return fmod(fmod(x, y) + y, y);
|
||||
} else {
|
||||
return ((x % y) + y) % y;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue