PrevUpHomeNext

utx::mod


template <std::integral T>
inline constexpr T utx::mod(const T & dividend, const T & divisor);

Take the remaining operation.

Header

#include <utxcpp/core.hpp>

Negative Mod Operation

In utxcpp 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.

Notice

[Note] Note
  • dividend and divisor must satisfy std::integral
  • dividend and divisor must be the same type.

Example

#include <utxcpp/core.hpp>

int main() {
	utx::i32 a = utx::mod(37, 7);
	utx::i32 b = utx::mod(101, -9);
	utx::i32 c = utx::mod(-23, 3);
	utx::i32 d = utx::mod(-104, -23);
	utx::print(a, b, c, d);
}

Result:

2 -7 1 -12

PrevUpHomeNext

utx::print

esv::print