diff --git a/CHANGELOG b/CHANGELOG index 0aacdd1..37b70e5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ Version 0.7.1 (2007.02.27): - * fixed the error 'overflow during printing' which was caused by Big::FromInt(Int value) - (the sign has to be set at the end) + * fixed the error 'overflow during printing' which was caused + by Big::FromInt(Int value) (the sign has to be set at the end) * fixed many small errors * added ATan (arctan), ACTan (arc ctan) functions diff --git a/TODO b/TODO index f7e18ca..ebf3cc1 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,7 @@ TODO TTMath Library =================== -* to add the method Mod (a remainder from a division) for the Big type * to add operators (or functions) and, or, xor * to add functions for generating random values * to add constructors (UInt, Int) for 64bit platforms (constractors which take int and unsigned) +* to add 'history' for functions like 'factorial' which take a lot of time during calculating diff --git a/ttmath/ttmath.h b/ttmath/ttmath.h index 38db0dd..66c392c 100644 --- a/ttmath/ttmath.h +++ b/ttmath/ttmath.h @@ -133,7 +133,40 @@ namespace ttmath return result; } - + + + /*! + it returns the sign of the value + e.g. -2 = 1 + 0 = 0 + 10 = 1 + */ + template + ValueType Sgn(ValueType x) + { + x.Sgn(); + + return x; + } + + + /*! + the remainder from the division + + e.g. + mod( 12.6 ; 3) = 0.6 because 12.6 = 3*4 + 0.6 + mod(-12.6 ; 3) = -0.6 + mod( 12.6 ; -3) = 0.6 + mod(-12.6 ; -3) = -0.6 + */ + template + ValueType Mod(ValueType a, const ValueType & b) + { + a.Mod(b); + + return a; + } + /*! this method skips the fraction from x e.g 2.2 = 2 diff --git a/ttmath/ttmathbig.h b/ttmath/ttmathbig.h index 6eadebd..79ba286 100644 --- a/ttmath/ttmathbig.h +++ b/ttmath/ttmathbig.h @@ -348,6 +348,28 @@ public: } + /*! + it remains the 'sign' of the value + e.g. -2 = 1 + 0 = 0 + 10 = 1 + */ + void Sgn() + { + if( IsSign() ) + { + SetOne(); + SetSign(); + } + else + if( IsZero() ) + SetZero(); + else + SetOne(); + } + + + /*! it sets the sign @@ -569,6 +591,36 @@ public: } + /*! + the remainder from the division + + e.g. + 12.6 mod 3 = 0.6 because 12.6 = 3*4 + 0.6 + -12.6 mod 3 = -0.6 + 12.6 mod -3 = 0.6 + -12.6 mod -3 = -0.6 + + it means: + in other words: this(old) = ss2 * q + this(new)(result) + */ + uint Mod(const Big & ss2) + { + TTMATH_REFERENCE_ASSERT( ss2 ) + + uint c = 0; + + Big temp(*this); + + c += temp.Div(ss2); + temp.SkipFraction(); + c += temp.Mul(ss2); + c += Sub(temp); + + return (c==0)? 0 : 1; + } + + + /*! power this = this ^ pow pow without a sign diff --git a/ttmath/ttmathparser.h b/ttmath/ttmathparser.h index 7cf02b8..362b66d 100644 --- a/ttmath/ttmathparser.h +++ b/ttmath/ttmathparser.h @@ -748,6 +748,32 @@ void ACTan(int sindex, int amount_of_args, ValueType & result) } +void Sgn(int sindex, int amount_of_args, ValueType & result) +{ + if( amount_of_args != 1 ) + Error( err_improper_amount_of_arguments ); + + result = ttmath::Sgn(stack[sindex].value); +} + + +void Mod(int sindex, int amount_of_args, ValueType & result) +{ + if( amount_of_args != 2 ) + Error( err_improper_amount_of_arguments ); + + if( stack[sindex+2].value.IsZero() ) + Error( err_improper_argument ); + + result = stack[sindex].value; + uint c = result.Mod(stack[sindex+2].value); + + if( c ) + Error( err_overflow ); +} + + + /*! this method returns the value from a user-defined function @@ -874,6 +900,8 @@ void CreateFunctionsTable() InsertFunctionToTable(std::string("acos"), &Parser::ACos); InsertFunctionToTable(std::string("atan"), &Parser::ATan); InsertFunctionToTable(std::string("actan"), &Parser::ACTan); + InsertFunctionToTable(std::string("sgn"), &Parser::Sgn); + InsertFunctionToTable(std::string("mod"), &Parser::Mod); } @@ -1728,8 +1756,6 @@ ErrorCode Parse(const char * str) stack.resize( default_stack_size ); -// char buf_temp[] = "-(1)"; -// pstring = buf_temp; try {