From a520fc5740da7d50a289357e4e6e529b826454e7 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sun, 21 Apr 2013 21:51:17 -0600 Subject: FIxed fractional support in comparisons. They still don't handle mixed scale comparisons correctly, it shouldn't be too hard to add, but yeah...not supported yet. --- src/main.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/number.cpp | 46 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 102 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 5e420f5..d1b9d1c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -194,6 +194,66 @@ void numbertestcomp() compcheck( -123, <=, -122 ); compcheck( -122, <=, -123 ); compcheck( 123, <=, -122 ); + + println("-==-==- Non-Integer Test -==-==-"); + + a.setScale( 8 ); + b.setScale( 8 ); + println("-==- Greater Than -==-"); + compcheck( 10.1, >, 10.4 ); + compcheck( 10.1, >, 10.1 ); + compcheck( 10.4, >, 10.1 ); + compcheck( 10.413, >, 10.413 ); + compcheck( 10.41329135, >, 10.41329134 ); + compcheck( 10.41329134, >, 10.41329135 ); + compcheck( 10.41329135, >, 10.41329135 ); + compcheck( -123.3, >, 123.2 ); + compcheck( -123.3, >, -123.2 ); + compcheck( -123.3, >, -123.3 ); + compcheck( -123.3, >, -123.2 ); + compcheck( 123.3, >, -123.2 ); + + println("-==- Less Than -==-"); + compcheck( 10.1, <, 10.4 ); + compcheck( 10.1, <, 10.1 ); + compcheck( 10.4, <, 10.1 ); + compcheck( 10.413, <, 10.413 ); + compcheck( 10.41329135, <, 10.41329134 ); + compcheck( 10.41329134, <, 10.41329135 ); + compcheck( 10.41329135, <, 10.41329135 ); + compcheck( -123.3, <, 123.2 ); + compcheck( -123.3, <, -123.2 ); + compcheck( -123.3, <, -123.3 ); + compcheck( -123.3, <, -123.2 ); + compcheck( 123.3, <, -123.2 ); + + println("-==- Greater Than or Equal To -==-"); + compcheck( 10.1, >=, 10.4 ); + compcheck( 10.1, >=, 10.1 ); + compcheck( 10.4, >=, 10.1 ); + compcheck( 10.413, >=, 10.413 ); + compcheck( 10.41329135, >=, 10.41329134 ); + compcheck( 10.41329134, >=, 10.41329135 ); + compcheck( 10.41329135, >=, 10.41329135 ); + compcheck( -123.3, >=, 123.2 ); + compcheck( -123.3, >=, -123.2 ); + compcheck( -123.3, >=, -123.3 ); + compcheck( -123.3, >=, -123.2 ); + compcheck( 123.3, >=, -123.2 ); + + println("-==- Less Than or Equal To -==-"); + compcheck( 10.1, <=, 10.4 ); + compcheck( 10.1, <=, 10.1 ); + compcheck( 10.4, <=, 10.1 ); + compcheck( 10.413, <=, 10.413 ); + compcheck( 10.41329135, <=, 10.41329134 ); + compcheck( 10.41329134, <=, 10.41329135 ); + compcheck( 10.41329135, <=, 10.41329135 ); + compcheck( -123.3, <=, 123.2 ); + compcheck( -123.3, <=, -123.2 ); + compcheck( -123.3, <=, -123.3 ); + compcheck( -123.3, <=, -123.2 ); + compcheck( 123.3, <=, -123.2 ); } int getHob( int x ) diff --git a/src/number.cpp b/src/number.cpp index d16bd98..b7a420c 100644 --- a/src/number.cpp +++ b/src/number.cpp @@ -133,6 +133,11 @@ bool Number::operator==( const Number &rhs ) const if( aInt[j] != rhs.aInt[j] ) return false; } + for( int j = 0; j < aFrac.getSize(); j++ ) + { + if( aFrac[j] != rhs.aFrac[j] ) + return false; + } return true; } @@ -164,6 +169,14 @@ bool Number::operator>( const Number &rhs ) const else if( iDiff > 0 ) return bPositive; } + for( int j = 0; j < iScale; j++ ) + { + int iDiff = aFrac[j] - rhs.aFrac[j]; + if( iDiff < 0 ) + return !bPositive; + else if( iDiff > 0 ) + return bPositive; + } return false; } @@ -189,6 +202,14 @@ bool Number::operator<( const Number &rhs ) const else if( iDiff < 0 ) return bPositive; } + for( int j = 0; j < iScale; j++ ) + { + int iDiff = aFrac[j] - rhs.aFrac[j]; + if( iDiff > 0 ) + return !bPositive; + else if( iDiff < 0 ) + return bPositive; + } return false; } @@ -214,6 +235,14 @@ bool Number::operator>=( const Number &rhs ) const else if( iDiff > 0 ) return bPositive; } + for( int j = 0; j < iScale; j++ ) + { + int iDiff = aFrac[j] - rhs.aFrac[j]; + if( iDiff < 0 ) + return !bPositive; + else if( iDiff > 0 ) + return bPositive; + } return true; } @@ -239,6 +268,14 @@ bool Number::operator<=( const Number &rhs ) const else if( iDiff < 0 ) return bPositive; } + for( int j = 0; j < iScale; j++ ) + { + int iDiff = aFrac[j] - rhs.aFrac[j]; + if( iDiff > 0 ) + return !bPositive; + else if( iDiff < 0 ) + return bPositive; + } return true; } @@ -518,14 +555,15 @@ void Number::setScale( int iNewScale ) return; else if( iScale < iNewScale ) { - for(; iScale < iNewScale; iNewScale-- ) - aFrac.remove(); + while( aFrac.getSize() < iNewScale ) + aFrac.append(0); } else { - for(; iScale > iNewScale; iNewScale++ ) - aFrac.append(0); + while( aFrac.getSize() > iNewScale ) + aFrac.remove(); } + iScale = iNewScale; } Number Number::add( const Number &rhs, bool bSub ) const -- cgit v1.2.3