From 9ad1a65f4dcbc31b031556803d27dc688a16ff4a Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 19 Apr 2013 14:47:40 -0600 Subject: It does fractional division, but the result is an int. It's funny, I haven't extended division past the ones place yet, but it does work correctly, so in theory it won't be too hard to do. I may need a little bit of extra code in the PackedIntArray class to insert a new digit at the begining. --- src/number.cpp | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) (limited to 'src/number.cpp') diff --git a/src/number.cpp b/src/number.cpp index 1c3cb3f..b923d5f 100644 --- a/src/number.cpp +++ b/src/number.cpp @@ -58,7 +58,7 @@ Number Number::operator*( const Number &rhs ) const int iCarry = 0; int iZeros = 0; - for( int j = 1-iScale-rhs.iScale; j < iCnt || iCarry > 0; j++ ) + for( int j = -iScale-rhs.iScale; j < iCnt || iCarry > 0; j++ ) { // Bu::println("Pos %1").arg(j); int iPos = iCarry; @@ -363,7 +363,7 @@ Number Number::add( const Number &rhs, bool bSub ) const if( bPositive == (bSub?!rhs.bPositive:rhs.bPositive)) { ret.bPositive = bPositive; - for( int j = 1-iScale; j < iPlaces || iCarry > 0; j++ ) + 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"). @@ -396,7 +396,7 @@ Number Number::add( const Number &rhs, bool bSub ) const ret.bPositive = bPositive; int iRes; int iComp = (iRadix-1); - for( int j = 1-iScale; j < iPlaces; j++ ) + for( int j = -iScale; j < iPlaces; j++ ) { iRes = digit( j ) + (iComp-rhs.digit( j )) + iCarry; // Bu::println(" Place: %1 + %2 + (%3) = %4"). @@ -441,11 +441,35 @@ Number Number::add( const Number &rhs, bool bSub ) const void Number::divide( const Number &rhs, Number &q, Number &r ) const { - q.aInt.clear(); - r = *this; - - if( rhs.aInt.getSize() > aInt.getSize() ) + if( rhs.iScale > 0 ) + { + Number newrhs = Number( 0, iRadix ); + Number newbase = Number( iScale, iRadix ); + int iOff; + for( iOff = -iScale; iOff < 0 && rhs.digit( iOff ) == 0; iOff++ ) { } + for( int j = iOff; j < rhs.aInt.getSize(); j++ ) + { + newrhs.aInt.append( rhs.digit( j ) ); + newbase.aInt.append( digit( j ) ); + } + for( int j = 0; j < aInt.getSize(); j++ ) + { + newbase.aInt.append( aInt.get( j ) ); + } + for( int j = iScale+iOff; j >= -iOff; j-- ) + { + newbase.aFrac.set( j+iOff, aFrac.get( j ) ); + } + + Bu::println("Conv %1 => %2 (iOff = %3)").arg( rhs ).arg( newrhs ).arg( iOff ); + Bu::println("Conv %1 => %2 (iOff = %3)").arg( *this ).arg( newbase ).arg( iOff ); + + newbase.divide( newrhs, q, r ); return; + } + + q = Number( iScale, iRadix ); + r = *this; if( bPositive == rhs.bPositive ) q.bPositive = true; @@ -481,12 +505,16 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const Number x( rhs.iScale, iRadix ); int iRes = -1; - for( ; x <= sub; iRes++, x = x + rhs ) { } + Bu::println("{x = %1, sub=%2, rhs=%4, iRes=%3}").arg( x ).arg(sub).arg(iRes).arg(rhs); + for( ; x <= sub; iRes++, x = x + rhs ) { + Bu::println("{x = %1, sub=%2, rhs=%4, iRes=%3, x+rhs=%5}").arg( x ).arg(sub).arg(iRes).arg(rhs).arg(x+rhs); + } x = sub - (x - rhs); if( !x.bPositive ) { iSample++; iAnchor--; + Bu::println("What? it's negative? %1").arg( x ); continue; } for( int j = 0; iAnchor+j >= 0 && j < x.aInt.getSize(); j++ ) -- cgit v1.2.3