diff options
| author | Mike Buland <mike@xagasoft.com> | 2014-10-30 14:28:40 -0600 |
|---|---|---|
| committer | Mike Buland <mike@xagasoft.com> | 2014-10-30 14:28:40 -0600 |
| commit | ab6a53d64a2afcc4dd106753548387cd9f417531 (patch) | |
| tree | d84ff2c92342bd2b6abf335557bc7af4340a6fa8 | |
| parent | 355384af24bfdb6e1346a83011daf5b5381aa2eb (diff) | |
| download | clic-ab6a53d64a2afcc4dd106753548387cd9f417531.tar.gz clic-ab6a53d64a2afcc4dd106753548387cd9f417531.tar.bz2 clic-ab6a53d64a2afcc4dd106753548387cd9f417531.tar.xz clic-ab6a53d64a2afcc4dd106753548387cd9f417531.zip | |
Fixed remaining known division issues.
It looks great, and works with everything I can think of so far.
| -rw-r--r-- | src/number.cpp | 43 |
1 files changed, 26 insertions, 17 deletions
diff --git a/src/number.cpp b/src/number.cpp index be9b8be..81aefbe 100644 --- a/src/number.cpp +++ b/src/number.cpp | |||
| @@ -439,7 +439,7 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const | |||
| 439 | nNum.aInt.append( digit( j ) ); | 439 | nNum.aInt.append( digit( j ) ); |
| 440 | } | 440 | } |
| 441 | // Bu::println("New numerator: %1").arg( nNum ); | 441 | // Bu::println("New numerator: %1").arg( nNum ); |
| 442 | while( iAnchor > -iScale && !nNum.isZero()) // division loop | 442 | while( iAnchor > -iScale && (!nNum.isZero() || iAnchor > 0)) // division loop |
| 443 | { | 443 | { |
| 444 | // As long as the numerator is smaller than the divisor we keep | 444 | // As long as the numerator is smaller than the divisor we keep |
| 445 | // including new digits. | 445 | // including new digits. |
| @@ -449,10 +449,10 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const | |||
| 449 | nNum.aInt.insert( 0, digit(iAnchor-iNumShift) ); | 449 | nNum.aInt.insert( 0, digit(iAnchor-iNumShift) ); |
| 450 | // Bu::println("Numerator too small, new numerator: %1").arg( nNum ); | 450 | // Bu::println("Numerator too small, new numerator: %1").arg( nNum ); |
| 451 | } | 451 | } |
| 452 | if( iAnchor < -iScale ) | ||
| 453 | break; | ||
| 454 | 452 | ||
| 455 | // Bu::println(" New anchor: %1").arg( iAnchor ); | 453 | // Bu::println(" New anchor: %1 vs iScale = %2").arg( iAnchor ).arg( iScale ); |
| 454 | if( iAnchor < 0 && -1-iAnchor >= iScale ) | ||
| 455 | break; | ||
| 456 | 456 | ||
| 457 | // How many times does the divisor fit into the numerator | 457 | // How many times does the divisor fit into the numerator |
| 458 | int iCount = 0; | 458 | int iCount = 0; |
| @@ -477,23 +477,32 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const | |||
| 477 | q.aFrac.set( -1-iAnchor, iCount ); | 477 | q.aFrac.set( -1-iAnchor, iCount ); |
| 478 | } | 478 | } |
| 479 | } | 479 | } |
| 480 | for( int j = -iAnchor; j < nNum.aInt.getSize(); j++ ) | 480 | // if( iAnchor <= 0 ) |
| 481 | r.aInt.append( nNum.aInt[j] ); | ||
| 482 | int jtop = 0; | ||
| 483 | for(; nNum.aInt[jtop] == 0; jtop++ ) { } | ||
| 484 | // Bu::println("Computing remainder fraction from %1 to %2") | ||
| 485 | // .arg( -iAnchor-1 ).arg( jtop ); | ||
| 486 | r.aFrac.clear(); | ||
| 487 | for( int j = -iAnchor-1, k = 0; j >= jtop && k < r.iScale; j--, k++ ) | ||
| 488 | { | 481 | { |
| 489 | if( j < nNum.aInt.getSize() ) | 482 | for( int j = -iAnchor; j < nNum.aInt.getSize(); j++ ) |
| 483 | r.aInt.append( nNum.aInt[j] ); | ||
| 484 | int jtop = 0; | ||
| 485 | for(; nNum.aInt[jtop] == 0; jtop++ ) { } | ||
| 486 | // Bu::println("Computing remainder fraction from %1 to %2") | ||
| 487 | // .arg( -iAnchor-1 ).arg( jtop ); | ||
| 488 | r.aFrac.clear(); | ||
| 489 | for( int j = -iAnchor-1, k = 0; j >= jtop && k < r.iScale; j--, k++ ) | ||
| 490 | { | 490 | { |
| 491 | r.aFrac.set( k, nNum.aInt[j] ); | 491 | if( j < nNum.aInt.getSize() ) |
| 492 | // Bu::println("setting %1 to %2").arg( k ).arg( nNum.aInt[j] ); | 492 | { |
| 493 | r.aFrac.set( k, nNum.aInt[j] ); | ||
| 494 | // Bu::println("setting %1 to %2").arg( k ).arg( nNum.aInt[j] ); | ||
| 495 | } | ||
| 493 | } | 496 | } |
| 494 | } | 497 | } |
| 495 | // Bu::println("Final numerator? %1").arg( nNum ); | 498 | /* else |
| 496 | // Bu::println("Remainder? %1").arg( r ); | 499 | { |
| 500 | Bu::println("iAnchor: %1").arg( iAnchor ); | ||
| 501 | } | ||
| 502 | Bu::println("Quotient: %1").arg( q ); | ||
| 503 | Bu::println("Final numerator? %1").arg( nNum ); | ||
| 504 | Bu::println("Remainder? %1").arg( r ); | ||
| 505 | */ | ||
| 497 | } | 506 | } |
| 498 | 507 | ||
| 499 | bool Number::isZero() const | 508 | bool Number::isZero() const |
