From 85219c48da0d3fff98a9d741e09d34e74abfec0b Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 17 Apr 2013 22:58:52 -0600 Subject: Division, attempt one - broken. It doesn't work by any means, and I think I confused myself partway through. I shouldn't code when I'm that tired, at least not on something this complex. --- src/main.cpp | 1 + src/number.cpp | 34 ++++++++++++++++++++-------------- src/packedintarray.cpp | 12 ++++++++++++ src/packedintarray.h | 1 + 4 files changed, 34 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 23388ab..fe9a1fe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -286,6 +286,7 @@ void fractest() println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); println("%1 - %2 = %3").arg( a ).arg( b ).arg( a - b ); println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); + println("%1 / %2 = %3").arg( a ).arg( b ).arg( a / b ); } int main( int , char *[] ) diff --git a/src/number.cpp b/src/number.cpp index 2d45e7e..1c3cb3f 100644 --- a/src/number.cpp +++ b/src/number.cpp @@ -452,26 +452,26 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const else q.bPositive = false; - int iMinWidth = rhs.aInt.getSize(); + int iMinWidth = Bu::buMax(1,rhs.aInt.getSize()); int iAnchor = r.aInt.getSize()-iMinWidth; int iSample = iMinWidth; do { -// Bu::println("%1\n-----").arg( r.aInt.toString() ); + Bu::println("%1\n-----").arg( r.aInt.toString() ); Number sub( iScale, iRadix ); for(;;) { -// Bu::println(" -> Anchor: %1, Sample: %2").arg( iAnchor ).arg( iSample ); + Bu::println(" -> Anchor: %1, Sample: %2").arg( iAnchor ).arg( iSample ); sub.aInt.set( r.aInt, iAnchor, iSample ); -// Bu::println("%1\n%2\n----").arg( sub.toString() ).arg( rhs.toString() ); + Bu::println("%1\n%2\n----").arg( sub.toString() ).arg( rhs.toString() ); if( sub < rhs ) { iSample++; iAnchor--; if( iAnchor < 0 ) { -// Bu::println("[Inner exit] Complete: q = %1, r = %2").arg( q.toString() ).arg( r.toString() ); + Bu::println("[Inner exit] Complete: q = %1, r = %2").arg( q.toString() ).arg( r.toString() ); return; } } @@ -479,29 +479,35 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const break; } - Number x( iScale, iRadix ); - int iRes = 0; + Number x( rhs.iScale, iRadix ); + int iRes = -1; for( ; x <= sub; iRes++, x = x + rhs ) { } x = sub - (x - rhs); + if( !x.bPositive ) + { + iSample++; + iAnchor--; + continue; + } for( int j = 0; iAnchor+j >= 0 && j < x.aInt.getSize(); j++ ) r.aInt.set( iAnchor+j, x.aInt.get( j ) ); while( r.aInt.getSize() > iAnchor+x.aInt.getSize() ) r.aInt.remove(); -// Bu::println(" -> Post remainder patch: %1 -> %2"). -// arg( x.toString() ).arg( r.toString() ); + Bu::println(" -> Post remainder patch: %1 -> %2"). + arg( x.toString() ).arg( r.toString() ); -// Bu::println("%1 (%2)").arg( iRes-1 ).arg( x.toString() ); + Bu::println("%1 (%2)").arg( iRes ).arg( x.toString() ); while( q.aInt.getSize() <= iAnchor ) q.aInt.append(0); - q.aInt.set( iAnchor, iRes-1 ); + q.aInt.set( iAnchor, iRes ); iSample = iMinWidth; iAnchor = r.aInt.getSize()-iMinWidth; -// Bu::println(" -> new Anchor: %1, Sample: %2").arg( iAnchor ). -// arg( iSample ); + Bu::println(" -> new Anchor: %1, Sample: %2").arg( iAnchor ). + arg( iSample ); } while( iAnchor >= 0 ); -// Bu::println("Complete: q = %1, r = %2").arg( q.toString() ).arg( r.toString() ); + Bu::println("Complete: q = %1, r = %2").arg( q.toString() ).arg( r.toString() ); } Bu::Formatter &operator<<( Bu::Formatter &f, const Number &n ) diff --git a/src/packedintarray.cpp b/src/packedintarray.cpp index d395aa2..bcd2f66 100644 --- a/src/packedintarray.cpp +++ b/src/packedintarray.cpp @@ -142,6 +142,18 @@ void PackedIntArray::set( const PackedIntArray &rSrc ) memcpy( aData, rSrc.aData, iSize*sizeof(Store) ); } +void PackedIntArray::copy( int iDest, const PackedIntArray &rSrc, int iStart, + int iSize ) +{ + iCount = iSize; + checkCapacity(); + + for( int j = 0; j < iSize; j++ ) + { + set( j+iDest, rSrc.get( iStart+j ) ); + } +} + void PackedIntArray::trim() { while( iCount > 0 && get( iCount-1 ) == 0 ) diff --git a/src/packedintarray.h b/src/packedintarray.h index ec109e5..457acf5 100644 --- a/src/packedintarray.h +++ b/src/packedintarray.h @@ -21,6 +21,7 @@ public: int getSize() const { return iCount; } void set( const PackedIntArray &rSrc, int iStart, int iSize ); void set( const PackedIntArray &rSrc ); + void copy( int iDest, const PackedIntArray &rSrc, int iStart, int iSize ); void trim(); int getBitWidth() const { return iBitWidth; } -- cgit v1.2.3