People give birds love, birds make people happy.
template <std::integral T> inline constexpr T esv::mod(const T & dividend, const T & divisor);
Take the remaining operation.
#include <esvcpp/core.hpp>
In esvcpp math, x/y means calculate the distance from x to scope [0, y] in y units.
For example, 7/3 will get 2, because there are 2 of 3 units from 7 to [0, 3].
So what is the remaining number? The remaining number must be in scope [0, y) after taking the result from x.
I think you have understood it now. The mod result must be in scope [0, y).
Examples:
21 % 9 == 3 ---------------- because 21 == 9*2 + 3, and 3 is in scope [0, 9)
(-17) % 5 == 3 ---------------- because -17 == 5*(-4) + 3, and 3 is in scope [0, 5)
13 % (-3) == -2 ---------------- because 13 == (-3)*(-5) + (-2), and -2 is in scope [0, -3)
x/y x%y
x - the dividend
y - the divisor
Conclusion
The mod operation x%y means clip the value x to the scope [0, y), it will remove the excess value and keep the real value without redundancy.
Note | |
---|---|
|
#include <esvcpp/core.hpp> int main() { esv::i32 a = esv::mod(37, 7); esv::i32 b = esv::mod(101, -9); esv::i32 c = esv::mod(-23, 3); esv::i32 d = esv::mod(-104, -23); esv::print(a, b, c, d); }
Result:
2 -7 1 -12