From 062881900a73916cc835db4ad14533140b336993 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Thu, 12 Apr 2007 17:17:22 +0000 Subject: [PATCH] changed: small changes in: Big::SetPi(), Big::Set05Pi(), Big::Set2Pi(), Big::ChangeSign() added: ASinh(), ACosh(), ATanh() /ATgh()/, ACoth() /ACtgh()/ and to the parser as well git-svn-id: svn://ttmath.org/publicrep/ttmath/trunk@35 e52654a7-88a9-db11-a3e9-0013d4bc506e --- ttmath/ttmath.h | 167 ++++++++++++++++++++++++++++++++++++++++++ ttmath/ttmathbig.h | 34 +++++++-- ttmath/ttmathparser.h | 60 +++++++++++++++ 3 files changed, 255 insertions(+), 6 deletions(-) diff --git a/ttmath/ttmath.h b/ttmath/ttmath.h index 9379d90..347fa67 100644 --- a/ttmath/ttmath.h +++ b/ttmath/ttmath.h @@ -1171,6 +1171,173 @@ namespace ttmath } + /* + * + * inverse hyperbolic functions + * + * + */ + + + /*! + inverse hyperbolic sine + + asinh(x) = ln( x + sqrt(x^2 + 1) ) + */ + template + ValueType ASinh(const ValueType & x, ErrorCode * err = 0) + { + ValueType xx(x), one, result; + uint c = 0; + one.SetOne(); + + c += xx.Mul(x); + c += xx.Add(one); + one.exponent.SubOne(); // one=0.5 + // xx is >= 1 + c += xx.PowFrac(one); // xx=sqrt(xx) + c += xx.Add(x); + c += result.Ln(xx); // xx > 0 + + // here can only be a carry + if( err ) + *err = c ? err_overflow : err_ok; + + return result; + } + + + /*! + inverse hyperbolic cosine + + acosh(x) = ln( x + sqrt(x^2 - 1) ) x in <1, infinity) + */ + template + ValueType ACosh(const ValueType & x, ErrorCode * err = 0) + { + ValueType xx(x), one, result; + uint c = 0; + one.SetOne(); + + if( x < one ) + { + if( err ) + *err = err_improper_argument; + + return result; + } + + c += xx.Mul(x); + c += xx.Sub(one); + // xx is >= 0 + // we can't call a PowFrac when the 'x' is zero + // if x is 0 the sqrt(0) is 0 + if( !xx.IsZero() ) + { + one.exponent.SubOne(); // one=0.5 + c += xx.PowFrac(one); // xx=sqrt(xx) + } + c += xx.Add(x); + c += result.Ln(xx); // xx >= 1 + + // here can only be a carry + if( err ) + *err = c ? err_overflow : err_ok; + + return result; + } + + + /*! + inverse hyperbolic tangent + + atanh(x) = 0.5 * ln( (1+x) / (1-x) ) x in (-1, 1) + */ + template + ValueType ATanh(const ValueType & x, ErrorCode * err = 0) + { + ValueType nominator(x), denominator, one, result; + uint c = 0; + one.SetOne(); + + if( !x.SmallerWithoutSignThan(one) ) + { + if( err ) + *err = err_improper_argument; + + return result; + } + + c += nominator.Add(one); + denominator = one; + c += denominator.Sub(x); + c += nominator.Div(denominator); + c += result.Ln(nominator); + c += result.exponent.SubOne(); + + // here can only be a carry + if( err ) + *err = c ? err_overflow : err_ok; + + return result; + } + + + /*! + inverse hyperbolic tantent + */ + template + ValueType ATgh(const ValueType & x, ErrorCode * err = 0) + { + return ATanh(x, err); + } + + + /*! + inverse hyperbolic cotangent + + acoth(x) = 0.5 * ln( (x+1) / (x-1) ) x in (-infinity, -1) or (1, infinity) + */ + template + ValueType ACoth(const ValueType & x, ErrorCode * err = 0) + { + ValueType nominator(x), denominator(x), one, result; + uint c = 0; + one.SetOne(); + + if( !x.GreaterWithoutSignThan(one) ) + { + if( err ) + *err = err_improper_argument; + + return result; + } + + c += nominator.Add(one); + c += denominator.Sub(one); + c += nominator.Div(denominator); + c += result.Ln(nominator); + c += result.exponent.SubOne(); + + // here can only be a carry + if( err ) + *err = c ? err_overflow : err_ok; + + return result; + } + + + /*! + inverse hyperbolic cotantent + */ + template + ValueType ACtgh(const ValueType & x, ErrorCode * err = 0) + { + return ACoth(x, err); + } + + + /* diff --git a/ttmath/ttmathbig.h b/ttmath/ttmathbig.h index 487a7b8..aa4fa43 100644 --- a/ttmath/ttmathbig.h +++ b/ttmath/ttmathbig.h @@ -182,10 +182,13 @@ public: } + +private: + /*! - it sets value pi + sets the mantissa of the value pi */ - void SetPi() + void SetMantissaPi() { // this is a static table which represents the value Pi (mantissa of it) // (first is the highest word) @@ -223,8 +226,19 @@ public: // and on 64bit platform value 64 (128/2=64)) mantissa.SetFromTable(temp_table, sizeof(temp_table) / sizeof(int)); - exponent = -sint(man)*sint(TTMATH_BITS_PER_UINT) + 2; + } + +public: + + + /*! + sets the value of pi + */ + void SetPi() + { + SetMantissaPi(); info = 0; + exponent = -sint(man)*sint(TTMATH_BITS_PER_UINT) + 2; } @@ -233,7 +247,8 @@ public: */ void Set05Pi() { - SetPi(); + SetMantissaPi(); + info = 0; exponent = -sint(man)*sint(TTMATH_BITS_PER_UINT) + 1; } @@ -243,7 +258,8 @@ public: */ void Set2Pi() { - SetPi(); + SetMantissaPi(); + info = 0; exponent = -sint(man)*sint(TTMATH_BITS_PER_UINT) + 3; } @@ -495,10 +511,16 @@ public: */ void ChangeSign() { + if( info & TTMATH_BIG_SIGN ) + { + info &= ~TTMATH_BIG_SIGN; + return; + } + if( IsZero() ) return; - info = (info & (~TTMATH_BIG_SIGN)) | ((~info) & TTMATH_BIG_SIGN); + info |= TTMATH_BIG_SIGN; } diff --git a/ttmath/ttmathparser.h b/ttmath/ttmathparser.h index a1772f6..4de664b 100644 --- a/ttmath/ttmathparser.h +++ b/ttmath/ttmathparser.h @@ -1043,6 +1043,60 @@ void Root(int sindex, int amount_of_args, ValueType & result) Error( err ); } + + +void ASinh(int sindex, int amount_of_args, ValueType & result) +{ + if( amount_of_args != 1 ) + Error( err_improper_amount_of_arguments ); + + ErrorCode err; + result = ttmath::ASinh(stack[sindex].value, &err); + + if( err != err_ok ) + Error( err ); +} + + +void ACosh(int sindex, int amount_of_args, ValueType & result) +{ + if( amount_of_args != 1 ) + Error( err_improper_amount_of_arguments ); + + ErrorCode err; + result = ttmath::ACosh(stack[sindex].value, &err); + + if( err != err_ok ) + Error( err ); +} + + +void ATanh(int sindex, int amount_of_args, ValueType & result) +{ + if( amount_of_args != 1 ) + Error( err_improper_amount_of_arguments ); + + ErrorCode err; + result = ttmath::ATanh(stack[sindex].value, &err); + + if( err != err_ok ) + Error( err ); +} + + +void ACoth(int sindex, int amount_of_args, ValueType & result) +{ + if( amount_of_args != 1 ) + Error( err_improper_amount_of_arguments ); + + ErrorCode err; + result = ttmath::ACoth(stack[sindex].value, &err); + + if( err != err_ok ) + Error( err ); +} + + /*! this method returns the value from a user-defined function @@ -1192,6 +1246,12 @@ void CreateFunctionsTable() InsertFunctionToTable(std::string("coth"), &Parser::Coth); InsertFunctionToTable(std::string("ctgh"), &Parser::Coth); InsertFunctionToTable(std::string("root"), &Parser::Root); + InsertFunctionToTable(std::string("asinh"), &Parser::ASinh); + InsertFunctionToTable(std::string("acosh"), &Parser::ACosh); + InsertFunctionToTable(std::string("atanh"), &Parser::ATanh); + InsertFunctionToTable(std::string("atgh"), &Parser::ATanh); + InsertFunctionToTable(std::string("acoth"), &Parser::ACoth); + InsertFunctionToTable(std::string("actgh"), &Parser::ACoth); }