From e73ce2f8bca28fa877e9cbac20dad6520a7446ff Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sun, 1 Nov 2009 13:26:19 +0000 Subject: [PATCH] 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 --- CHANGELOG | 1 + ttmath/ttmathbig.h | 1 + ttmath/ttmathint.h | 1 + ttmath/ttmathuint.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 5931319..9d11e93 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -44,6 +44,7 @@ Version 0.9.0 prerelease (2009.11.01): uint FromString(const wchar_t * source, const Conv & conv, const wchar_t **, bool *) uint FromString(const std::string & string, const Conv & conv, const wchar_t **, bool *) uint FromString(const std::wstring & string, const Conv & conv, const wchar_t **, bool *) + * added: UInt::Sqrt() - a new algorithm for calculating the square root * changed: Factorial() is using the Gamma() function now * changed: Big::Div(ss2) Big::Mod(ss2) diff --git a/ttmath/ttmathbig.h b/ttmath/ttmathbig.h index 1cddf0e..f7a4680 100644 --- a/ttmath/ttmathbig.h +++ b/ttmath/ttmathbig.h @@ -1509,6 +1509,7 @@ public: } + private: #ifdef TTMATH_CONSTANTSGENERATOR diff --git a/ttmath/ttmathint.h b/ttmath/ttmathint.h index 260ad39..cc89064 100644 --- a/ttmath/ttmathint.h +++ b/ttmath/ttmathint.h @@ -534,6 +534,7 @@ public: } + /*! * * convertion methods diff --git a/ttmath/ttmathuint.h b/ttmath/ttmathuint.h index 0bc9a6a..7c6e5a8 100644 --- a/ttmath/ttmathuint.h +++ b/ttmath/ttmathuint.h @@ -2252,6 +2252,50 @@ public: } + /*! + square root + e.g. Sqrt(9) = 3 + ('digit-by-digit' algorithm) + */ + void Sqrt() + { + UInt bit, temp; + + if( IsZero() ) + return; + + UInt 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