From ab6a53d64a2afcc4dd106753548387cd9f417531 Mon Sep 17 00:00:00 2001
From: Mike Buland <mike@xagasoft.com>
Date: Thu, 30 Oct 2014 14:28:40 -0600
Subject: Fixed remaining known division issues.

It looks great, and works with everything I can think of so far.
---
 src/number.cpp | 43 ++++++++++++++++++++++++++-----------------
 1 file 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
         nNum.aInt.append( digit( j ) );
     }
 //    Bu::println("New numerator: %1").arg( nNum );
-    while( iAnchor > -iScale && !nNum.isZero()) // division loop
+    while( iAnchor > -iScale && (!nNum.isZero() || iAnchor > 0)) // division loop
     {
         // As long as the numerator is smaller than the divisor we keep
         // including new digits.
@@ -449,10 +449,10 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const
             nNum.aInt.insert( 0, digit(iAnchor-iNumShift) );
 //            Bu::println("Numerator too small, new numerator: %1").arg( nNum );
         }
-        if( iAnchor < -iScale )
-            break;
 
-//        Bu::println("  New anchor: %1").arg( iAnchor );
+//        Bu::println("  New anchor: %1 vs iScale = %2").arg( iAnchor ).arg( iScale );
+        if( iAnchor < 0 && -1-iAnchor >= iScale )
+            break;
         
         // How many times does the divisor fit into the numerator
         int iCount = 0;
@@ -477,23 +477,32 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const
             q.aFrac.set( -1-iAnchor, iCount );
         }
     }
-    for( int j = -iAnchor; j < nNum.aInt.getSize(); j++ )
-        r.aInt.append( nNum.aInt[j] );
-    int jtop = 0;
-    for(; nNum.aInt[jtop] == 0; jtop++ ) { }
-//    Bu::println("Computing remainder fraction from %1 to %2")
-//        .arg( -iAnchor-1 ).arg( jtop );
-    r.aFrac.clear();
-    for( int j = -iAnchor-1, k = 0; j >= jtop && k < r.iScale; j--, k++ )
+//    if( iAnchor <= 0 )
     {
-        if( j < nNum.aInt.getSize() )
+        for( int j = -iAnchor; j < nNum.aInt.getSize(); j++ )
+            r.aInt.append( nNum.aInt[j] );
+        int jtop = 0;
+        for(; nNum.aInt[jtop] == 0; jtop++ ) { }
+    //    Bu::println("Computing remainder fraction from %1 to %2")
+    //        .arg( -iAnchor-1 ).arg( jtop );
+        r.aFrac.clear();
+        for( int j = -iAnchor-1, k = 0; j >= jtop && k < r.iScale; j--, k++ )
         {
-            r.aFrac.set( k, nNum.aInt[j] );
-//            Bu::println("setting %1 to %2").arg( k ).arg( nNum.aInt[j] );
+            if( j < nNum.aInt.getSize() )
+            {
+                r.aFrac.set( k, nNum.aInt[j] );
+    //            Bu::println("setting %1 to %2").arg( k ).arg( nNum.aInt[j] );
+            }
         }
     }
-//    Bu::println("Final numerator? %1").arg( nNum );
-//    Bu::println("Remainder? %1").arg( r );  
+/*    else
+    {
+        Bu::println("iAnchor: %1").arg( iAnchor );
+    }
+    Bu::println("Quotient: %1").arg( q );
+    Bu::println("Final numerator? %1").arg( nNum );
+    Bu::println("Remainder? %1").arg( r );  
+    */
 }
 
 bool Number::isZero() const
-- 
cgit v1.2.3