title: nth root implementation
body: <p>I am working on a way to calculate the n<sup>th</sup> root of a number. However, I am having problems with the n<sup>th</sup> root of negative numbers.</p>
  
  <p>Most people say to use <code>Math.pow(num, 1 / root)</code>, but this does not work for negative numbers.</p>
  
  <p>I have tried this:</p>
  
  <pre><code>public static double root(double num, double root) {
      if (num &lt; 0) {
          return -Math.pow(Math.abs(num), (1 / root));
      }
      return Math.pow(num, 1.0 / root);
  }
  </code></pre>
  
  <p>but, it does not work for all numbers as the root can be a decimal. For example <code>root(-26, 0.8)</code> returns <code>-58.71</code>, but that is an invalid input. This will also give the wrong answer for even roots. For example <code>root(-2, 2)</code> returns <code>-1.41421</code>, but -2 does not have a square root.</p>