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