added: UInt::Sqrt() - a new algorithm for calculating the square root
git-svn-id: svn://ttmath.org/publicrep/ttmath/trunk@228 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
@@ -1509,6 +1509,7 @@ public:
|
||||
}
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
#ifdef TTMATH_CONSTANTSGENERATOR
|
||||
|
@@ -534,6 +534,7 @@ public:
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
*
|
||||
* convertion methods
|
||||
|
@@ -2252,6 +2252,50 @@ public:
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
square root
|
||||
e.g. Sqrt(9) = 3
|
||||
('digit-by-digit' algorithm)
|
||||
*/
|
||||
void Sqrt()
|
||||
{
|
||||
UInt<value_size> bit, temp;
|
||||
|
||||
if( IsZero() )
|
||||
return;
|
||||
|
||||
UInt<value_size> value(*this);
|
||||
|
||||
SetZero();
|
||||
bit.SetZero();
|
||||
bit.table[value_size-1] = (TTMATH_UINT_HIGHEST_BIT >> 1);
|
||||
|
||||
while( bit > value )
|
||||
bit.Rcr(2);
|
||||
|
||||
while( !bit.IsZero() )
|
||||
{
|
||||
temp = *this;
|
||||
temp.Add(bit);
|
||||
|
||||
if( value >= temp )
|
||||
{
|
||||
value.Sub(temp);
|
||||
Rcr(1);
|
||||
Add(bit);
|
||||
}
|
||||
else
|
||||
{
|
||||
Rcr(1);
|
||||
}
|
||||
|
||||
bit.Rcr(2);
|
||||
}
|
||||
|
||||
TTMATH_LOG("UInt::Sqrt")
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this method sets n first bits to value zero
|
||||
|
Reference in New Issue
Block a user