- fixed a bug in ttmath.g (missing closing brace in Cos())

git-svn-id: svn://ttmath.org/publicrep/ttmath/branches/chk@154 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Christian Kaiser 2009-05-28 11:52:31 +00:00
commit b31d34ebdd
6 changed files with 276 additions and 94 deletions

View File

@ -1,6 +1,6 @@
o = main.o o = main.o
CC = g++ CC = g++
CFLAGS = -s -O2 -DCONSTANTSGENERATOR CFLAGS = -s -O2 -DTTMATH_CONSTANTSGENERATOR
name = gen name = gen

View File

@ -91,7 +91,7 @@ void CalcE()
ttmath::Big<1,400> e; ttmath::Big<1,400> e;
ttmath::uint steps; ttmath::uint steps;
// macro CONSTANTSGENERATOR has to be defined // macro TTMATH_CONSTANTSGENERATOR has to be defined
e.ExpSurrounding0(1, &steps); e.ExpSurrounding0(1, &steps);
std::cout << "---------------- e ----------------" << std::endl; std::cout << "---------------- e ----------------" << std::endl;
e.mantissa.PrintTable(std::cout); e.mantissa.PrintTable(std::cout);
@ -105,7 +105,7 @@ void CalcLn(int x)
ttmath::Big<1,400> ln; ttmath::Big<1,400> ln;
ttmath::uint steps; ttmath::uint steps;
// macro CONSTANTSGENERATOR has to be defined // macro TTMATH_CONSTANTSGENERATOR has to be defined
ln.LnSurrounding1(x, &steps); ln.LnSurrounding1(x, &steps);
std::cout << "---------------- ln(" << x << ") ----------------" << std::endl; std::cout << "---------------- ln(" << x << ") ----------------" << std::endl;
ln.mantissa.PrintTable(std::cout); ln.mantissa.PrintTable(std::cout);

View File

@ -1,3 +1,4 @@
/* /*
* This file is a part of TTMath Bignum Library * This file is a part of TTMath Bignum Library
* and is distributed under the (new) BSD licence. * and is distributed under the (new) BSD licence.
@ -467,6 +468,21 @@ namespace ttmath
PrepareSin( x, change_sign ); PrepareSin( x, change_sign );
ValueType result = Sin0pi05( x ); ValueType result = Sin0pi05( x );
if( PrepareSin( x, change_sign ) )
{
// x is too big, we cannnot reduce the 2*PI period
// prior to version 0.8.5 the result was zero
// result has NaN flag set by default
if( err )
*err = err_overflow; // maybe another error code?
return result; // NaN is set by default
}
result = Sin0pi05( x );
one.SetOne(); one.SetOne();
// after calculations there can be small distortions in the result // after calculations there can be small distortions in the result
@ -497,7 +513,13 @@ namespace ttmath
x.Add( pi05 ); x.Add( pi05 );
return Sin(x); if( c )
{
if( err )
*err = err_overflow;
}
return ValueType(); // result is undefined (NaN is set by default)
} }
@ -521,6 +543,8 @@ namespace ttmath
if( err ) if( err )
*err = err_improper_argument; *err = err_improper_argument;
result.SetNan();
return result; return result;
} }
@ -561,6 +585,8 @@ namespace ttmath
if( err ) if( err )
*err = err_improper_argument; *err = err_improper_argument;
result.SetNan();
return result; return result;
} }
@ -748,7 +774,7 @@ namespace ttmath
{ {
using namespace auxiliaryfunctions; using namespace auxiliaryfunctions;
ValueType one; ValueType result, one;
one.SetOne(); one.SetOne();
bool change_sign = false; bool change_sign = false;
@ -757,7 +783,7 @@ namespace ttmath
if( err ) if( err )
*err = err_improper_argument; *err = err_improper_argument;
return one; return result; // NaN is set by default
} }
if( x.IsSign() ) if( x.IsSign() )
@ -768,8 +794,6 @@ namespace ttmath
one.exponent.SubOne(); // =0.5 one.exponent.SubOne(); // =0.5
ValueType result;
// asin(-x) = -asin(x) // asin(-x) = -asin(x)
if( x.GreaterWithoutSignThan(one) ) if( x.GreaterWithoutSignThan(one) )
result = ASin_1(x); result = ASin_1(x);
@ -1149,7 +1173,7 @@ namespace ttmath
if( err ) if( err )
*err = err_improper_argument; *err = err_improper_argument;
return x; return ValueType(); // NaN is set by default
} }
ValueType ex, emx, nominator, denominator; ValueType ex, emx, nominator, denominator;
@ -1238,7 +1262,7 @@ namespace ttmath
if( err ) if( err )
*err = err_improper_argument; *err = err_improper_argument;
return result; return result; // NaN is set by default
} }
c += xx.Mul(x); c += xx.Mul(x);
@ -1279,7 +1303,7 @@ namespace ttmath
if( err ) if( err )
*err = err_improper_argument; *err = err_improper_argument;
return result; return result; // NaN is set by default
} }
c += nominator.Add(one); c += nominator.Add(one);
@ -1324,7 +1348,7 @@ namespace ttmath
if( err ) if( err )
*err = err_improper_argument; *err = err_improper_argument;
return result; return result; // NaN is set by default
} }
c += nominator.Add(one); c += nominator.Add(one);
@ -1443,7 +1467,7 @@ namespace ttmath
if( err ) if( err )
*err = err_improper_argument; *err = err_improper_argument;
return delimiter; return delimiter; // NaN is set by default
} }
multipler = 60; multipler = 60;
@ -1624,7 +1648,7 @@ namespace ttmath
if( err ) if( err )
*err = err_improper_argument; *err = err_improper_argument;
return x; return ValueType(); // NaN is set by default
} }
if( x.IsZero() ) if( x.IsZero() )
@ -1662,6 +1686,8 @@ namespace ttmath
if( err ) if( err )
*err = err_improper_argument; *err = err_improper_argument;
x.SetNan();
return true; return true;
} }
@ -1680,6 +1706,8 @@ namespace ttmath
if( err ) if( err )
*err = err_improper_argument; *err = err_improper_argument;
x.SetNan();
return true; return true;
} }
@ -1729,6 +1757,8 @@ namespace ttmath
if( err ) if( err )
*err = err_improper_argument; *err = err_improper_argument;
x.SetNan();
return true; return true;
} }
@ -1777,6 +1807,8 @@ namespace ttmath
if( err ) if( err )
*err = err_improper_argument; *err = err_improper_argument;
x.SetNan();
return true; return true;
} }
} }
@ -1861,6 +1893,7 @@ namespace ttmath
while( !carry && multipler<maxvalue ) while( !carry && multipler<maxvalue )
{ {
// !! the test here we don't have to make in all iterations (performance)
if( stop && stop->WasStopSignal() ) if( stop && stop->WasStopSignal() )
{ {
if( err ) if( err )
@ -1893,6 +1926,7 @@ namespace ttmath
while( !carry && multipler < x ) while( !carry && multipler < x )
{ {
// !! the test here we don't have to make in all iterations (performance)
if( stop && stop->WasStopSignal() ) if( stop && stop->WasStopSignal() )
{ {
if( err ) if( err )
@ -1936,6 +1970,8 @@ namespace ttmath
if( err ) if( err )
*err = err_improper_argument; *err = err_improper_argument;
result.SetNan();
return result; return result;
} }
@ -1947,6 +1983,8 @@ namespace ttmath
if( err ) if( err )
*err = err_overflow; *err = err_overflow;
result.SetNan();
return result; return result;
} }
@ -1967,8 +2005,11 @@ namespace ttmath
status = FactorialMore(x, err, stop, result); status = FactorialMore(x, err, stop, result);
if( status == 2 ) if( status == 2 )
{
// the calculation has been interrupted // the calculation has been interrupted
result.SetNan();
return result; return result;
}
err_tmp = status==1 ? err_overflow : err_ok; err_tmp = status==1 ? err_overflow : err_ok;
history.Add(x, result, err_tmp); history.Add(x, result, err_tmp);

View File

@ -91,14 +91,35 @@ tchar_t info;
/*! /*!
the number of a bit from 'info' which means that a value is with a sign the number of a bit from 'info' which means that a value is with a sign
(when the bit is set) (when the bit is set)
/at the moment the rest bits from 'info' are not used/
*/ */
#define TTMATH_BIG_SIGN 128 #define TTMATH_BIG_SIGN 128
/*!
Not a number
if this bit is set that there is no a valid number
*/
#define TTMATH_BIG_NAN 64
/*!
this method sets NaN if there was a carry (and returns 1 in such a case)
c can be 0, 1 or other value different from zero
*/
uint CheckCarry(uint c)
{
if( c != 0 )
{
SetNan();
return 1;
}
return 0;
}
public: public:
@ -122,8 +143,9 @@ public:
return 0; return 0;
uint comp = mantissa.CompensationToLeft(); uint comp = mantissa.CompensationToLeft();
uint c = exponent.Sub( comp );
return exponent.Sub( comp ); return CheckCarry(c);
} }
@ -188,6 +210,15 @@ public:
} }
/*!
this method sets NaN flag (Not a Number)
when this flag is set that means there is no a valid number
*/
void SetNan()
{
info |= TTMATH_BIG_NAN;
}
private: private:
@ -508,6 +539,7 @@ public:
{ {
/* /*
we only have to test the mantissa we only have to test the mantissa
also we don't check the NaN flag
*/ */
return mantissa.IsZero(); return mantissa.IsZero();
} }
@ -515,6 +547,7 @@ public:
/*! /*!
this method returns true when there's the sign set this method returns true when there's the sign set
also we don't check the NaN flag
*/ */
bool IsSign() const bool IsSign() const
{ {
@ -522,6 +555,16 @@ public:
} }
/*!
this method returns true when there is not a valid number
*/
bool IsNan() const
{
return (info & TTMATH_BIG_NAN) == TTMATH_BIG_NAN;
}
/*! /*!
this method clears the sign this method clears the sign
(there'll be an absolute value) (there'll be an absolute value)
@ -544,6 +587,9 @@ public:
*/ */
void Sgn() void Sgn()
{ {
if( IsNan() )
return;
if( IsSign() ) if( IsSign() )
{ {
SetOne(); SetOne();
@ -618,6 +664,9 @@ public:
uint c = 0; uint c = 0;
if( IsNan() || ss2.IsNan() )
return CheckCarry(1);
exp_offset.Sub( ss2.exponent ); exp_offset.Sub( ss2.exponent );
exp_offset.Abs(); exp_offset.Abs();
@ -659,12 +708,12 @@ public:
// there shouldn't be a carry here because // there shouldn't be a carry here because
// (1) (2) guarantee that the mantissa of this // (1) (2) guarantee that the mantissa of this
// is greater than or equal to the mantissa of the ss2 // is greater than or equal to the mantissa of the ss2
TTMATH_VERIFY( mantissa.Sub(ss2.mantissa) == 0 ) TTMATH_VERIFY( mantissa.Sub(ss2.mantissa) >= 0 )
} }
c += Standardizing(); c += Standardizing();
return (c==0)? 0 : 1; return CheckCarry(c);
} }
@ -692,6 +741,9 @@ public:
*/ */
uint BitAnd(Big<exp, man> ss2) uint BitAnd(Big<exp, man> ss2)
{ {
if( IsNan() || ss2.IsNan() )
return CheckCarry(1);
if( IsSign() || ss2.IsSign() ) if( IsSign() || ss2.IsSign() )
return 2; return 2;
@ -725,7 +777,7 @@ public:
c += Standardizing(); c += Standardizing();
return (c==0)? 0 : 1; return CheckCarry(c);
} }
@ -740,6 +792,9 @@ public:
*/ */
uint BitOr(Big<exp, man> ss2) uint BitOr(Big<exp, man> ss2)
{ {
if( IsNan() || ss2.IsNan() )
return CheckCarry(1);
if( IsSign() || ss2.IsSign() ) if( IsSign() || ss2.IsSign() )
return 2; return 2;
@ -770,7 +825,7 @@ public:
c += Standardizing(); c += Standardizing();
return (c==0)? 0 : 1; return CheckCarry(c);
} }
@ -785,6 +840,9 @@ public:
*/ */
uint BitXor(Big<exp, man> ss2) uint BitXor(Big<exp, man> ss2)
{ {
if( IsNan() || ss2.IsNan() )
return CheckCarry(1);
if( IsSign() || ss2.IsSign() ) if( IsSign() || ss2.IsSign() )
return 2; return 2;
@ -815,7 +873,7 @@ public:
c += Standardizing(); c += Standardizing();
return (c==0)? 0 : 1; return CheckCarry(c);
} }
@ -830,6 +888,9 @@ public:
UInt<man+1> man_result; UInt<man+1> man_result;
uint i,c = 0; uint i,c = 0;
if( IsNan() )
return 1;
// man_result = mantissa * ss2.mantissa // man_result = mantissa * ss2.mantissa
mantissa.MulInt(ss2, man_result); mantissa.MulInt(ss2, man_result);
@ -858,7 +919,7 @@ public:
c += Standardizing(); c += Standardizing();
return (c==0)? 0 : 1; return CheckCarry(c);
} }
@ -869,6 +930,9 @@ public:
*/ */
uint MulInt(sint ss2) uint MulInt(sint ss2)
{ {
if( IsNan() )
return 1;
if( ss2 == 0 ) if( ss2 == 0 )
{ {
SetZero(); SetZero();
@ -908,6 +972,9 @@ public:
UInt<man*2> man_result; UInt<man*2> man_result;
uint i,c; uint i,c;
if( IsNan() || ss2.IsNan() )
return CheckCarry(1);
// man_result = mantissa * ss2.mantissa // man_result = mantissa * ss2.mantissa
mantissa.MulBig(ss2.mantissa, man_result); mantissa.MulBig(ss2.mantissa, man_result);
@ -938,7 +1005,7 @@ public:
c += Standardizing(); c += Standardizing();
return (c==0)? 0 : 1; return CheckCarry(c);
} }
@ -956,11 +1023,8 @@ public:
UInt<man*2> man2; UInt<man*2> man2;
uint i,c; uint i,c;
if( ss2.IsZero() ) if( IsNan() || ss2.IsNan() || ss2.IsZero() )
{ return CheckCarry(1);
// we don't divide by zero
return 1;
}
for(i=0 ; i<man ; ++i) for(i=0 ; i<man ; ++i)
{ {
@ -991,7 +1055,7 @@ public:
c += Standardizing(); c += Standardizing();
return (c==0)? 0 : 1; return CheckCarry(c);
} }
@ -1005,7 +1069,7 @@ public:
-12.6 mod -3 = -0.6 -12.6 mod -3 = -0.6
it means: it means:
in other words: this(old) = ss2 * q + this(new -- result) in other words: this(old) = ss2 * q + this(new)
*/ */
uint Mod(const Big<exp, man> & ss2) uint Mod(const Big<exp, man> & ss2)
{ {
@ -1013,14 +1077,23 @@ public:
uint c = 0; uint c = 0;
Big<exp, man> temp(*this); if( IsNan() || ss2.IsNan() )
return CheckCarry(1);
c += temp.Div(ss2); if( !SmallerWithoutSignThan(ss2) )
temp.SkipFraction(); {
c += temp.Mul(ss2); Big<exp, man> temp(*this);
c += Sub(temp);
return (c==0)? 0 : 1; c = temp.Div(ss2);
temp.SkipFraction();
c += temp.Mul(ss2);
c += Sub(temp);
if( !SmallerWithoutSignThan( ss2 ) )
c += 1;
}
return CheckCarry(c);
} }
@ -1039,30 +1112,35 @@ public:
template<uint pow_size> template<uint pow_size>
uint Pow(UInt<pow_size> pow) uint Pow(UInt<pow_size> pow)
{ {
if( IsNan() )
return 1;
if(pow.IsZero() && IsZero()) if(pow.IsZero() && IsZero())
{
// we don't define zero^zero // we don't define zero^zero
SetNan();
return 2; return 2;
}
Big<exp, man> start(*this), start_temp; Big<exp, man> start(*this), start_temp;
Big<exp, man> result; Big<exp, man> result;
result.SetOne(); result.SetOne();
uint c = 0;
while( !pow.IsZero() ) while( !c && !pow.IsZero() )
{ {
if( pow.table[0] & 1 ) if( pow.table[0] & 1 )
if( result.Mul(start) ) c += result.Mul(start);
return 1;
start_temp = start; start_temp = start;
if( start.Mul(start_temp) ) c += start.Mul(start_temp);
return 1;
pow.Rcr(1); pow.Rcr(1);
} }
*this = result; *this = result;
return 0; return CheckCarry(c);
} }
@ -1078,27 +1156,29 @@ public:
template<uint pow_size> template<uint pow_size>
uint Pow(Int<pow_size> pow) uint Pow(Int<pow_size> pow)
{ {
if( IsNan() )
return 1;
if( !pow.IsSign() ) if( !pow.IsSign() )
return Pow( UInt<pow_size>(pow) ); return Pow( UInt<pow_size>(pow) );
if( IsZero() ) if( IsZero() )
{
// if 'p' is negative then // if 'p' is negative then
// 'this' must be different from zero // 'this' must be different from zero
SetNan();
return 2; return 2;
}
if( pow.ChangeSign() ) uint c = pow.ChangeSign();
return 1;
Big<exp, man> t(*this); Big<exp, man> t(*this);
uint c_temp = t.Pow( UInt<pow_size>(pow) ); c += t.Pow( UInt<pow_size>(pow) ); // here can only be a carry (return:1)
if( c_temp > 0 )
return c_temp;
SetOne(); SetOne();
if( Div(t) ) c += Div(t);
return 1;
return 0; return CheckCarry(c);
} }
@ -1135,8 +1215,14 @@ public:
*/ */
uint PowUInt(Big<exp, man> pow) uint PowUInt(Big<exp, man> pow)
{ {
if( IsNan() || pow.IsNan() )
return CheckCarry(1);
if( pow.IsZero() && IsZero() ) if( pow.IsZero() && IsZero() )
{
SetNan();
return 2; return 2;
}
if( pow.IsSign() ) if( pow.IsSign() )
pow.Abs(); pow.Abs();
@ -1145,27 +1231,26 @@ public:
Big<exp, man> result; Big<exp, man> result;
Big<exp, man> one; Big<exp, man> one;
Int<exp> e_one; Int<exp> e_one;
uint c = 0;
e_one.SetOne(); e_one.SetOne();
one.SetOne(); one.SetOne();
result = one; result = one;
while( pow >= one ) while( !c && pow >= one )
{ {
if( pow.Mod2() ) if( pow.Mod2() )
if( result.Mul(start) ) c += result.Mul(start);
return 1;
start_temp = start; start_temp = start;
if( start.Mul(start_temp) ) c += start.Mul(start_temp);
return 1;
pow.exponent.Sub( e_one ); c += pow.exponent.Sub( e_one );
} }
*this = result; *this = result;
return 0; return CheckCarry(c);
} }
@ -1183,24 +1268,27 @@ public:
{ {
TTMATH_REFERENCE_ASSERT( pow ) TTMATH_REFERENCE_ASSERT( pow )
if( IsNan() || pow.IsNan() )
return CheckCarry(1);
if( !pow.IsSign() ) if( !pow.IsSign() )
return PowUInt(pow); return PowUInt(pow);
if( IsZero() ) if( IsZero() )
{
// if 'pow' is negative then // if 'pow' is negative then
// 'this' must be different from zero // 'this' must be different from zero
SetNan();
return 2; return 2;
}
Big<exp, man> temp(*this); Big<exp, man> temp(*this);
uint c_temp = temp.PowUInt(pow); uint c = temp.PowUInt(pow); // here can only be a carry (result:1)
if( c_temp > 0 )
return c_temp;
SetOne(); SetOne();
if( Div(temp) ) c += Div(temp);
return 1;
return 0; return CheckCarry(c);
} }
@ -1218,16 +1306,22 @@ public:
{ {
TTMATH_REFERENCE_ASSERT( pow ) TTMATH_REFERENCE_ASSERT( pow )
if( IsNan() || pow.IsNan() )
return CheckCarry(1);
Big<exp, man> temp; Big<exp, man> temp;
uint c = temp.Ln(*this); uint c = temp.Ln(*this);
if( c!= 0 ) if( c != 0 ) // can be 2 from Ln()
{
SetNan();
return c; return c;
}
c += temp.Mul(pow); c += temp.Mul(pow);
c += Exp(temp); c += Exp(temp);
return (c==0)? 0 : 1; return CheckCarry(c);
} }
@ -1245,11 +1339,17 @@ public:
{ {
TTMATH_REFERENCE_ASSERT( pow ) TTMATH_REFERENCE_ASSERT( pow )
if( IsNan() || pow.IsNan() )
return CheckCarry(1);
if( IsZero() ) if( IsZero() )
{ {
// 0^pow will be 0 only for pow>0 // 0^pow will be 0 only for pow>0
if( pow.IsSign() || pow.IsZero() ) if( pow.IsSign() || pow.IsZero() )
{
SetNan();
return 2; return 2;
}
SetZero(); SetZero();
@ -1271,7 +1371,7 @@ public:
private: private:
#ifdef CONSTANTSGENERATOR #ifdef TTMATH_CONSTANTSGENERATOR
public: public:
#endif #endif
@ -1299,7 +1399,7 @@ public:
old_value = *this; old_value = *this;
// we begin from 1 in order to not testing at the beginning // we begin from 1 in order to not testing at the beginning
#ifdef CONSTANTSGENERATOR #ifdef TTMATH_CONSTANTSGENERATOR
for(i=1 ; true ; ++i) for(i=1 ; true ; ++i)
#else #else
for(i=1 ; i<=TTMATH_ARITHMETIC_MAX_LOOP ; ++i) for(i=1 ; i<=TTMATH_ARITHMETIC_MAX_LOOP ; ++i)
@ -1360,6 +1460,9 @@ public:
{ {
uint c = 0; uint c = 0;
if( x.IsNan() )
return CheckCarry(1);
if( x.IsZero() ) if( x.IsZero() )
{ {
SetOne(); SetOne();
@ -1412,7 +1515,7 @@ public:
c += PowUInt(e_); c += PowUInt(e_);
} }
return (c==0)? 0 : 1; return CheckCarry(c);
} }
@ -1420,7 +1523,7 @@ public:
private: private:
#ifdef CONSTANTSGENERATOR #ifdef TTMATH_CONSTANTSGENERATOR
public: public:
#endif #endif
@ -1462,7 +1565,7 @@ public:
uint i; uint i;
#ifdef CONSTANTSGENERATOR #ifdef TTMATH_CONSTANTSGENERATOR
for(i=1 ; true ; ++i) for(i=1 ; true ; ++i)
#else #else
// we begin from 1 in order to not testing at the beginning // we begin from 1 in order to not testing at the beginning
@ -1525,15 +1628,21 @@ public:
return values: return values:
0 - ok 0 - ok
1 - overflow 1 - overflow (carry)
2 - incorrect argument (x<=0) 2 - incorrect argument (x<=0)
*/ */
uint Ln(const Big<exp,man> & x) uint Ln(const Big<exp,man> & x)
{ {
TTMATH_REFERENCE_ASSERT( x ) TTMATH_REFERENCE_ASSERT( x )
if( x.IsNan() )
return CheckCarry(1);
if( x.IsSign() || x.IsZero() ) if( x.IsSign() || x.IsZero() )
{
SetNan();
return 2; return 2;
}
// m will be the value of the mantissa in range <1,2) // m will be the value of the mantissa in range <1,2)
Big<exp,man> m(x); Big<exp,man> m(x);
@ -1552,7 +1661,7 @@ public:
c += exponent_temp.Mul(ln2); c += exponent_temp.Mul(ln2);
c += Add(exponent_temp); c += Add(exponent_temp);
return (c==0)? 0 : 1; return CheckCarry(c);
} }
@ -1574,14 +1683,23 @@ public:
TTMATH_REFERENCE_ASSERT( base ) TTMATH_REFERENCE_ASSERT( base )
TTMATH_REFERENCE_ASSERT( x ) TTMATH_REFERENCE_ASSERT( x )
if( x.IsNan() || base.IsNan() )
return CheckCarry(1);
if( x.IsSign() || x.IsZero() ) if( x.IsSign() || x.IsZero() )
{
SetNan();
return 2; return 2;
}
Big<exp,man> denominator;; Big<exp,man> denominator;;
denominator.SetOne(); denominator.SetOne();
if( base.IsSign() || base.IsZero() || base==denominator ) if( base.IsSign() || base.IsZero() || base==denominator )
{
SetNan();
return 3; return 3;
}
if( x == denominator ) // (this is: if x == 1) if( x == denominator ) // (this is: if x == 1)
{ {
@ -1590,14 +1708,14 @@ public:
return 0; return 0;
} }
// another error values we've tested at the start // another error values we've tested at the beginning
// there can be only a carry // there can only be a carry
uint c = Ln(x); uint c = Ln(x);
c += denominator.Ln(base); c += denominator.Ln(base);
c += Div(denominator); c += Div(denominator);
return (c==0)? 0 : 1; return CheckCarry(c);
} }
@ -1618,9 +1736,15 @@ public:
{ {
info = another.info; info = another.info;
if( exponent.FromInt(another.exponent) ) if( IsNan() )
return 1; return 1;
if( exponent.FromInt(another.exponent) )
{
SetNan();
return 1;
}
uint man_len_min = (man < another_man)? man : another_man; uint man_len_min = (man < another_man)? man : another_man;
uint i; uint i;
uint c = 0; uint c = 0;
@ -1634,7 +1758,7 @@ public:
// MS Visual Express 2005 reports a warning (in the lines with 'uint man_diff = ...'): // MS Visual Express 2005 reports a warning (in the lines with 'uint man_diff = ...'):
// warning C4307: '*' : integral constant overflow // warning C4307: '*' : integral constant overflow
// but we're using 'if( man > another_man )' and 'if( man < another_man )' and there'll be no such a situation here // but we're using 'if( man > another_man )' and 'if( man < another_man )' and there'll be no such situation here
#ifndef __GNUC__ #ifndef __GNUC__
#pragma warning( disable: 4307 ) #pragma warning( disable: 4307 )
#endif #endif
@ -1658,7 +1782,7 @@ public:
// mantissa doesn't have to be standardized (either the highest bit is set or all bits are equal zero) // mantissa doesn't have to be standardized (either the highest bit is set or all bits are equal zero)
CorrectZero(); CorrectZero();
return (c == 0 )? 0 : 1; return CheckCarry(c);
} }
@ -2467,13 +2591,16 @@ public:
FromBig(value); FromBig(value);
} }
/*! /*!
a default constructor a default constructor
warning: we don't set any of the members to zero etc. we don't set any of the members to zero
only NaN flag is set
*/ */
Big() Big()
{ {
SetNan();
} }
@ -2566,8 +2693,15 @@ public:
tchar_t decimal_point = TTMATH_COMMA_CHARACTER_1 ) const tchar_t decimal_point = TTMATH_COMMA_CHARACTER_1 ) const
{ {
static tchar_t error_overflow_msg[] = TTMATH_TEXT("overflow"); static tchar_t error_overflow_msg[] = TTMATH_TEXT("overflow");
static char error_nan_msg[] = "NaN";
result.erase(); result.erase();
if( IsNan() )
{
result = error_nan_msg;
return 0;
}
if(base<2 || base>16) if(base<2 || base>16)
{ {
result = error_overflow_msg; result = error_overflow_msg;
@ -3263,6 +3397,8 @@ public:
if( base<2 || base>16 ) if( base<2 || base>16 )
{ {
SetNan();
if( after_source ) if( after_source )
*after_source = source; *after_source = source;
@ -3292,7 +3428,7 @@ public:
if( value_read ) if( value_read )
*value_read = value_read_temp; *value_read = value_read_temp;
return (c==0)? 0 : 1; return CheckCarry(c);
} }
@ -3593,6 +3729,7 @@ public:
and returns the result and returns the result
(in other words it treats 'this' and 'ss2' as values without a sign) (in other words it treats 'this' and 'ss2' as values without a sign)
we don't check the NaN flag
*/ */
bool SmallerWithoutSignThan(const Big<exp,man> & ss2) const bool SmallerWithoutSignThan(const Big<exp,man> & ss2) const
{ {
@ -3627,6 +3764,7 @@ public:
and returns the result and returns the result
(in other words it treats 'this' and 'ss2' as values without a sign) (in other words it treats 'this' and 'ss2' as values without a sign)
we don't check the NaN flag
*/ */
bool GreaterWithoutSignThan(const Big<exp,man> & ss2) const bool GreaterWithoutSignThan(const Big<exp,man> & ss2) const
{ {
@ -3661,6 +3799,7 @@ public:
and returns the result and returns the result
(in other words it treats 'this' and 'ss2' as values without a sign) (in other words it treats 'this' and 'ss2' as values without a sign)
we don't check the NaN flag
*/ */
bool EqualWithoutSign(const Big<exp,man> & ss2) const bool EqualWithoutSign(const Big<exp,man> & ss2) const
{ {
@ -3867,7 +4006,7 @@ public:
*/ */
void SkipFraction() void SkipFraction()
{ {
if( IsZero() ) if( IsNan() || IsZero() )
return; return;
if( !exponent.IsSign() ) if( !exponent.IsSign() )
@ -3901,7 +4040,7 @@ public:
*/ */
void RemainFraction() void RemainFraction()
{ {
if( IsZero() ) if( IsNan() || IsZero() )
return; return;
if( !exponent.IsSign() ) if( !exponent.IsSign() )
@ -3951,6 +4090,9 @@ public:
Big<exp,man> half; Big<exp,man> half;
uint c; uint c;
if( IsNan() )
return 1;
half.Set05(); half.Set05();
if( IsSign() ) if( IsSign() )
@ -3966,7 +4108,7 @@ public:
SkipFraction(); SkipFraction();
return c; return CheckCarry(c);
} }

View File

@ -971,8 +971,9 @@ public:
Mul2Big(ss2, result); Mul2Big(ss2, result);
// copying result // copying result
for(i=0 ; i<value_size ; ++i) memcpy(table,result.table,sizeof(table));
table[i] = result.table[i]; //for(i=0 ; i<value_size ; ++i)
// table[i] = result.table[i];
// testing carry // testing carry
for( ; i<value_size*2 ; ++i) for( ; i<value_size*2 ; ++i)

View File

@ -91,8 +91,8 @@ namespace ttmath
__asm __asm
{ {
xor eax,eax // eax=0 xor eax,eax // eax=0
xor edx,edx // edx=0
mov ecx,[b] mov ecx,[b]
mov edx,eax // edx=0
mov ebx,[p1] mov ebx,[p1]
mov esi,[p2] mov esi,[p2]
@ -290,24 +290,22 @@ namespace ttmath
__asm __asm
{ {
mov ecx, [b] mov ecx, [b]
sub ecx, [index]
mov ebx, [p1]
mov edx, [index] mov edx, [index]
mov ebx, [p1]
mov eax, [x1] mov eax, [x1]
sub ecx, edx // max uints to add (value_size - index)
add [ebx+edx*4], eax add [ebx+edx*4], eax
lea edx, [edx+1] // inc edx, but faster (no flags dependencies)
lea ecx, [ecx-1] lea ecx, [ecx-1]
mov eax, [x2] mov eax, [x2]
ALIGN 16 ALIGN 16
p: p:
adc [ebx+edx*4], eax adc [ebx+edx*4+4], eax
jnc end jnc end
xor eax, eax mov eax, 0
lea edx, [edx+1] // inc edx, but faster (no flags dependencies) lea edx, [edx+1] // inc edx, but faster (no flags dependencies)
dec ecx dec ecx
jnz p jnz p