fixed/optimized: Big::AboutEqual() didn't compile on GCC
(there was used ttmath::Abs() function which is unknown in Big<> class) AboutEqual() returned true for: "2345.5" and "-2345.5" (the sign was not checked) renamed: Big::AboutEqual() to Big::AboutEqualWithoutSign() added: Big::AboutEqual() git-svn-id: svn://ttmath.org/publicrep/ttmath/trunk@189 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
parent
28964d30f7
commit
74b31b1f54
|
@ -1,4 +1,4 @@
|
|||
Version 0.9.0 prerelease (2009.09.07):
|
||||
Version 0.9.0 prerelease (2009.09.10):
|
||||
* added: support for wide characters (wchar_t)
|
||||
wide characters are used when macro TTMATH_USE_WCHAR is defined
|
||||
this macro is defined automatically when there is macro UNICODE or _UNICODE defined
|
||||
|
@ -20,7 +20,8 @@ Version 0.9.0 prerelease (2009.09.07):
|
|||
* added: multithread support for Big<> class
|
||||
you should compile with TTMATH_MULTITHREADS
|
||||
and use TTMATH_MULTITHREADS_HELPER macro somewhere in your *.cpp file
|
||||
* added: Big::AboutEqual(const Big<exp,man> & ss2, int nBitsToIgnore = 4)
|
||||
* added: Big::AboutEqual(const Big<exp,man> & ss2, int nBitsToIgnore)
|
||||
Big::AboutEqualWithoutSign(const Big<exp,man> & ss2, int nBitsToIgnore)
|
||||
the last nBitsToIgnore bits from mantissas will be skipped when comparing
|
||||
* added: x86_64 asm code for Microsoft Visual compiler
|
||||
file: ttmathuint_x86_64_msvc.asm
|
||||
|
|
|
@ -3916,74 +3916,78 @@ public:
|
|||
}
|
||||
|
||||
|
||||
bool AboutEqual(const Big<exp,man> & ss2, int nBitsToIgnore = 4) const
|
||||
/*!
|
||||
this method compares this and ss2 by ingoring some nBitsToIgnore last bits from mantissas
|
||||
if nBitsToIgnore is 0 then the method is equivalent to EqualWithoutSign() method
|
||||
(signs are not checked, we don't check the NaN flag too)
|
||||
|
||||
sample:
|
||||
Big<1,1> a, b;
|
||||
a = " 1234.5678";
|
||||
b = "-1234.5677999";
|
||||
// binary a: 10011010010.100100010101101101010
|
||||
// binary b: -10011010010.100100010101101101000
|
||||
// two last bits are different
|
||||
if( a.AboutEqualWithoutSign(b, 2) ) // the result is true
|
||||
std::cout << "true" << std::endl;
|
||||
*/
|
||||
bool AboutEqualWithoutSign(const Big<exp,man> & ss2, uint nBitsToIgnore) const
|
||||
{
|
||||
// we should check the mantissas beforehand because sometimes we can have
|
||||
// a mantissa set to zero but in the exponent something another value
|
||||
// (maybe we've forgotten about calling CorrectZero() ?)
|
||||
if( mantissa.IsZero() )
|
||||
// there is no sense to ignore the whole mantissas
|
||||
TTMATH_ASSERT( nBitsToIgnore < man*TTMATH_BITS_PER_UINT )
|
||||
|
||||
uint words = nBitsToIgnore / TTMATH_BITS_PER_UINT;
|
||||
uint bits = nBitsToIgnore % TTMATH_BITS_PER_UINT;
|
||||
uint i;
|
||||
|
||||
if( bits == 0 )
|
||||
{
|
||||
if( ss2.mantissa.IsZero() )
|
||||
return true;
|
||||
|
||||
return(ss2.AboutEqual(*this,nBitsToIgnore));
|
||||
}
|
||||
|
||||
if( ss2.mantissa.IsZero() )
|
||||
{
|
||||
return(this->exponent <= uint(2*(-sint(man*TTMATH_BITS_PER_UINT))+nBitsToIgnore));
|
||||
}
|
||||
|
||||
// exponents may not differ much!
|
||||
ttmath::Int<exp> expdiff(this->exponent - ss2.exponent);
|
||||
|
||||
// they may differ one if for example mantissa1=0x80000000, mantissa2=0xffffffff
|
||||
if( ttmath::Abs(expdiff) > 1 )
|
||||
return(false);
|
||||
|
||||
// calculate the 'difference' mantissa
|
||||
ttmath::UInt<man> man1(this->mantissa);
|
||||
ttmath::UInt<man> man2(ss2.mantissa);
|
||||
ttmath::UInt<man> mandiff;
|
||||
|
||||
switch( expdiff.ToInt() )
|
||||
{
|
||||
case +1:
|
||||
man2.Rcr(1,0);
|
||||
mandiff = man1;
|
||||
mandiff.Sub(man2);
|
||||
break;
|
||||
case -1:
|
||||
man1.Rcr(1,0);
|
||||
mandiff = man2;
|
||||
mandiff.Sub(man1);
|
||||
break;
|
||||
default:
|
||||
if( man2 > man1 )
|
||||
{
|
||||
mandiff = man2;
|
||||
mandiff.Sub(man1);
|
||||
i = words;
|
||||
}
|
||||
else
|
||||
{
|
||||
mandiff = man1;
|
||||
mandiff.Sub(man2);
|
||||
}
|
||||
break;
|
||||
i = words + 1;
|
||||
uint mask = TTMATH_UINT_MAX_VALUE << bits;
|
||||
|
||||
if( (mantissa.table[words] & mask) != (ss2.mantissa.table[words] & mask) )
|
||||
return false;
|
||||
}
|
||||
|
||||
// faster to mask the bits!
|
||||
TTMATH_ASSERT( nBitsToIgnore < TTMATH_BITS_PER_UINT );
|
||||
|
||||
for( int n = man-1; n > 0; --n )
|
||||
for( ; i<man ; ++i )
|
||||
{
|
||||
if( mandiff.table[n] != 0 )
|
||||
return(false);
|
||||
if( mantissa.table[i] != ss2.mantissa.table[i] )
|
||||
return false;
|
||||
}
|
||||
|
||||
uint nMask = ~((1 << nBitsToIgnore) - 1);
|
||||
if( exponent != ss2.exponent )
|
||||
return false;
|
||||
|
||||
return((mandiff.table[0] & nMask) == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
this method compares this and ss2 by ingoring some nBitsToIgnore last bits from mantissas
|
||||
if nBitsToIgnore is 0 then the method is equivalent to operator==() operator
|
||||
(we don't check the NaN flag)
|
||||
|
||||
this method is similar to AboutEqualWithoutSign but the signs are checked now
|
||||
sample:
|
||||
Big<1,1> a, b;
|
||||
a = "456.6789";
|
||||
b = "456.678901";
|
||||
// binary a: 111001000.10101101110011000101111
|
||||
// binary b: 111001000.10101101110011000110111
|
||||
// five last bits are different
|
||||
if( a.AboutEqual(b, 5) ) // the result is true
|
||||
std::cout << "true" << std::endl;
|
||||
*/
|
||||
bool AboutEqual(const Big<exp,man> & ss2, uint nBitsToIgnore) const
|
||||
{
|
||||
if( IsSign() != ss2.IsSign() )
|
||||
return false;
|
||||
|
||||
return AboutEqualWithoutSign(ss2, nBitsToIgnore);
|
||||
}
|
||||
|
||||
|
||||
|
@ -4006,8 +4010,6 @@ public:
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool operator==(const Big<exp,man> & ss2) const
|
||||
{
|
||||
if( IsSign() != ss2.IsSign() )
|
||||
|
@ -4017,8 +4019,6 @@ public:
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool operator>(const Big<exp,man> & ss2) const
|
||||
{
|
||||
if( IsSign() && !ss2.IsSign() )
|
||||
|
@ -4038,7 +4038,6 @@ public:
|
|||
}
|
||||
|
||||
|
||||
|
||||
bool operator>=(const Big<exp,man> & ss2) const
|
||||
{
|
||||
return !operator<( ss2 );
|
||||
|
|
Loading…
Reference in New Issue