0

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.

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).

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.

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, 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, 18446744073709551616,

As you see, it's stuck at 18446744073709551616. Maybe someone will just take a glance at it and find out.

Thanks for any help.

6
  • 1
    Please provide a minimal reproducible example. Starting with line 1 we cannot possibly know what vecmath::prod is actually doing Commented Mar 13, 2019 at 20:27
  • vecmath::prod is calculating the Product of any given Vector b and the Vector in vecmath itself. Commented Mar 13, 2019 at 20:29
  • apparently it does not, otherwise you would not be asking here. Please take the tour and read the help center. Among other things you will get informed that questions seeking debugging help are required to include a minimal reproducible example, desired and expected output Commented Mar 13, 2019 at 20:30
  • Among the code that is missing is the vecmath class definition, constructor definition, operator+ with vecmath operators, and the nfzehnhoch function. Commented Mar 13, 2019 at 20:33
  • Ok, I have updated it. The nfzehnhoch function was explained. Commented Mar 13, 2019 at 20:43

1 Answer 1

0

The value you get stuck at is 2^64. The underlying library is probably detecting the overflow and not doing the multiplication.

Sign up to request clarification or add additional context in comments.

1 Comment

the example is calculating powers of 2, maybe it is just coincidence that it fails after 64 steps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.