1
0
Fork 0

Add number/oint.cc

Signed-off-by: subcrip <contact@subc.rip>
This commit is contained in:
subcrip 2024-10-28 14:53:33 +08:00
parent 1496b2c80c
commit 75a10aca7b
1 changed files with 28 additions and 0 deletions

28
number/oint.cc Normal file
View File

@ -0,0 +1,28 @@
template <typename T, T MDL, T MAX = INT_MAX>
struct oint {
static_assert(MDL > MAX, "Number will never overflow because modulus is too small");
bool carry;
T val;
oint() : carry(0), val(0) {}
oint(const T& v) : carry(v > MAX), val(v % MDL) {}
friend oint operator*(const oint& a, const oint& b) {
oint res;
res.carry = a.carry | b.carry;
if (not res.carry and a.val > MAX / b.val) {
res.carry = 1;
}
res.val = (a.val * b.val) % MDL;
return res;
}
friend oint operator+(const oint& a, const oint& b) {
oint res;
res.carry = a.carry | b.carry;
if (not res.carry and a.val > MAX - b.val) {
res.carry = 1;
}
res.val = (a.val + b.val) % MDL;
return res;
}
oint& operator+=(const oint& rhs) { return *this = *this + rhs; }
oint& operator*=(const oint& rhs) { return *this = *this * rhs; }
};