简体   繁体   English

添加存储在向量中的大整数的函数的问题

[英]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>大量数字。

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} ).问题此函数仅适用于 0 到 10000000 之间的数字( 10000000vector<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 .例如,它吐出10000000000 + 123 + = 1012300000123 Why is that happening?为什么会这样? Edit 1 I was asked about this division x.size() / sizeof(uint_fast8_t) .编辑 1我被问到这个部门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.我将它除以 uint_fast8_t 的大小以获得向量中的元素数。 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.这可以更简单地表达,使用std::vector::resizestd::transform和适当的函数对象。

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)这是工作代码:(我认为插入函数可以在没有 for 循环的情况下插入许多零。我会检查是否可以替换它)

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;

} }

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

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