diff options
Diffstat (limited to 'src/number.cpp')
-rw-r--r-- | src/number.cpp | 63 |
1 files changed, 42 insertions, 21 deletions
diff --git a/src/number.cpp b/src/number.cpp index 05e310c..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 | ||
613 | void 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 | |||
613 | void Number::setScale( int iNewScale ) | 626 | void Number::setScale( int iNewScale ) |
614 | { | 627 | { |
615 | if( iNewScale == 0 ) | 628 | if( iNewScale == 0 ) |
@@ -662,11 +675,11 @@ Number Number::toRadix( int iNewRadix, int iNewScale ) const | |||
662 | n.set( iNewRadix ); | 675 | n.set( iNewRadix ); |
663 | while( !me.isZero() ) | 676 | while( !me.isZero() ) |
664 | { | 677 | { |
665 | Bu::println("%1 %% %2 = %3").arg( me ).arg( n ).arg( me%n ); | 678 | // Bu::println("%1 %% %2 = %3").arg( me ).arg( n ).arg( me%n ); |
666 | int dig = (me%n).toInt32(); | 679 | int dig = (me%n).toInt32(); |
667 | ret.aInt.append( dig ); | 680 | ret.aInt.append( dig ); |
668 | me = me / n; | 681 | me = me / n; |
669 | Bu::println("New digit: %1 (%2)").arg( dig ).arg( me ); | 682 | // Bu::println("New digit: %1 (%2)").arg( dig ).arg( me ); |
670 | } | 683 | } |
671 | 684 | ||
672 | return ret; | 685 | return ret; |
@@ -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; |