简体   繁体   中英

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. so how do i fix that?

the entire code is at 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

    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. If it is 0 (or 1) then rhs will never reach 0.

In the expression "copyn > (copyd << 1)", "copyd << 1" can overflow, leading to the infinite loop you're observing. I would suggest checking for the overflow, or making the check something more like "(copyn >> n) > copyd".

If there were an infinite loop, you wouldn't even be able to output -1. -1 isn't as small as -123455, but you should try it. 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 .

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