summaryrefslogtreecommitdiff
path: root/src/number.cpp
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2013-04-16 15:35:36 -0600
committerMike Buland <mike@xagasoft.com>2013-04-16 15:35:36 -0600
commit439c5a36138a66b0c76ec3136c79a86ca3619f17 (patch)
treed4f6cfbe9e9f880a5171e3964a658dc2f8968841 /src/number.cpp
parent800af3a90062284ef2a0b21d191a531a8e3ab30b (diff)
downloadclic-439c5a36138a66b0c76ec3136c79a86ca3619f17.tar.gz
clic-439c5a36138a66b0c76ec3136c79a86ca3619f17.tar.bz2
clic-439c5a36138a66b0c76ec3136c79a86ca3619f17.tar.xz
clic-439c5a36138a66b0c76ec3136c79a86ca3619f17.zip
Broke the string parser.
Diffstat (limited to 'src/number.cpp')
-rw-r--r--src/number.cpp45
1 files changed, 28 insertions, 17 deletions
diff --git a/src/number.cpp b/src/number.cpp
index 5ef8009..c7a4cc6 100644
--- a/src/number.cpp
+++ b/src/number.cpp
@@ -5,19 +5,21 @@
5 5
6#define RadixToBits( x ) ((int)(log((x))*0xb.8aa3b295c17fp-3+1.0)) 6#define RadixToBits( x ) ((int)(log((x))*0xb.8aa3b295c17fp-3+1.0))
7 7
8Number::Number( int iRadix, int iScale ) : 8Number::Number( int iScale, int iRadix ) :
9 iRadix( iRadix ), 9 iRadix( iRadix ),
10 iScale( iScale ), 10 iScale( iScale ),
11 bPositive( true ), 11 bPositive( true ),
12 aInt( RadixToBits(iRadix) ) 12 aInt( RadixToBits(iRadix) ),
13 aFrac( RadixToBits(iRadix), iScale )
13{ 14{
14} 15}
15 16
16Number::Number( const Bu::String &sData, int iRadix, int iScale ) : 17Number::Number( const Bu::String &sData, int iScale, int iRadix ) :
17 iRadix( iRadix ), 18 iRadix( iRadix ),
18 iScale( iScale ), 19 iScale( iScale ),
19 bPositive( true ), 20 bPositive( true ),
20 aInt( RadixToBits( iRadix ) ) 21 aInt( RadixToBits( iRadix ) ),
22 aFrac( RadixToBits(iRadix), iScale )
21{ 23{
22 set( sData ); 24 set( sData );
23} 25}
@@ -50,7 +52,7 @@ Number Number::operator-( const Number &rhs ) const
50 52
51Number Number::operator*( const Number &rhs ) const 53Number Number::operator*( const Number &rhs ) const
52{ 54{
53 Number ret( iRadix, iScale ); 55 Number ret( iScale, iRadix );
54 56
55 int iCnt = aInt.getSize()+rhs.aInt.getSize(); 57 int iCnt = aInt.getSize()+rhs.aInt.getSize();
56 58
@@ -94,14 +96,14 @@ Number Number::operator*( const Number &rhs ) const
94 96
95Number Number::operator/( const Number &rhs ) const 97Number Number::operator/( const Number &rhs ) const
96{ 98{
97 Number q( iRadix, iScale ), r( iRadix, iScale ); 99 Number q( iScale, iRadix ), r( iScale, iRadix );
98 divide( rhs, q, r ); 100 divide( rhs, q, r );
99 return q; 101 return q;
100} 102}
101 103
102Number Number::operator%( const Number &rhs ) const 104Number Number::operator%( const Number &rhs ) const
103{ 105{
104 Number q( iRadix, iScale ), r( iRadix, iScale ); 106 Number q( iScale, iRadix ), r( iScale, iRadix );
105 divide( rhs, q, r ); 107 divide( rhs, q, r );
106 return r; 108 return r;
107} 109}
@@ -238,20 +240,29 @@ void Number::set( const Bu::String &sNum )
238{ 240{
239 aInt.clear(); 241 aInt.clear();
240 bPositive = true; 242 bPositive = true;
241 for( int j = sNum.getSize()-1; j >= 0; j-- ) 243 int j = sNum.getSize()-1;
244
245 if( sNum[j] == '+' )
246 j--;
247 else if( sNum[j] == '-' )
242 { 248 {
243 if( sNum[j] == '+' ) 249 bPositive = false;
244 break; 250 j--;
245 if( sNum[j] == '-' ) 251 }
246 { 252 for( ; sNum[j] == '0' && j >= 0; j-- ) { }
247 bPositive = false; 253
254 for( ; j >= 0; j-- )
255 {
256 Bu::println(" -> '%1'").arg( sNum[j] );
257 if( sNum[j] == '.' )
248 break; 258 break;
249 }
250 if( sNum[j] >= '0' && sNum[j] <= '9' ) 259 if( sNum[j] >= '0' && sNum[j] <= '9' )
251 aInt.append( sNum[j]-'0' ); 260 aInt.append( sNum[j]-'0' );
252 else 261 else
253 aInt.append( sNum[j]-'a'+10 ); 262 aInt.append( sNum[j]-'a'+10 );
254 } 263 }
264
265 Bu::println("done.");
255} 266}
256 267
257void Number::set( const Number &sNum ) 268void Number::set( const Number &sNum )
@@ -289,7 +300,7 @@ int Number::digit( int iIdx ) const
289 300
290Number Number::add( const Number &rhs, bool bSub ) const 301Number Number::add( const Number &rhs, bool bSub ) const
291{ 302{
292 Number ret( iRadix, iScale ); 303 Number ret( iScale, iRadix );
293 304
294 int iPlaces = Bu::buMax(rhs.aInt.getSize(), aInt.getSize() ); 305 int iPlaces = Bu::buMax(rhs.aInt.getSize(), aInt.getSize() );
295 306
@@ -386,7 +397,7 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const
386 do 397 do
387 { 398 {
388// Bu::println("%1\n-----").arg( r.aInt.toString() ); 399// Bu::println("%1\n-----").arg( r.aInt.toString() );
389 Number sub( iRadix, iScale ); 400 Number sub( iScale, iRadix );
390 for(;;) 401 for(;;)
391 { 402 {
392// Bu::println(" -> Anchor: %1, Sample: %2").arg( iAnchor ).arg( iSample ); 403// Bu::println(" -> Anchor: %1, Sample: %2").arg( iAnchor ).arg( iSample );
@@ -406,7 +417,7 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const
406 break; 417 break;
407 } 418 }
408 419
409 Number x( iRadix, iScale ); 420 Number x( iScale, iRadix );
410 int iRes = 0; 421 int iRes = 0;
411 for( ; x <= sub; iRes++, x = x + rhs ) { } 422 for( ; x <= sub; iRes++, x = x + rhs ) { }
412 x = sub - (x - rhs); 423 x = sub - (x - rhs);