diff options
author | Mike Buland <mike@xagasoft.com> | 2013-04-17 21:11:27 -0600 |
---|---|---|
committer | Mike Buland <mike@xagasoft.com> | 2013-04-17 21:11:27 -0600 |
commit | 55ef5612f8950bd042ffd6d001908845a4321f8e (patch) | |
tree | 8c1738928c542d74bfbcd7cc1c4949c8b92ffae7 /src | |
parent | 2bd38e44ba6eeab1f8ba7d25024a769c6df67452 (diff) | |
download | clic-55ef5612f8950bd042ffd6d001908845a4321f8e.tar.gz clic-55ef5612f8950bd042ffd6d001908845a4321f8e.tar.bz2 clic-55ef5612f8950bd042ffd6d001908845a4321f8e.tar.xz clic-55ef5612f8950bd042ffd6d001908845a4321f8e.zip |
Now you can multiply fractional numbers.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 1 | ||||
-rw-r--r-- | src/number.cpp | 26 | ||||
-rw-r--r-- | src/packedintarray.cpp | 2 |
3 files changed, 18 insertions, 11 deletions
diff --git a/src/main.cpp b/src/main.cpp index 061ebbb..23388ab 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -285,6 +285,7 @@ void fractest() | |||
285 | b = "0.987"; | 285 | b = "0.987"; |
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 | } | 289 | } |
289 | 290 | ||
290 | int main( int , char *[] ) | 291 | int main( int , char *[] ) |
diff --git a/src/number.cpp b/src/number.cpp index 9fd3865..2d45e7e 100644 --- a/src/number.cpp +++ b/src/number.cpp | |||
@@ -52,21 +52,21 @@ Number Number::operator-( const Number &rhs ) const | |||
52 | 52 | ||
53 | Number Number::operator*( const Number &rhs ) const | 53 | Number Number::operator*( const Number &rhs ) const |
54 | { | 54 | { |
55 | Number ret( iScale, iRadix ); | 55 | Number ret( iScale+rhs.iScale,iRadix ); |
56 | 56 | ||
57 | int iCnt = aInt.getSize()+rhs.aInt.getSize(); | 57 | int iCnt = aInt.getSize()+rhs.aInt.getSize(); |
58 | 58 | ||
59 | int iCarry = 0; | 59 | int iCarry = 0; |
60 | int iZeros = 0; | 60 | int iZeros = 0; |
61 | for( int j = 0; j < iCnt || iCarry > 0; j++ ) | 61 | for( int j = 1-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; |
65 | iCarry = 0; | 65 | iCarry = 0; |
66 | for( int k = 0; k < rhs.aInt.getSize(); k++ ) | 66 | for( int k = 1-rhs.iScale; k < rhs.aInt.getSize(); k++ ) |
67 | { | 67 | { |
68 | if( j-k < 0 ) | 68 | // if( j-k < 0 ) |
69 | break; | 69 | // break; |
70 | 70 | ||
71 | int iRes = digit(j-k)*rhs.digit(k); | 71 | int iRes = digit(j-k)*rhs.digit(k); |
72 | iPos += iRes; | 72 | iPos += iRes; |
@@ -75,13 +75,19 @@ Number Number::operator*( const Number &rhs ) const | |||
75 | // arg( j-k ).arg( digit(j-k) ).arg( k ).arg( rhs.digit(k) ). | 75 | // arg( j-k ).arg( digit(j-k) ).arg( k ).arg( rhs.digit(k) ). |
76 | // arg( iRes ).arg( iPos ).arg( iCarry ); | 76 | // arg( iRes ).arg( iPos ).arg( iCarry ); |
77 | } | 77 | } |
78 | if( (iPos%iRadix) == 0 ) | 78 | // Bu::println("=> %1 = %2 (%3 / %4)").arg( j ).arg( iPos ).arg( iPos%iRadix ).arg( iPos/iRadix ); |
79 | iZeros++; | 79 | if( j < 0 ) |
80 | ret.aFrac.set( -1-j, (iPos%iRadix) ); | ||
80 | else | 81 | else |
81 | { | 82 | { |
82 | for(; iZeros > 0; iZeros-- ) | 83 | if( (iPos%iRadix) == 0 ) |
83 | ret.aInt.append( 0 ); | 84 | iZeros++; |
84 | ret.aInt.append( iPos%iRadix ); | 85 | else |
86 | { | ||
87 | for(; iZeros > 0; iZeros-- ) | ||
88 | ret.aInt.append( 0 ); | ||
89 | ret.aInt.append( iPos%iRadix ); | ||
90 | } | ||
85 | } | 91 | } |
86 | iCarry += iPos/iRadix; | 92 | iCarry += iPos/iRadix; |
87 | } | 93 | } |
diff --git a/src/packedintarray.cpp b/src/packedintarray.cpp index badc829..d395aa2 100644 --- a/src/packedintarray.cpp +++ b/src/packedintarray.cpp | |||
@@ -31,7 +31,7 @@ PackedIntArray::PackedIntArray( PackedIntArray::Unit iBitWidth ) : | |||
31 | PackedIntArray::PackedIntArray( PackedIntArray::Unit iBitWidth, int iCount ): | 31 | PackedIntArray::PackedIntArray( PackedIntArray::Unit iBitWidth, int iCount ): |
32 | iBitWidth( iBitWidth ), | 32 | iBitWidth( iBitWidth ), |
33 | aData( NULL ), | 33 | aData( NULL ), |
34 | iCapacity( bitsizeof(StoreCount(iCount))/iBitWidth ), | 34 | iCapacity( (StoreCount(iCount)*StoreBits)/iBitWidth ), |
35 | iCount( iCount ), | 35 | iCount( iCount ), |
36 | uMask( 0 ) | 36 | uMask( 0 ) |
37 | { | 37 | { |