summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/main.cpp1
-rw-r--r--src/number.cpp53
2 files changed, 36 insertions, 18 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 1499285..061ebbb 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -284,6 +284,7 @@ void fractest()
284 println("%1").arg( a ); 284 println("%1").arg( a );
285 b = "0.987"; 285 b = "0.987";
286 println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); 286 println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b );
287 println("%1 - %2 = %3").arg( a ).arg( b ).arg( a - b );
287} 288}
288 289
289int main( int , char *[] ) 290int main( int , char *[] )
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