2011-06-17 72 views
0

主编辑:有人请向我解释如何修复operator /以便它能正常工作吗?我意识到这种转变并不总是正确的,例如10/3,这将导致无限循环。那我该如何解决这个问题?如何正确实现`operator /`?

整个代码为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; 
    } 

它似乎是正确的,但IM 10改装时,不知何故得到随机值很大,即使我的MOD功能只是

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

当你通过代码时,发生什么情况? – 2011-06-17 03:04:04

+0

你得到的输出是什么。你尝试过flush()输出吗? – iammilind 2011-06-17 03:04:09

+0

它冻结了。我无法找到问题的确切来源,尽管它似乎在'operator /'中。但正如我所说,尽可能慢,它不能///慢 – calccrypto 2011-06-17 03:05:32

回答

2

在“copyn>(copyd < < 1)”中,“copyd < < 1”可能溢出,导致您观察到无限循环。我会建议检查溢出,或者使检查更像“(copyn >> n)> copyd”。

2

这个怎么样:

int div; // Uninitialized variable. 

如果流上的所有测试都失败会发生什么。
然后div可以有任何价值。如果它是0(或1),那么rhs将永远不会达到0.

+0

这绝对是不好的代码,但在这种情况下,我认为div确实设置为10. – DSM 2011-06-17 03:22:17

0

如果存在无限循环,则甚至不能输出-1。 -1并不像-123455那么小,但你应该尝试一下。运营商< <没有问题,但是您对运营商/的假设存在问题。还有别的东西错了,但是我不知道我是否应该做的功课^ _^

+0

这个答案的建议在执行/运算符时出现错误。 – 2011-06-17 03:45:02

+0

作业?你在开玩笑,对吧? – calccrypto 2011-06-17 05:04:18

0

我不知道这是一个问题,但它看起来像:

stream << out; 
return stream; 

是外的功能,在课堂范围内。

您可能想要在else之后摆脱}之一。