title: Converting base of floating point number without losing precision
body: <h2>Terminology</h2>
  
  <p>In this question I am calling "floating point number" "decimal number" to prevent ambiguation with the <code>float</code>/<code>double</code> Java primitive data types. The term "decimal" has no relationship with "base 10".</p>
  
  <h2>Background</h2>
  
  <p>I am expressing a decimal number of any base in this way:</p>
  
  <pre><code>class Decimal{
      int[] digits;
      int exponent;
      int base;
      int signum;
  }
  </code></pre>
  
  <p>which approximately expresses this <code>double</code> value:</p>
  
  <pre><code>public double toDouble(){
      if(signum == 0) return 0d;
      double out = 0d;
      for(int i = digits.length - 1, j = 0; i &gt;= 0; i--, j++){
          out += digits[i] * Math.pow(base, j + exponent);
      }
      return out * signum;
  }
  </code></pre>
  
  <p>I am aware that some conversions are not possible. For example, it is not possible to convert <code>0.1 (base 3)</code> to base 10, because it is a recurring decimal. Similarly, converting <code>0.1 (base 9)</code> to base 3 is not possible, but covnerting <code>0.3 (base 3)</code> is possible. There are probably other cases that I have not considered.</p>
  
  <h3>The traditional way</h3>
  
  <p>The traditional way (by hand) of change of base, for integers, from base 10 to base 2, is to divide the number by the exponents of 2, and from base 2 to base 10 is to multiply the digits by respective exponents of 2. Changing from base <em>x</em> to base <em>y</em> usually involves converting to base 10 as an intermediate.</p>
  
  <h2>First question: Argument validation</h2>
  
  <p>Therefore, my first question is, if I were to implement the method <code>public Decimal Decimal.changeBase(int newBase)</code>, how can I validate whether <code>newBase</code> can be made without resulting in recurring decimals (which is incompatible with the design of the <code>int[] digits</code> field, since I don't plan to make an <code>int recurringOffset</code> field just for this.</p>
  
  <h2>Second question: Implementation</h2>
  
  <p>Hence, how to implement this? I instinctively feel that this question is much easier to solve if the first question is solved.</p>
  
  <h2>Third question: What about recurring number output:</h2>
  
  <blockquote>
    <p>I don't plan to make an <code>int recurringOffset</code> field just for this.</p>
  </blockquote>
  
  <p>For the sake of future readers, this question should also be asked.</p>
  
  <p>For example, according to <a href="http://www.wolframalpha.com/input/?i=0.1_4%20to%20base%209">Wolfram|Alpha</a>:</p>
  
  <pre><code>0.1 (base 4) = 0.[2...] (base 9)
  </code></pre>
  
  <p>How can this be calculated (by hand, if by programming sounds too complicated)?</p>
  
  <p>I think that a data structure like this can represent this decimal number:</p>
  
  <pre><code>class Decimal{
      int[] constDigits;
      int exponent;
      int base;
      int signum;
      @Nullable @NonEmpty int[] appendRecurring;
  }
  </code></pre>
  
  <p>For example, <a href="http://www.wolframalpha.com/input/?i=61%2F55"><code>61/55</code></a> can be expressed like this:</p>
  
  <pre><code>{
      constDigits: [1, 1], // 11
      exponent: -1, // 11e-1
      base: 10,
      signum: 1, // positive
      appendRecurring: [0, 9]
  }
  </code></pre>
  
  <p><hr></p>
  
  <h3>Not a homework question</h3>
  
  <p><strong>I am not looking for any libraries. Please do not answer this question with reference to any libraries.</strong> (Because I'm writing this class just for fun, OK?)</p>