当前位置: 首页>>代码示例>>C++>>正文


C++ BigInteger::lshift方法代码示例

本文整理汇总了C++中BigInteger::lshift方法的典型用法代码示例。如果您正苦于以下问题:C++ BigInteger::lshift方法的具体用法?C++ BigInteger::lshift怎么用?C++ BigInteger::lshift使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BigInteger的用法示例。


在下文中一共展示了BigInteger::lshift方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: not

/*  Was hoping dividing via a Newton's approximation of the reciprocal would be faster, but
     its not (makes D2A about twice as slow!).  Its not the 32bit divide above that's slow,
     its how many times you have to iterate over entire BigIntegers.  Below uses a shift,
     and three multiplies:
*/
BigInteger* BigInteger::divideByReciprocalMethod(const BigInteger* divisor, BigInteger* residual, BigInteger* result)
{
    // handle easy cases where divisor is >= this
    int compareTo = this->compare(divisor);
    if (compareTo == -1)
    {
        residual->copyFrom(this);
        result->setValue(0);
        return result;
    }
    else if (compareTo == 0)
    {
        residual->setValue(0);
        result->setValue(1);
        return result;
    }

    uint32 d2Prec = divisor->lg2();
    uint32 e = 1 + d2Prec;
    uint32 ag = 1;
    uint32 ar = 31 + this->lg2() - d2Prec;
    BigInteger u;
    u.setFromInteger(1);

    BigInteger ush;
    ush.setFromInteger(1);

    BigInteger usq;
    usq.setFromInteger(0);

    while (1)
    {
        u.lshift(e + 1,&ush);
        divisor->mult(&u,&usq); // usq = divisor*u^2
        usq.multBy(&u);
        ush.subtract(&usq, &u); // u = ush - usq;

        int32 ush2 = u.lg2(); // ilg2(u);
        e *= 2;
        ag *= 2;
        int32 usq2 = 4 + ag;
        ush2 -= usq2; // BigInteger* diff = ush->subtract(usq); // ush -= usq;  ush > 0
        if (ush2 > 0) // (ush->compare(usq) == 1)
        {
            u.rshiftBy(ush2); // u >>= ush;
            e -= ush2;
        }
        if (ag > ar)
            break;
    }
    result = this->mult(&u,result);         // mult by reciprocal (scaled by e)
    result->rshiftBy(e);                    // remove scaling

    BigInteger temp;
    temp.setFromInteger(0);
    divisor->mult(result, &temp);  // compute residual as this - divisor*result
    this->subtract(&temp,residual);  //  todo: should be a more optimal way of doing this

    // ... doesn't work, e is the wrong scale for this (too large)....
    // residual = this->lshift(e,residual); // residual = (this*2^e - result) / 2^e
    // residual->decrementBy(result);
    // residual->rshiftBy(e);                   // remove scaling
    return(result);
}
开发者ID:changm,项目名称:tessa,代码行数:69,代码来源:BigInteger.cpp


注:本文中的BigInteger::lshift方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。