summaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/main.cpp15
-rw-r--r--src/number.cpp45
-rw-r--r--src/number.h5
3 files changed, 43 insertions, 22 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 4454c08..0e5a77d 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -259,7 +259,7 @@ void ordertest()
259 259
260void radixtest() 260void radixtest()
261{ 261{
262 Number a( 16 ), b( 16 ); 262 Number a( 0, 16 ), b( 0, 16 );
263 263
264 a = "f8a72bce3"; 264 a = "f8a72bce3";
265 b = "9ea8cb3"; 265 b = "9ea8cb3";
@@ -276,6 +276,15 @@ void radixtest()
276 println("%1 * %2 = %3").arg( a ).arg( b ).arg( (a * b) ); 276 println("%1 * %2 = %3").arg( a ).arg( b ).arg( (a * b) );
277} 277}
278 278
279void fractest()
280{
281 Number a( 8 ), b( 8 );
282
283 a = "123.456";
284 b = "0.987";
285 println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b );
286}
287
279int main( int , char *[] ) 288int main( int , char *[] )
280{ 289{
281 println("CliC"); 290 println("CliC");
@@ -283,8 +292,8 @@ int main( int , char *[] )
283// packedtest1(); 292// packedtest1();
284// numbertest1(); 293// numbertest1();
285// numbertestcomp(); 294// numbertestcomp();
286 radixtest(); 295// radixtest();
287 296 fractest();
288// ordertest(); 297// ordertest();
289 298
290 return 0; 299 return 0;
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);
diff --git a/src/number.h b/src/number.h
index deac5eb..c16dd55 100644
--- a/src/number.h
+++ b/src/number.h
@@ -7,8 +7,8 @@
7class Number 7class Number
8{ 8{
9public: 9public:
10 Number( int iRadix=10, int iScale=0 ); 10 Number( int iScale=0, int iRadix=10 );
11 Number( const Bu::String &sData, int iRadix=10, int iScale=0 ); 11 Number( const Bu::String &sData, int iScale=0, int iRadix=10 );
12 virtual ~Number(); 12 virtual ~Number();
13 13
14 Number &operator=( const Bu::String &sNum ); 14 Number &operator=( const Bu::String &sNum );
@@ -50,6 +50,7 @@ private:
50 int iScale; 50 int iScale;
51 bool bPositive; 51 bool bPositive;
52 PackedIntArray aInt; 52 PackedIntArray aInt;
53 PackedIntArray aFrac;
53}; 54};
54 55
55Bu::Formatter &operator<<( Bu::Formatter &f, const Number &n ); 56Bu::Formatter &operator<<( Bu::Formatter &f, const Number &n );