繁体   English   中英

如何正确实现`operator /`?

[英]How do I properly implement `operator/`?

主要编辑:有人可以向我解释如何修复操作员/使其正常工作吗? 我意识到移位并非总是正确的,例如10 / 3 ,这将导致无限循环。 那么我该如何解决呢?

整个代码在http://ideone.com/GhF0e

uint128_t operator/(uint128_t rhs){
    // Save some calculations ///////////////////////
    if (rhs == 0){
        std::cout << "Error: division or modulus by zero" << std::endl;
        exit(1);
    }
    if (rhs == 1)
        return *this;
    if (*this == rhs)
        return uint128_t(1);
    if ((*this == 0) | (*this < rhs))
        return uint128_t(0, 0);
    // //////////////////////////////////////////////
    uint128_t copyn(*this), quotient = 0;
    while (copyn >= rhs){
        uint128_t copyd(rhs), temp(1);
        // shift the divosr to match the highest bit
        while (copyn > (copyd << 1)){
            copyd <<= 1;
            temp <<= 1;
        }
        copyn -= copyd;
        quotient += temp;
    }
    return quotient;
}

这个对吗?

    uint128_t operator/(uint128_t rhs){
        // Save some calculations ///////////////////////
        if (rhs == 0){
            std::cout << "Error: division or modulus by zero" << std::endl;
            exit(1);
        }
        if (rhs == 1)
            return *this;
        if (*this == rhs)
            return uint128_t(1);
        if ((*this == 0) | (*this < rhs))
            return uint128_t(0);
        uint128_t copyd(rhs);
        // Checks for divisors that are powers of two
        uint8_t s = 0;
        while ((copyd.LOWER & 1) == 0){
            copyd >>= 1;
            s++;
        }
        if (copyd == 1)
            return *this >> s;
        // //////////////////////////////////////////////

        uint128_t copyn(*this), quotient = 0;
        copyd = rhs;
        uint8_t n_b = 255, d_b = 0;
        while (copyd){
            copyd >>= 1;
            d_b++;// bit size of denomiator
        }
        copyd = rhs;
        while (n_b > d_b){
            // get the highest bit of dividend at current step
            n_b = 0;
            uint128_t copycopyn(copyn);
            while (copycopyn){
                copycopyn >>= 1;
                n_b++;
            }
            uint8_t highest_bit = n_b - d_b - 1;
            copyn -= copyd << highest_bit;
            quotient += uint128_t(1) << highest_bit;
        }
        if (n_b == d_b)
            quotient++;
        return quotient;
    }

这似乎是正确的,除了我以某种方式在修改10时获得随机大值,即使我的mod函数只是

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

那这个呢:

int div;  // Uninitialized variable.

如果流上的所有测试均失败,会发生什么情况。
那么div可能有任何值。 如果它是0(或1),那么rhs将永远不会达到0。

在表达式“ copyn>(copyed << 1)”中,“ copyed << 1”可能溢出,从而导致您正在观察的无限循环。 我建议检查溢出,或者使检查更像“(copyn >> n)>复制”。

如果存在无限循环,您甚至将无法输出-1。 -1不小于-123455,但您应该尝试一下。 运算符<<没什么错,但是您对运算符/的假设有问题。 也有其他问题,但是我不确定是否应该做作业^ _ ^

我不确定这是否是问题,但它看起来像:

stream << out;
return stream;

在函数范围之外,在类范围内。

你可能想摆脱的一个}年代以后else

暂无
暂无

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

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