简体   繁体   中英

Issue with function that adds large integers stored in vectors

So I' ve decided to write my own multiprecision data type. I've written a simple function that adds large numbers stored in vector<uint_fast8_t> .

vector<uint_fast8_t> Add(vector<uint_fast8_t > x, vector<uint_fast8_t > y){
    unsigned int x_size = x.size() / sizeof(uint_fast8_t);
    unsigned int y_size = y.size() / sizeof(uint_fast8_t);
    unsigned int res_size{};
    if(x_size>y_size){
        res_size = x_size;
        y.insert(y.end(),uint_fast8_t(0), res_size-y_size);
    } else{
        res_size = x_size;
        x.insert(x.end(),uint_fast8_t(0), res_size-x_size);
    }
    reverse(x.begin(), x.end());
    reverse(y.begin(), y.end());
    vector<uint_fast8_t > res(res_size, 0);
    for(unsigned int i = 0; i < res_size; ++i){
        uint_fast8_t curr = res[i] + x[i] + y[i];
        if(curr >= 10){
            if(i==res_size){
                res.push_back(uint_fast8_t(1));
            } else{
                res[i+1] = uint_fast8_t(1);
            }
            res[i] = curr - uint_fast8_t(10);
        } else{
            res[i] = curr;
        }
    }
    reverse(res.begin(), res.end());
    return res;
}

Issue This function works only for numbers from 0 to 10000000 ( 10000000 is vector<uint_fast8_t>{1,0,0,0,0,0,0,0} ). For larger numbers the results are crazy. For example it spits out 10000000000 + 123 + = 1012300000123 . Why is that happening? Edit 1 I was asked about this division x.size() / sizeof(uint_fast8_t) . As far as I know it returns the size of object in bytes. I divide it by size of uint_fast8_t to get the number of elements in vector. It seemed to work well. Maybe I misunderstood something.

This can be expressed much simpler, usingstd::vector::resize , std::transform and an appropriate function object.

using Multiprecision = std::vector<uint_fast8_t>;

Multiprecision Add(Multiprecision x, Multiprecision y){

    auto common_size = std::max(x.size(), y.size());
    x.resize(common_size);
    y.resize(common_size);
    Multiprecision res(common_size);

    bool carry = false;
    std::transform(x.begin(), x.end(), y.begin(), res.begin(), [&carry](uint_fast8_t x, uint_fast8_t y){ 
        uint_fast8_t value = x + y + carry; 
        carry = (value >= 10);
        return value - (carry * 10);
    });

    return res;
}

Ok I fixed it

Thank you for pointing out some mistakes in my code. I am a begginer so it's important to me. With your help I managed to fix the issue. I think the problem was in pre sizing the vector. Here's working code: (I think the insert function can insert many zeros without for loop. I'll check an maybe replace it)

vector<uint_fast8_t> Add(vector<uint_fast8_t > x, vector<uint_fast8_t > y) {
unsigned long x_size = x.size();
unsigned long y_size = y.size();
unsigned long res_size{};
uint_fast8_t curr = 0;
vector<uint_fast8_t > res{};

if(x_size>y_size){
    res_size = x_size;
    for(unsigned int i = 0; i < res_size - y_size; ++i){
        y.insert(y.begin(), uint_fast8_t(0));
    }
} else{
    res_size = y_size;
    for(unsigned int j = 0; j < res_size - x_size; ++j){
        x.insert(x.begin(), uint_fast8_t(0));
    }
}

reverse(x.begin(), x.end());
reverse(y.begin(), y.end());

for(unsigned int k = 0; k < res_size; ++k){
    curr += x[k] + y[k];
    if(curr >= 10){
        res.push_back(curr - uint_fast8_t(10));
        curr = 1;
        if(k == res_size -1){
            res.push_back(curr);
        }
    } else{
        res.push_back(curr);
        curr = 0;
    }
}

reverse(res.begin(), res.end());

return res;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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