Next Making Programs Faster 119

Numerical Calculation


If you want something relatively simple like the 5th root of 100:
      $x = Root( 100, 5 );


the result is reasonably fast. However, with each iteration, it get progressively slower. So if you wanted something enormous, like:
      $x = Root( 500000, 555 );


you could be waiting for ages. If we leave the number of iterations low, the result will likely be very inaccurate, but as we increase the number of iterations, each individual iteration gets slower and slower. The only thing I've been able to come up with so far is the comparison of $guess and $current inside the for loop. I was able to get a bit of a speed boost by doing a string comparison rather than a numeric comparison. Any suggestions on how to speed this up?

Next Copyright © 2003 M. J. Dominus