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/number.cpp | 59 +++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 19 deletions(-) (limited to 'src/number.cpp') 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; -- cgit v1.2.3