简体   繁体   English

带整数的C ++矢量积

[英]C++ Vector Product with Integer

I have a weird Limit with my Datatype. 我的数据类型有一个怪异的限制。 Without explaining much, I will just show you. 在不多解释的情况下,我仅向您展示。

This is the Class. 这是班级。

class vecmath
{
private:
    std::vector<int> avec;
public:
    vecmath(std::string b);
    vecmath(std::vector<int>);
    ~vecmath();
    vecmath add(vecmath b);
    vecmath mult(vecmath b);
    vecmath prod(vecmath b);
    void read();
    std::string write();
    std::vector<int> getVec();
    vecmath operator+(vecmath a);
    vecmath operator*(vecmath a);
};

This is the constructor which is being used. 这是正在使用的构造函数。

vecmath::vecmath(std::string b) {
    for (unsigned long long i = 0; i < b.length(); ++i) {
        avec.push_back((int)(b[i]) - 48);
    }
}

This is the Add Function. 这是添加功能。 It is being called in the operator+ function which just returns it. 在operator +函数中调用它,仅返回它。

vecmath vecmath::add(vecmath b) {
    std::vector<int> cbvec = b.getVec();
    std::vector<int> cavec = avec;
    if (cavec.size() > cbvec.size()) {
        std::swap(cbvec, cavec);
    }

    std::vector<int> ret;
    unsigned long long asize = cavec.size();
    unsigned long long bsize = cbvec.size();

    std::reverse(cavec.begin(), cavec.end());
    std::reverse(cbvec.begin(), cbvec.end());

    int carry = 0;
    for (unsigned long long i = 0; i < asize; ++i) {

        int sum = (cavec[i] + cbvec[i] + carry);
        ret.push_back(sum % 10);

        carry = sum / 10;
    }

    for (unsigned long long i = asize; i < bsize; ++i) {
        int sum = (cbvec[i] + carry);
        ret.push_back(sum % 10);
        carry = sum / 10;
    }

    if (carry) {
        ret.push_back(carry);
    }

    std::reverse(ret.begin(), ret.end());

    return vecmath(ret);
}

This is the function which calculates a Product from 2 Vectors (1 is in the Class itself). 这是从2个向量计算乘积的函数(类本身有1个)。

vecmath vecmath::prod(vecmath b) {
    vecmath ret("0");
    std::vector<int> cbvec = b.getVec();
    std::vector<int> cavec = avec;

    if(cavec.size() > cbvec.size()) {
        std::swap(cavec, cbvec);
    }

    unsigned long long asize = cavec.size();
    unsigned long long bsize = cbvec.size();

    std::reverse(cavec.begin(), cavec.end());
    std::reverse(cbvec.begin(), cbvec.end());

    for(unsigned long long i = 0; i < asize; ++i) {
        for (unsigned long long j = 0; j < bsize; ++j) {
            ret = ret + vecmath(std::to_string(cavec[i] * cbvec[j] * nfzehnhoch(i) * nfzehnhoch(j)));
        }
    }

    return ret;
}

This is the nfzehnhoch function. 这是nfzehnhoch函数。

unsigned long long nfzehnhoch(unsigned int n) {
    unsigned long long ret = 1;
    for(unsigned int i = 0; i < n; ++i) {
        ret *= 10;
    }
    return ret;
}

This is my main Function. 这是我的主要功能。

int main(void) {
    vecmath a("2");
    vecmath c("2");

    for(int i = 1; i <= 100; ++i) {
        std::cout << c.write() << ", " << std::endl;
        c = c * a;
    }

    std::string end;
    std::getline(std::cin, end);
}

This is what I get when I run it. 这就是我运行它时得到的。

2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944, 549755813888, 1099511627776, 2199023255552, 4398046511104, 8796093022208, 17592186044416, 35184372088832, 70368744177664, 140737488355328, 281474976710656, 562949953421312, 1125899906842624, 2251799813685248, 4503599627370496, 9007199254740992, 18014398509481984, 36028797018963968, 72057594037927936, 144115188075855872, 288230376151711744, 576460752303423488, 1152921504606846976, 2305843009213693952, 4611686018427387904, 9223372036854775808, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 184467440737095 2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432, 67108864,134217728,268435456,536870912,1073741824,2147483648,4294967296,8589934592,17179869184,34359738368,68719476736,137438953472,274877906944,549755813888,1099511627776,2199023255552,4398046511104,87960930416968760382 926758968972 938762897 2251799813685248,4503599627370496,9007199254740992,18014398509481984,36028797018963968,72057594037927936,144115188075855872,288230376151711744,576460752303423488,1152921504606846976,2305843009213693952,4611686018427387904,9223372036854775808,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,184467440737095 51616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 18446744073709551616, 51616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616,18446744073709551616, 18446744073709551616,18446744073709551616,

As you see, it's stuck at 18446744073709551616. Maybe someone will just take a glance at it and find out. 如您所见,它卡在了18446744073709551616上,也许有人会一眼就知道了。

Thanks for any help. 谢谢你的帮助。

The value you get stuck at is 2^64. 卡住的值是2 ^ 64。 The underlying library is probably detecting the overflow and not doing the multiplication. 底层库可能正在检测溢出并且未进行乘法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM