本文整理汇总了C++中BigInteger::copyFrom方法的典型用法代码示例。如果您正苦于以下问题:C++ BigInteger::copyFrom方法的具体用法?C++ BigInteger::copyFrom怎么用?C++ BigInteger::copyFrom使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BigInteger
的用法示例。
在下文中一共展示了BigInteger::copyFrom方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: quickDivMod
// Divide this by divisor, put the remainder (i.e. this % divisor) into residual. If optional third argument result
// is not null, use it to store the result of the div, else allocate a new BigInteger for the result.
// Note: this has been hard to get right. If bugs do show up, use divideByReciprocalMethod instead
// Note2: this is optimized for the types of numerator/denominators generated by D2A. It will not work when
// the result would be a value bigger than 9. For general purpose BigInteger division, use divideByReciprocalMethod.
BigInteger* BigInteger::quickDivMod(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;
}
int dWords = divisor->numWords;
/* this section only necessary for true division instead of special case division needed by D2A. We are
assuming the result is a single digit value < 10 and > -1
int next = this->numWords - dWords;
residual->copyFrom(this, next, dWords); // residual holds a divisor->numWords sized chunk of this.
*/
residual->copyFrom(this,0,numWords);
BigInteger decrement;
decrement.setFromInteger(0);
result->setNumWords(divisor->numWords, true);
uint64_t factor;
//do // need to loop over dword chunks of residual to make this handle division of any arbitrary bigIntegers
{
// guess largest factor that * divisor will fit into residual
const uint64_t n = (uint64_t)(residual->wordBuffer[residual->numWords-1]);
factor = n / divisor->wordBuffer[dWords-1];
if ( ((factor <= 0) || (factor > 10)) // over estimate of 9 could be 10
&& residual->numWords > 1 && dWords > 1)
{
uint64_t bigR = ( ((uint64_t)residual->wordBuffer[residual->numWords-1]) << 32)
+ (residual->wordBuffer[residual->numWords-2]);
factor = bigR / divisor->wordBuffer[dWords-1];
if (factor > 9)
{ // Note: This only works because of the relative size of the two operands
// which the D2A class produces. todo: try generating a numerator out of the
// the top 32 significant bits of residual (may have to get bits from two seperate words)
// and a denominator out of the top 24 significant bits of divisor and use them for
// the factor guess. Same principal as above applied to 8 bit units.
factor = 9;
/*
BigInteger::free(decrement);
return divideByReciprocalMethod(divisor, residual, result);
*/
}
}
if (factor)
{
decrement.copyFrom(divisor);
decrement.multAndIncrementBy( (uint32)factor,0);
// check for overestimate
// fix bug 121952: must check for larger overestimate, which
// can occur despite the checks above in some rare cases.
// To see this case, use:
// this=4398046146304000200000000
// divisor=439804651110400000000000
while (decrement.compare(residual) == 1 && factor > 0)
{
decrement.decrementBy(divisor);
factor--;
}
// reduce dividend (aka residual) by factor*divisor, leave remainder in residual
residual->decrementBy(&decrement);
}
// check for factor 0 underestimate
int comparedTo = residual->compare(divisor);
if (comparedTo == 1) // correct guess if its an off by one estimate
{
residual->decrementBy(divisor);
factor++;
}
result->wordBuffer[0] = (uint32)factor;
/* The above works for the division requirements of D2A, where divisor is always around 10 larger
than the dividend and the result is always a digit 1-9.
To make this routine work for general division, the following would need to be fleshed out /debugged.
residual->trimLeadingZeros();
// While we have more words to divide by and the residual has less words than the
// divisor,
if (--next >= 0)
{
do
{
result->lshiftBy(32); // shift current result over by a word
//.........这里部分代码省略.........