summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/number.cpp44
1 files changed, 36 insertions, 8 deletions
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
58 58
59 int iCarry = 0; 59 int iCarry = 0;
60 int iZeros = 0; 60 int iZeros = 0;
61 for( int j = 1-iScale-rhs.iScale; j < iCnt || iCarry > 0; j++ ) 61 for( int j = -iScale-rhs.iScale; j < iCnt || iCarry > 0; j++ )
62 { 62 {
63// Bu::println("Pos %1").arg(j); 63// Bu::println("Pos %1").arg(j);
64 int iPos = iCarry; 64 int iPos = iCarry;
@@ -363,7 +363,7 @@ Number Number::add( const Number &rhs, bool bSub ) const
363 if( bPositive == (bSub?!rhs.bPositive:rhs.bPositive)) 363 if( bPositive == (bSub?!rhs.bPositive:rhs.bPositive))
364 { 364 {
365 ret.bPositive = bPositive; 365 ret.bPositive = bPositive;
366 for( int j = 1-iScale; j < iPlaces || iCarry > 0; j++ ) 366 for( int j = -iScale; j < iPlaces || iCarry > 0; j++ )
367 { 367 {
368 int iRes = iCarry + digit( j ) + rhs.digit( j ); 368 int iRes = iCarry + digit( j ) + rhs.digit( j );
369// Bu::println(" [%5] Place: %1 + %2 + (%3) = %4"). 369// Bu::println(" [%5] Place: %1 + %2 + (%3) = %4").
@@ -396,7 +396,7 @@ Number Number::add( const Number &rhs, bool bSub ) const
396 ret.bPositive = bPositive; 396 ret.bPositive = bPositive;
397 int iRes; 397 int iRes;
398 int iComp = (iRadix-1); 398 int iComp = (iRadix-1);
399 for( int j = 1-iScale; j < iPlaces; j++ ) 399 for( int j = -iScale; j < iPlaces; j++ )
400 { 400 {
401 iRes = digit( j ) + (iComp-rhs.digit( j )) + iCarry; 401 iRes = digit( j ) + (iComp-rhs.digit( j )) + iCarry;
402// Bu::println(" Place: %1 + %2 + (%3) = %4"). 402// Bu::println(" Place: %1 + %2 + (%3) = %4").
@@ -441,11 +441,35 @@ Number Number::add( const Number &rhs, bool bSub ) const
441 441
442void Number::divide( const Number &rhs, Number &q, Number &r ) const 442void Number::divide( const Number &rhs, Number &q, Number &r ) const
443{ 443{
444 q.aInt.clear(); 444 if( rhs.iScale > 0 )
445 r = *this; 445 {
446 446 Number newrhs = Number( 0, iRadix );
447 if( rhs.aInt.getSize() > aInt.getSize() ) 447 Number newbase = Number( iScale, iRadix );
448 int iOff;
449 for( iOff = -iScale; iOff < 0 && rhs.digit( iOff ) == 0; iOff++ ) { }
450 for( int j = iOff; j < rhs.aInt.getSize(); j++ )
451 {
452 newrhs.aInt.append( rhs.digit( j ) );
453 newbase.aInt.append( digit( j ) );
454 }
455 for( int j = 0; j < aInt.getSize(); j++ )
456 {
457 newbase.aInt.append( aInt.get( j ) );
458 }
459 for( int j = iScale+iOff; j >= -iOff; j-- )
460 {
461 newbase.aFrac.set( j+iOff, aFrac.get( j ) );
462 }
463
464 Bu::println("Conv %1 => %2 (iOff = %3)").arg( rhs ).arg( newrhs ).arg( iOff );
465 Bu::println("Conv %1 => %2 (iOff = %3)").arg( *this ).arg( newbase ).arg( iOff );
466
467 newbase.divide( newrhs, q, r );
448 return; 468 return;
469 }
470
471 q = Number( iScale, iRadix );
472 r = *this;
449 473
450 if( bPositive == rhs.bPositive ) 474 if( bPositive == rhs.bPositive )
451 q.bPositive = true; 475 q.bPositive = true;
@@ -481,12 +505,16 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const
481 505
482 Number x( rhs.iScale, iRadix ); 506 Number x( rhs.iScale, iRadix );
483 int iRes = -1; 507 int iRes = -1;
484 for( ; x <= sub; iRes++, x = x + rhs ) { } 508 Bu::println("{x = %1, sub=%2, rhs=%4, iRes=%3}").arg( x ).arg(sub).arg(iRes).arg(rhs);
509 for( ; x <= sub; iRes++, x = x + rhs ) {
510 Bu::println("{x = %1, sub=%2, rhs=%4, iRes=%3, x+rhs=%5}").arg( x ).arg(sub).arg(iRes).arg(rhs).arg(x+rhs);
511 }
485 x = sub - (x - rhs); 512 x = sub - (x - rhs);
486 if( !x.bPositive ) 513 if( !x.bPositive )
487 { 514 {
488 iSample++; 515 iSample++;
489 iAnchor--; 516 iAnchor--;
517 Bu::println("What? it's negative? %1").arg( x );
490 continue; 518 continue;
491 } 519 }
492 for( int j = 0; iAnchor+j >= 0 && j < x.aInt.getSize(); j++ ) 520 for( int j = 0; iAnchor+j >= 0 && j < x.aInt.getSize(); j++ )