summaryrefslogtreecommitdiff
path: root/src/number.cpp
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2015-01-29 08:49:55 -0700
committerMike Buland <mike@xagasoft.com>2015-01-29 08:49:55 -0700
commitf03026f5c2cde1eecfda273eaa52df10287f0f9e (patch)
treec3b5398bb29b848fc5fca2a87633961150d41cad /src/number.cpp
parente64cb84fe43660818f7d6eff2954147495b1ac8b (diff)
downloadclic-f03026f5c2cde1eecfda273eaa52df10287f0f9e.tar.gz
clic-f03026f5c2cde1eecfda273eaa52df10287f0f9e.tar.bz2
clic-f03026f5c2cde1eecfda273eaa52df10287f0f9e.tar.xz
clic-f03026f5c2cde1eecfda273eaa52df10287f0f9e.zip
Fixed nasty subtraction bug when dealing with fractions.0.13
It turned out to be a really simple solution, but man, that was embarassing. I forgot to include the fractional portion of a number when fixing my radix+1 compliment numbers.
Diffstat (limited to '')
-rw-r--r--src/number.cpp59
1 files changed, 40 insertions, 19 deletions
diff --git a/src/number.cpp b/src/number.cpp
index eb803be..660ba61 100644
--- a/src/number.cpp
+++ b/src/number.cpp
@@ -610,6 +610,19 @@ int Number::digit( int iIdx ) const
610 return aInt[iIdx]; 610 return aInt[iIdx];
611} 611}
612 612
613void Number::setDigit( int iIdx, int iVal )
614{
615 if( iIdx < 0 )
616 {
617 if( iIdx >= -iScale )
618 aFrac.set( -1-iIdx, iVal );
619 return;
620 }
621 if( iIdx >= aInt.getSize() )
622 return;
623 aInt.set( iIdx, iVal );
624}
625
613void Number::setScale( int iNewScale ) 626void Number::setScale( int iNewScale )
614{ 627{
615 if( iNewScale == 0 ) 628 if( iNewScale == 0 )
@@ -680,7 +693,7 @@ Number Number::add( const Number &rhs, bool bSub ) const
680 693
681 int iZeros = 0; 694 int iZeros = 0;
682 int iCarry = 0; 695 int iCarry = 0;
683// Bu::println("::: %1 + %2:").arg( toString() ).arg( rhs.toString() ); 696 DBS( ADD, Bu::println("::: %1 + %2:").arg( toString() ).arg( rhs.toString() ) );
684 697
685 if( bPositive == (bSub?!rhs.bPositive:rhs.bPositive)) 698 if( bPositive == (bSub?!rhs.bPositive:rhs.bPositive))
686 { 699 {
@@ -688,9 +701,10 @@ Number Number::add( const Number &rhs, bool bSub ) const
688 for( int j = -iScale; j < iPlaces || iCarry > 0; j++ ) 701 for( int j = -iScale; j < iPlaces || iCarry > 0; j++ )
689 { 702 {
690 int iRes = iCarry + digit( j ) + rhs.digit( j ); 703 int iRes = iCarry + digit( j ) + rhs.digit( j );
691// Bu::println(" [%5] Place: %1 + %2 + (%3) = %4"). 704 DBS( ADD,
692// arg( digit(j) ).arg( rhs.digit( j ) ).arg( iCarry ) 705 Bu::println(" [%5] Place: %1 + %2 + (%3) = %4").
693// .arg( iRes ).arg( j ); 706 arg( digit(j) ).arg( rhs.digit( j ) ).arg( iCarry )
707 .arg( iRes ).arg( j ) );
694 if( j < 0 ) 708 if( j < 0 )
695 ret.aFrac.set( -1-j, (iRes%iRadix) ); 709 ret.aFrac.set( -1-j, (iRes%iRadix) );
696 else 710 else
@@ -714,16 +728,25 @@ Number Number::add( const Number &rhs, bool bSub ) const
714 else 728 else
715 { 729 {
716 iCarry = 1; 730 iCarry = 1;
717// Bu::println("--method b--"); 731 DBS( ADD, Bu::println("--method b--") );
718 ret.bPositive = bPositive; 732 ret.bPositive = bPositive;
719 int iRes; 733 int iRes;
720 int iComp = (iRadix-1); 734 int iComp = (iRadix-1);
735
721 for( int j = -iScale; j < iPlaces; j++ ) 736 for( int j = -iScale; j < iPlaces; j++ )
722 { 737 {
723 iRes = digit( j ) + (iComp-rhs.digit( j )) + iCarry; 738 iRes = digit( j ) + (iComp-rhs.digit( j )) + iCarry;
724// Bu::println(" Place: %1 + %2 + (%3) = %4"). 739 if( iRes < iRadix )
725// arg( digit(j) ).arg( 9-rhs.digit( j ) ).arg( iCarry ) 740 {
726// .arg( iRes ); 741 iCarry = 0;
742 }
743 else
744 iCarry = iRes/iRadix;
745
746 DBS( ADD,
747 Bu::println(" Place: %1 + %2 + (%3) = %4").
748 arg( digit(j) ).arg( iComp-rhs.digit( j ) ).arg( iCarry )
749 .arg( iRes ) );
727 if( j < 0 ) 750 if( j < 0 )
728 ret.aFrac.set( -1-j, (iRes%iRadix) ); 751 ret.aFrac.set( -1-j, (iRes%iRadix) );
729 else 752 else
@@ -738,24 +761,22 @@ Number Number::add( const Number &rhs, bool bSub ) const
738 761
739 ret.aInt.append( (iRes%iRadix) ); 762 ret.aInt.append( (iRes%iRadix) );
740 } 763 }
741 if( iRes < iRadix )
742 iCarry = 0;
743 else
744 iCarry = iRes/iRadix;
745 } 764 }
746 if( iCarry == 0 ) 765 if( iCarry == 0 )
747 { 766 {
748 ret.bPositive = false; 767 ret.bPositive = false;
749 ret.aInt.set( 0, iComp-ret.aInt.get( 0 )+1); 768 int iVal = iComp-ret.digit( -ret.iScale )+1;
750 for( int j = 1; j < ret.aInt.getSize(); j++ ) 769 iCarry = (iVal >= iRadix) ? 1 : 0;
770 ret.setDigit( -ret.iScale, iVal%iRadix );
771
772 for( int j = -iScale+1; j < ret.aInt.getSize(); j++ )
751 { 773 {
752 ret.aInt.set( j, iComp-ret.aInt.get( j )); 774 iVal = iComp-ret.digit( j )+iCarry;
775 iCarry = (iVal >= iRadix) ? 1 : 0;
776 ret.setDigit( j, iVal%iRadix );
753 } 777 }
754 } 778 }
755 else 779 ret.aInt.trim();
756 {
757 ret.aInt.trim();
758 }
759 } 780 }
760 781
761 return ret; 782 return ret;