From 10559f103a72a36eda3e9649ffb229b1b39743c0 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 17 Apr 2013 20:36:32 -0600 Subject: Fractional portions parse now. --- src/main.cpp | 1 + src/number.cpp | 33 +++++++++++++++++++++------------ src/packedintarray.cpp | 7 ++++--- src/packedintarray.h | 1 + 4 files changed, 27 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 0e5a77d..1499285 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -281,6 +281,7 @@ void fractest() Number a( 8 ), b( 8 ); a = "123.456"; + println("%1").arg( a ); b = "0.987"; println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); } diff --git a/src/number.cpp b/src/number.cpp index 8ebe870..49e8a60 100644 --- a/src/number.cpp +++ b/src/number.cpp @@ -9,8 +9,8 @@ Number::Number( int iScale, int iRadix ) : iRadix( iRadix ), iScale( iScale ), bPositive( true ), - aInt( RadixToBits(iRadix) ), - aFrac( RadixToBits(iRadix), iScale ) + aInt( RadixToBits( iRadix ) ), + aFrac( aInt.getBitWidth(), iScale ) { } @@ -19,7 +19,7 @@ Number::Number( const Bu::String &sData, int iScale, int iRadix ) : iScale( iScale ), bPositive( true ), aInt( RadixToBits( iRadix ) ), - aFrac( RadixToBits(iRadix), iScale ) + aFrac( aInt.getBitWidth(), iScale ) { set( sData ); } @@ -286,29 +286,38 @@ void Number::set( const Bu::String &sNum ) void Number::set( const Number &sNum ) { aInt.set( sNum.aInt ); + aFrac.set( sNum.aFrac ); bPositive = sNum.bPositive; iScale = sNum.iScale; + iRadix = sNum.iRadix; } Bu::String Number::toString() const { - if( aInt.getSize() == 0 ) + int iSigDig = iScale-1; + for( ; iSigDig >= 0 && aFrac.get( iSigDig ) == 0; iSigDig-- ) { } + if( aInt.getSize() == 0 && iSigDig <= 0 ) return "0"; Bu::String sRet; if( !bPositive ) sRet += '-'; - for( int j = aInt.getSize()-1; j >= 0; j-- ) + if( aInt.getSize() > 0 ) { - int x = aInt.get( j ); - if( x >= 10 ) - sRet += x-10+'a'; - else - sRet += x+'0'; + for( int j = aInt.getSize()-1; j >= 0; j-- ) + { + int x = aInt.get( j ); + if( x >= 10 ) + sRet += x-10+'a'; + else + sRet += x+'0'; + } } - if( iScale > 0 ) + else + sRet += "0"; + if( iSigDig >= 0 ) { sRet += '.'; - for( int j = 0; j < iScale; j++ ) + for( int j = 0; j <= iSigDig; j++ ) { int x = aFrac.get( j ); if( x >= 10 ) diff --git a/src/packedintarray.cpp b/src/packedintarray.cpp index 0e137bf..badc829 100644 --- a/src/packedintarray.cpp +++ b/src/packedintarray.cpp @@ -31,12 +31,13 @@ PackedIntArray::PackedIntArray( PackedIntArray::Unit iBitWidth ) : PackedIntArray::PackedIntArray( PackedIntArray::Unit iBitWidth, int iCount ): iBitWidth( iBitWidth ), aData( NULL ), - iCapacity( StoreCount(iCount) ), + iCapacity( bitsizeof(StoreCount(iCount))/iBitWidth ), iCount( iCount ), uMask( 0 ) { - aData = new Store[StoreCount(iCapacity)]; - memset( aData, 0, StoreCount(iCapacity)); + int iSize = StoreCount(iCapacity); + aData = new Store[iSize]; + memset( aData, 0, iSize*sizeof(Store)); for( int j = 0; j < iBitWidth; j++ ) uMask |= (1<