summaryrefslogtreecommitdiff
path: root/src/number.cpp
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2013-04-17 20:52:57 -0600
committerMike Buland <mike@xagasoft.com>2013-04-17 20:52:57 -0600
commit2bd38e44ba6eeab1f8ba7d25024a769c6df67452 (patch)
tree545a0f06155fbdafd42cb8e306b1634eabcfe47f /src/number.cpp
parent10559f103a72a36eda3e9649ffb229b1b39743c0 (diff)
downloadclic-2bd38e44ba6eeab1f8ba7d25024a769c6df67452.tar.gz
clic-2bd38e44ba6eeab1f8ba7d25024a769c6df67452.tar.bz2
clic-2bd38e44ba6eeab1f8ba7d25024a769c6df67452.tar.xz
clic-2bd38e44ba6eeab1f8ba7d25024a769c6df67452.zip
You can now add and subtract fractional numbers.
Diffstat (limited to 'src/number.cpp')
-rw-r--r--src/number.cpp53
1 files changed, 35 insertions, 18 deletions
diff --git a/src/number.cpp b/src/number.cpp
index 49e8a60..9fd3865 100644
--- a/src/number.cpp
+++ b/src/number.cpp
@@ -332,6 +332,13 @@ Bu::String Number::toString() const
332 332
333int Number::digit( int iIdx ) const 333int Number::digit( int iIdx ) const
334{ 334{
335 if( iIdx < 0 )
336 {
337 if( iIdx <= -iScale )
338 return 0;
339 else
340 return aFrac[-1-iIdx];
341 }
335 if( iIdx >= aInt.getSize() ) 342 if( iIdx >= aInt.getSize() )
336 return 0; 343 return 0;
337 return aInt[iIdx]; 344 return aInt[iIdx];
@@ -350,21 +357,26 @@ Number Number::add( const Number &rhs, bool bSub ) const
350 if( bPositive == (bSub?!rhs.bPositive:rhs.bPositive)) 357 if( bPositive == (bSub?!rhs.bPositive:rhs.bPositive))
351 { 358 {
352 ret.bPositive = bPositive; 359 ret.bPositive = bPositive;
353 for( int j = 0; j < iPlaces || iCarry > 0; j++ ) 360 for( int j = 1-iScale; j < iPlaces || iCarry > 0; j++ )
354 { 361 {
355 int iRes = iCarry + digit( j ) + rhs.digit( j ); 362 int iRes = iCarry + digit( j ) + rhs.digit( j );
356// Bu::println(" Place: %1 + %2 + (%3) = %4"). 363// Bu::println(" [%5] Place: %1 + %2 + (%3) = %4").
357// arg( digit(j) ).arg( rhs.digit( j ) ).arg( iCarry ) 364// arg( digit(j) ).arg( rhs.digit( j ) ).arg( iCarry )
358// .arg( iRes ); 365// .arg( iRes ).arg( j );
359 if( iRes == 0 ) 366 if( j < 0 )
367 ret.aFrac.set( -1-j, (iRes%iRadix) );
368 else
360 { 369 {
361 iZeros++; 370 if( iRes == 0 )
362 continue; 371 {
372 iZeros++;
373 continue;
374 }
375 for(; iZeros > 0; iZeros-- )
376 ret.aInt.append( 0 );
377
378 ret.aInt.append( (iRes%iRadix) );
363 } 379 }
364 for(; iZeros > 0; iZeros-- )
365 ret.aInt.append( 0 );
366
367 ret.aInt.append( (iRes%iRadix) );
368 if( iRes < iRadix ) 380 if( iRes < iRadix )
369 iCarry = 0; 381 iCarry = 0;
370 else 382 else
@@ -378,21 +390,26 @@ Number Number::add( const Number &rhs, bool bSub ) const
378 ret.bPositive = bPositive; 390 ret.bPositive = bPositive;
379 int iRes; 391 int iRes;
380 int iComp = (iRadix-1); 392 int iComp = (iRadix-1);
381 for( int j = 0; j < iPlaces; j++ ) 393 for( int j = 1-iScale; j < iPlaces; j++ )
382 { 394 {
383 iRes = digit( j ) + (iComp-rhs.digit( j )) + iCarry; 395 iRes = digit( j ) + (iComp-rhs.digit( j )) + iCarry;
384// Bu::println(" Place: %1 + %2 + (%3) = %4"). 396// Bu::println(" Place: %1 + %2 + (%3) = %4").
385// arg( digit(j) ).arg( 9-rhs.digit( j ) ).arg( iCarry ) 397// arg( digit(j) ).arg( 9-rhs.digit( j ) ).arg( iCarry )
386// .arg( iRes ); 398// .arg( iRes );
387 if( iRes == 0 ) 399 if( j < 0 )
400 ret.aFrac.set( -1-j, (iRes%iRadix) );
401 else
388 { 402 {
389 iZeros++; 403 if( iRes == 0 )
390 continue; 404 {
391 } 405 iZeros++;
392 for(; iZeros > 0; iZeros-- ) 406 continue;
393 ret.aInt.append( 0 ); 407 }
408 for(; iZeros > 0; iZeros-- )
409 ret.aInt.append( 0 );
394 410
395 ret.aInt.append( (iRes%iRadix) ); 411 ret.aInt.append( (iRes%iRadix) );
412 }
396 if( iRes < iRadix ) 413 if( iRes < iRadix )
397 iCarry = 0; 414 iCarry = 0;
398 else 415 else