diff options
author | Mike Buland <mike@xagasoft.com> | 2013-04-17 22:58:52 -0600 |
---|---|---|
committer | Mike Buland <mike@xagasoft.com> | 2013-04-17 22:58:52 -0600 |
commit | 85219c48da0d3fff98a9d741e09d34e74abfec0b (patch) | |
tree | 39a5b8c13926dd2083c8552d701276678f6515c5 /src | |
parent | 55ef5612f8950bd042ffd6d001908845a4321f8e (diff) | |
download | clic-85219c48da0d3fff98a9d741e09d34e74abfec0b.tar.gz clic-85219c48da0d3fff98a9d741e09d34e74abfec0b.tar.bz2 clic-85219c48da0d3fff98a9d741e09d34e74abfec0b.tar.xz clic-85219c48da0d3fff98a9d741e09d34e74abfec0b.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 1 | ||||
-rw-r--r-- | src/number.cpp | 34 | ||||
-rw-r--r-- | src/packedintarray.cpp | 12 | ||||
-rw-r--r-- | src/packedintarray.h | 1 |
4 files changed, 34 insertions, 14 deletions
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() | |||
286 | println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); | 286 | println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); |
287 | println("%1 - %2 = %3").arg( a ).arg( b ).arg( a - b ); | 287 | println("%1 - %2 = %3").arg( a ).arg( b ).arg( a - b ); |
288 | println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); | 288 | println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); |
289 | println("%1 / %2 = %3").arg( a ).arg( b ).arg( a / b ); | ||
289 | } | 290 | } |
290 | 291 | ||
291 | int main( int , char *[] ) | 292 | 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 | |||
452 | else | 452 | else |
453 | q.bPositive = false; | 453 | q.bPositive = false; |
454 | 454 | ||
455 | int iMinWidth = rhs.aInt.getSize(); | 455 | int iMinWidth = Bu::buMax(1,rhs.aInt.getSize()); |
456 | 456 | ||
457 | int iAnchor = r.aInt.getSize()-iMinWidth; | 457 | int iAnchor = r.aInt.getSize()-iMinWidth; |
458 | int iSample = iMinWidth; | 458 | int iSample = iMinWidth; |
459 | do | 459 | do |
460 | { | 460 | { |
461 | // Bu::println("%1\n-----").arg( r.aInt.toString() ); | 461 | Bu::println("%1\n-----").arg( r.aInt.toString() ); |
462 | Number sub( iScale, iRadix ); | 462 | Number sub( iScale, iRadix ); |
463 | for(;;) | 463 | for(;;) |
464 | { | 464 | { |
465 | // Bu::println(" -> Anchor: %1, Sample: %2").arg( iAnchor ).arg( iSample ); | 465 | Bu::println(" -> Anchor: %1, Sample: %2").arg( iAnchor ).arg( iSample ); |
466 | sub.aInt.set( r.aInt, iAnchor, iSample ); | 466 | sub.aInt.set( r.aInt, iAnchor, iSample ); |
467 | // Bu::println("%1\n%2\n----").arg( sub.toString() ).arg( rhs.toString() ); | 467 | Bu::println("%1\n%2\n----").arg( sub.toString() ).arg( rhs.toString() ); |
468 | if( sub < rhs ) | 468 | if( sub < rhs ) |
469 | { | 469 | { |
470 | iSample++; | 470 | iSample++; |
471 | iAnchor--; | 471 | iAnchor--; |
472 | if( iAnchor < 0 ) | 472 | if( iAnchor < 0 ) |
473 | { | 473 | { |
474 | // Bu::println("[Inner exit] Complete: q = %1, r = %2").arg( q.toString() ).arg( r.toString() ); | 474 | Bu::println("[Inner exit] Complete: q = %1, r = %2").arg( q.toString() ).arg( r.toString() ); |
475 | return; | 475 | return; |
476 | } | 476 | } |
477 | } | 477 | } |
@@ -479,29 +479,35 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const | |||
479 | break; | 479 | break; |
480 | } | 480 | } |
481 | 481 | ||
482 | Number x( iScale, iRadix ); | 482 | Number x( rhs.iScale, iRadix ); |
483 | int iRes = 0; | 483 | int iRes = -1; |
484 | for( ; x <= sub; iRes++, x = x + rhs ) { } | 484 | for( ; x <= sub; iRes++, x = x + rhs ) { } |
485 | x = sub - (x - rhs); | 485 | x = sub - (x - rhs); |
486 | if( !x.bPositive ) | ||
487 | { | ||
488 | iSample++; | ||
489 | iAnchor--; | ||
490 | continue; | ||
491 | } | ||
486 | for( int j = 0; iAnchor+j >= 0 && j < x.aInt.getSize(); j++ ) | 492 | for( int j = 0; iAnchor+j >= 0 && j < x.aInt.getSize(); j++ ) |
487 | r.aInt.set( iAnchor+j, x.aInt.get( j ) ); | 493 | r.aInt.set( iAnchor+j, x.aInt.get( j ) ); |
488 | while( r.aInt.getSize() > iAnchor+x.aInt.getSize() ) | 494 | while( r.aInt.getSize() > iAnchor+x.aInt.getSize() ) |
489 | r.aInt.remove(); | 495 | r.aInt.remove(); |
490 | // Bu::println(" -> Post remainder patch: %1 -> %2"). | 496 | Bu::println(" -> Post remainder patch: %1 -> %2"). |
491 | // arg( x.toString() ).arg( r.toString() ); | 497 | arg( x.toString() ).arg( r.toString() ); |
492 | 498 | ||
493 | // Bu::println("%1 (%2)").arg( iRes-1 ).arg( x.toString() ); | 499 | Bu::println("%1 (%2)").arg( iRes ).arg( x.toString() ); |
494 | while( q.aInt.getSize() <= iAnchor ) | 500 | while( q.aInt.getSize() <= iAnchor ) |
495 | q.aInt.append(0); | 501 | q.aInt.append(0); |
496 | q.aInt.set( iAnchor, iRes-1 ); | 502 | q.aInt.set( iAnchor, iRes ); |
497 | 503 | ||
498 | iSample = iMinWidth; | 504 | iSample = iMinWidth; |
499 | iAnchor = r.aInt.getSize()-iMinWidth; | 505 | iAnchor = r.aInt.getSize()-iMinWidth; |
500 | // Bu::println(" -> new Anchor: %1, Sample: %2").arg( iAnchor ). | 506 | Bu::println(" -> new Anchor: %1, Sample: %2").arg( iAnchor ). |
501 | // arg( iSample ); | 507 | arg( iSample ); |
502 | } while( iAnchor >= 0 ); | 508 | } while( iAnchor >= 0 ); |
503 | 509 | ||
504 | // Bu::println("Complete: q = %1, r = %2").arg( q.toString() ).arg( r.toString() ); | 510 | Bu::println("Complete: q = %1, r = %2").arg( q.toString() ).arg( r.toString() ); |
505 | } | 511 | } |
506 | 512 | ||
507 | Bu::Formatter &operator<<( Bu::Formatter &f, const Number &n ) | 513 | 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 ) | |||
142 | memcpy( aData, rSrc.aData, iSize*sizeof(Store) ); | 142 | memcpy( aData, rSrc.aData, iSize*sizeof(Store) ); |
143 | } | 143 | } |
144 | 144 | ||
145 | void PackedIntArray::copy( int iDest, const PackedIntArray &rSrc, int iStart, | ||
146 | int iSize ) | ||
147 | { | ||
148 | iCount = iSize; | ||
149 | checkCapacity(); | ||
150 | |||
151 | for( int j = 0; j < iSize; j++ ) | ||
152 | { | ||
153 | set( j+iDest, rSrc.get( iStart+j ) ); | ||
154 | } | ||
155 | } | ||
156 | |||
145 | void PackedIntArray::trim() | 157 | void PackedIntArray::trim() |
146 | { | 158 | { |
147 | while( iCount > 0 && get( iCount-1 ) == 0 ) | 159 | 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: | |||
21 | int getSize() const { return iCount; } | 21 | int getSize() const { return iCount; } |
22 | void set( const PackedIntArray &rSrc, int iStart, int iSize ); | 22 | void set( const PackedIntArray &rSrc, int iStart, int iSize ); |
23 | void set( const PackedIntArray &rSrc ); | 23 | void set( const PackedIntArray &rSrc ); |
24 | void copy( int iDest, const PackedIntArray &rSrc, int iStart, int iSize ); | ||
24 | void trim(); | 25 | void trim(); |
25 | int getBitWidth() const { return iBitWidth; } | 26 | int getBitWidth() const { return iBitWidth; } |
26 | 27 | ||