diff --git a/ttmath/ttmath.h b/ttmath/ttmath.h index 57b07a0..94a0eb4 100644 --- a/ttmath/ttmath.h +++ b/ttmath/ttmath.h @@ -1843,20 +1843,7 @@ namespace ttmath return ValueType(); // NaN is set by default } - if( x.IsZero() ) - { - // Sqrt(0) = 0 - if( err ) - *err = err_ok; - - return x; - } - - ValueType pow; - pow.Set05(); - - // PowFrac can return only a carry because x is greater than zero - uint c = x.PowFrac(pow); + uint c = x.Sqrt(); if( err ) *err = c ? err_overflow : err_ok; diff --git a/ttmath/ttmathbig.h b/ttmath/ttmathbig.h index f7a4680..46b1b8e 100644 --- a/ttmath/ttmathbig.h +++ b/ttmath/ttmathbig.h @@ -1509,6 +1509,33 @@ public: } + /*! + this function calculates the square root + + Sqrt(9) = 3 + + return: 0 - ok + 1 - carry + 2 - improper argument (this<0 or NaN) + */ + uint Sqrt() + { + if( IsNan() || IsSign() ) + { + SetNan(); + return 2; + } + + if( IsZero() ) + return 0; + + Big pow; + pow.Set05(); + + // PowFrac can return only a carry because x is greater than zero + return PowFrac(pow); + } + private: