最近写int128发现乘法容易越界,于是想到这个办法
再做一次分割
总位数30位(十进制)
int128 mul(int128 a, int128 b) {
int128 c;
c.hig = a.hig * b.hig * 1000000000000000ll;
c.hig += a.hig * b.low;
c.hig += a.low * b.hig;
long long all = a.low % 10000000ll,bll= b.low % 10000000ll;
long long alh = a.low / 10000000ll, blh = b.low / 10000000ll;
c.low = all * bll;
long long lhh = alh * blh;
c.low += lhh % 10ll * 100000000000000ll;
c.hig += lhh / 10;
long long xlh = all * blh, ylh = alh * bll;
c.low += xlh % 100000000ll * 10000000ll;
c.low += ylh % 100000000ll * 10000000ll;
c.hig += xlh / 100000000ll;
c.hig += ylh / 100000000ll;
c.hig += c.low / 1000000000000000ll;
c.low %= 1000000000000000ll;
return c;
}