简体   繁体   English

如何正确实现`operator /`?

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

MAJOR EDIT: Can someone please explain to me how to fix operator/ so that it will work properly? 主要编辑:有人可以向我解释如何修复操作员/使其正常工作吗? i realize that the shift is not always correct, such as for 10 / 3 , which will cause infinite loops. 我意识到移位并非总是正确的,例如10 / 3 ,这将导致无限循环。 so how do i fix that? 那么我该如何解决呢?

the entire code is at http://ideone.com/GhF0e 整个代码在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;
}

is this correct? 这个对吗?

    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;
    }

it seems to be correct, except im somehow getting random large values when modding by 10, even though my mod function is just 这似乎是正确的,除了我以某种方式在修改10时获得随机大值,即使我的mod函数只是

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

What about this: 那这个呢:

int div;  // Uninitialized variable.

What happens if all the test on the stream fail. 如果流上的所有测试均失败,会发生什么情况。
Then div could have any value. 那么div可能有任何值。 If it is 0 (or 1) then rhs will never reach 0. 如果它是0(或1),那么rhs将永远不会达到0。

In the expression "copyn > (copyd << 1)", "copyd << 1" can overflow, leading to the infinite loop you're observing. 在表达式“ copyn>(copyed << 1)”中,“ copyed << 1”可能溢出,从而导致您正在观察的无限循环。 I would suggest checking for the overflow, or making the check something more like "(copyn >> n) > copyd". 我建议检查溢出,或者使检查更像“(copyn >> n)>复制”。

If there were an infinite loop, you wouldn't even be able to output -1. 如果存在无限循环,您甚至将无法输出-1。 -1 isn't as small as -123455, but you should try it. -1不小于-123455,但您应该尝试一下。 There is nothing wrong with operator << but there is something wrong with your assumption about operator /. 运算符<<没什么错,但是您对运算符/的假设有问题。 There's something else wrong too, but I'm not sure if I should do your homework ^_^ 也有其他问题,但是我不确定是否应该做作业^ _ ^

I'm not sure if this is the problem, but it looks like: 我不确定这是否是问题,但它看起来像:

stream << out;
return stream;

Is outside of the function, at class scope. 在函数范围之外,在类范围内。

You probably want to get rid of one of the } s after else . 你可能想摆脱的一个}年代以后else

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

相关问题 如何使用“if constexpr”正确实现“operator()”,以便它与 std::generate 一起使用? - How do I properly implement "operator()" with "if constexpr" so that it works with std::generate? 如何为此结构实现[]运算符? - How do I implement the [] operator for this structure? 如何为动态数组实现operator []? - How do I implement operator[] for dynamic array? 如何正确重载流运算符? - How do I overload a stream operator properly? 如何正确重载&#39;=&#39;运算符以处理重载&#39;[]&#39;运算符的数组? - How do I properly overload the '=' operator to work with an array that is overloading the '[ ]' operator? 如何实现一个operator-&gt;用于指向外部存储器的花哨指针? - How do I implement a operator-> for a fancy pointer to external memory? 创建复制构造函数时如何正确重载 operator= - How do I properly overload a operator= when creating a copy constructor 如何在C ++中正确组织和实现SFML音频? - How do I properly organize and implement SFML audio in C++? 如何在Qt中正确实现“最小化托盘”功能? - How do I properly implement a “minimize to tray” function in Qt? 如何正确实现两个子类之间的赋值运算符 - How to properly implement assignment operator between two child-classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM