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/number.cpp | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) (limited to 'src/number.cpp') 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 ) -- cgit v1.2.3