From f03026f5c2cde1eecfda273eaa52df10287f0f9e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 29 Jan 2015 08:49:55 -0700 Subject: Fixed nasty subtraction bug when dealing with fractions. It turned out to be a really simple solution, but man, that was embarassing. I forgot to include the fractional portion of a number when fixing my radix+1 compliment numbers. --- src/config.h | 3 ++- src/number.cpp | 59 ++++++++++++++++++++++++++++++++++++------------------ src/number.h | 1 + src/unitnumber.cpp | 12 +++++++---- src/unitnumber.h | 1 + 5 files changed, 52 insertions(+), 24 deletions(-) diff --git a/src/config.h b/src/config.h index f55be13..482ef9e 100644 --- a/src/config.h +++ b/src/config.h @@ -1,11 +1,12 @@ #ifndef CONFIG_H #define CONFIG_H -#define CLIC_VERSION "0.12" +#define CLIC_VERSION "0.13" #define CLIC_VERSION_STR "Clic v" CLIC_VERSION #define DEBUG_DIVIDE false #define DEBUG_PARSE false +#define DEBUG_ADD false #define DBS( s, x ) if( DEBUG_ ##s ) { x; } (void)0 #define DBS_START( s ) if( DEBUG_ ##s ) { (void)0 diff --git a/src/number.cpp b/src/number.cpp index eb803be..660ba61 100644 --- a/src/number.cpp +++ b/src/number.cpp @@ -610,6 +610,19 @@ int Number::digit( int iIdx ) const return aInt[iIdx]; } +void Number::setDigit( int iIdx, int iVal ) +{ + if( iIdx < 0 ) + { + if( iIdx >= -iScale ) + aFrac.set( -1-iIdx, iVal ); + return; + } + if( iIdx >= aInt.getSize() ) + return; + aInt.set( iIdx, iVal ); +} + void Number::setScale( int iNewScale ) { if( iNewScale == 0 ) @@ -680,7 +693,7 @@ Number Number::add( const Number &rhs, bool bSub ) const int iZeros = 0; int iCarry = 0; -// Bu::println("::: %1 + %2:").arg( toString() ).arg( rhs.toString() ); + DBS( ADD, Bu::println("::: %1 + %2:").arg( toString() ).arg( rhs.toString() ) ); if( bPositive == (bSub?!rhs.bPositive:rhs.bPositive)) { @@ -688,9 +701,10 @@ Number Number::add( const Number &rhs, bool bSub ) const for( int j = -iScale; j < iPlaces || iCarry > 0; j++ ) { int iRes = iCarry + digit( j ) + rhs.digit( j ); -// Bu::println(" [%5] Place: %1 + %2 + (%3) = %4"). -// arg( digit(j) ).arg( rhs.digit( j ) ).arg( iCarry ) -// .arg( iRes ).arg( j ); + DBS( ADD, + Bu::println(" [%5] Place: %1 + %2 + (%3) = %4"). + arg( digit(j) ).arg( rhs.digit( j ) ).arg( iCarry ) + .arg( iRes ).arg( j ) ); if( j < 0 ) ret.aFrac.set( -1-j, (iRes%iRadix) ); else @@ -714,16 +728,25 @@ Number Number::add( const Number &rhs, bool bSub ) const else { iCarry = 1; -// Bu::println("--method b--"); + DBS( ADD, Bu::println("--method b--") ); ret.bPositive = bPositive; int iRes; int iComp = (iRadix-1); + for( int j = -iScale; j < iPlaces; j++ ) { iRes = digit( j ) + (iComp-rhs.digit( j )) + iCarry; -// Bu::println(" Place: %1 + %2 + (%3) = %4"). -// arg( digit(j) ).arg( 9-rhs.digit( j ) ).arg( iCarry ) -// .arg( iRes ); + if( iRes < iRadix ) + { + iCarry = 0; + } + else + iCarry = iRes/iRadix; + + DBS( ADD, + Bu::println(" Place: %1 + %2 + (%3) = %4"). + arg( digit(j) ).arg( iComp-rhs.digit( j ) ).arg( iCarry ) + .arg( iRes ) ); if( j < 0 ) ret.aFrac.set( -1-j, (iRes%iRadix) ); else @@ -738,24 +761,22 @@ Number Number::add( const Number &rhs, bool bSub ) const ret.aInt.append( (iRes%iRadix) ); } - if( iRes < iRadix ) - iCarry = 0; - else - iCarry = iRes/iRadix; } if( iCarry == 0 ) { ret.bPositive = false; - ret.aInt.set( 0, iComp-ret.aInt.get( 0 )+1); - for( int j = 1; j < ret.aInt.getSize(); j++ ) + int iVal = iComp-ret.digit( -ret.iScale )+1; + iCarry = (iVal >= iRadix) ? 1 : 0; + ret.setDigit( -ret.iScale, iVal%iRadix ); + + for( int j = -iScale+1; j < ret.aInt.getSize(); j++ ) { - ret.aInt.set( j, iComp-ret.aInt.get( j )); + iVal = iComp-ret.digit( j )+iCarry; + iCarry = (iVal >= iRadix) ? 1 : 0; + ret.setDigit( j, iVal%iRadix ); } } - else - { - ret.aInt.trim(); - } + ret.aInt.trim(); } return ret; diff --git a/src/number.h b/src/number.h index 2f7a583..79cd5f5 100644 --- a/src/number.h +++ b/src/number.h @@ -45,6 +45,7 @@ public: Bu::String toString() const; int digit( int iIdx ) const; + void setDigit( int iIdx, int iVal ); int getScale() const { return iScale; } int getEffectiveScale() const; diff --git a/src/unitnumber.cpp b/src/unitnumber.cpp index 985f0d9..d213ed7 100644 --- a/src/unitnumber.cpp +++ b/src/unitnumber.cpp @@ -136,13 +136,17 @@ void UnitNumber::number1() #define mathTest( anum, op, bnum, scale, answ ) \ unitTest( (Number(anum, scale) op Number(bnum, scale)).toString() == answ ) -/* + void UnitNumber::number2() { - mathTest( "1", /, "17", 10, "0.0588235294" ); - mathTest( "1", /, "177", 10, "0.0056497175" ); + mathTest("55", +, "-100", 0, "-45"); + mathTest("30.01", +, "-31.21", 2, "-1.20"); + mathTest("55", -, "100", 0, "-45"); + mathTest("30.01", -, "31.21", 2, "-1.20"); +// mathTest( "1", /, "17", 10, "0.0588235294" ); +// mathTest( "1", /, "177", 10, "0.0056497175" ); } -*/ + /* // clic diff --git a/src/unitnumber.h b/src/unitnumber.h index 96d882e..097fd5b 100644 --- a/src/unitnumber.h +++ b/src/unitnumber.h @@ -14,6 +14,7 @@ public: void multiply1(); void divide1(); void number1(); + void number2(); void compare1(); void radix1(); void fraction1(); -- cgit v1.2.3