summaryrefslogtreecommitdiff
path: root/src/number.cpp
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2013-04-17 22:58:52 -0600
committerMike Buland <mike@xagasoft.com>2013-04-17 22:58:52 -0600
commit85219c48da0d3fff98a9d741e09d34e74abfec0b (patch)
tree39a5b8c13926dd2083c8552d701276678f6515c5 /src/number.cpp
parent55ef5612f8950bd042ffd6d001908845a4321f8e (diff)
downloadclic-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/number.cpp')
-rw-r--r--src/number.cpp34
1 files changed, 20 insertions, 14 deletions
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
507Bu::Formatter &operator<<( Bu::Formatter &f, const Number &n ) 513Bu::Formatter &operator<<( Bu::Formatter &f, const Number &n )