显示导致垃圾的大整数

时间:2011-08-14 23:55:07

标签: c++ math operator-overloading

我写了一个名为integer的类,可以处理任意位大小的整数,我似乎已经完成了除了一件事之外的所有事情,尽管我尝试修复它所做的一切,但拒绝正常工作:

std::ostream & operator<<(std::ostream & stream, integer rhs){
    std::string out = "";
    if (rhs == 0)
        out = "0";
    else {
        int div = 10;
        if (stream.flags() & stream.oct)
            div = 8;
        if (stream.flags() & stream.hex)
            div = 16;
        while (rhs){
            // not doing mod to avoid dividing twice
            integer next = rhs / div;
            out = "0123456789abcdef"[(int) (rhs - next * div)] + out;
            rhs = next;
        }
    }
    stream << out;
    return stream;
}

上面的代码给了我最奇怪的答案(非十六进制字符与正确的字符混合),假设它没有崩溃。我不明白为什么。我的加法,减法乘法和除法运算符是正确的。类型转换是正确的。以十六进制显示的内部值(std :: deque)是正确的:43981将在while循环之前ab cd时显示cout。然而,在while循环中,尽管div = 10中的等式与mod函数等价,但我会在[]时得到像509这样的疯狂值。但是在我的main.cpp中的代码之外我会得到正确的值。

还有什么要检查的?

编辑:我不明白:我将while循环的内部改为:

        integer next = rhs / div;
        integer a =  next * div;
        integer b = rhs - a;
        out = "0123456789abcdef"[(int) (b)] + out;
        rhs = next;

它工作正常。然而,当我将next * div移到b以替换那里的a时,程序崩溃了

edit2:这是运营商,按要求:

    integer operator+(integer rhs){
        if (rhs == 0)
            return *this;
        if (*this == 0)
            return rhs;
        std::deque <uint_fast8_t> top = value, bottom = rhs.value;
        if (value.size() < rhs.value.size())
            top.swap(bottom);
        top.push_front(0);
        while (bottom.size() + 1 < top.size())
            bottom.push_front(0);
        bool carry = false, next_carry;
        for(std::deque <uint_fast8_t>::reverse_iterator i = top.rbegin(), j = bottom.rbegin(); j != bottom.rend(); i++, j++){
            next_carry = ((*i + *j + carry) > 255);
            *i += *j + carry;
            carry = next_carry;
        }
        if (carry)
            *top.begin() = 1;
        return integer(top);
    }

    integer operator-(integer rhs){
        if (rhs == 0)
            return *this;
        if (*this == rhs)
            return integer(0);
        if (rhs > *this)
            exit(2);// to be worked on
        unsigned int old_b = rhs.bits();
        rhs = rhs.twos_complement();
        for(unsigned int i = old_b; i < bits(); i++)
            rhs ^= integer(1) << i;
        return (*this + rhs) & (~(integer(1) << bits()));   // Flip bits to get max of 1 << x
    }

    // Peasant Multiplication
    integer peasant(integer lhs, integer rhs){
        integer SUM = 0;
        while (lhs){
            if (lhs & 1)
                SUM += rhs;
            lhs >>= 1;
            rhs <<= 1;
        }
        return SUM;
    }

    integer karatsuba(integer lhs, integer rhs){
        // b is base = 256
        // m is chars
        // bm is max value
        integer m = std::max(lhs.value.size(), rhs.value.size()) >> 1;
        integer bm = 1;
        bm <<= (m << 3);

        if ((lhs < bm) || (rhs < bm))
            return peasant(lhs, rhs);

        integer x0 = lhs % bm;
        integer x1 = lhs / bm;
        integer y0 = rhs % bm;
        integer y1 = rhs / bm;

        integer z0 = karatsuba(x0, y0);
        integer z2 = karatsuba(x1, y1);
        integer z1 = karatsuba(x1 + x0, y1 + y0) - z2 - z0;
        return karatsuba(karatsuba(z2, bm) + z1, bm) + z0;
    }

    const integer operator*(integer rhs){
        integer lhs = *this;
        return karatsuba(lhs, rhs);
    }

    integer operator/(integer rhs){
        if (rhs == 0){
            std::cout << "Error: division or modulus by zero" << std::endl;
            exit(1);
        }
        if (rhs == 1)
            return *this;
        if (*this == rhs)
            return integer(1);
        if ((*this == 0) | (*this < rhs))
            return integer(0);
        // Check for divisors that are powers of two
        uint16_t s = 0;
        integer copyd(rhs);
        while ((copyd & 1) == 0){
            copyd >>= 1;
            s++;
        }
        if (copyd == 1)
            return *this >> s;
        ////////////////////////////////////////////////
        integer copyn(*this), quotient = 0;
        while (copyn >= rhs){
            copyd = rhs;
            integer temp(1);
            while ((copyn >> 1) > copyd){
                copyd <<= 1;
                temp <<= 1;
            }
            copyn -= copyd;
            quotient += temp;
        }
        return quotient;
    }

    integer operator%(integer rhs){
        *this = *this - (*this / rhs) * rhs;
        return *this;
    }

完整代码为here

非常感谢!!!

1 个答案:

答案 0 :(得分:1)

在您的某个运营商实施中,这很可能是一个错误。检查next的值。并为next * divrhs - next * div引入临时值,以便验证中间结果。同时检查演员表的结果为int

其他想法:

  • 不要对2的幂使用相同的代码,可以使用简单的位移和掩码处理。
  • 同样值得避免字符串连接,根据位数计算所需字符数的上限,然后在最后修剪不需要的字符串。