Next | Making Programs Faster | 125 |
Instead of making a lousy initial guess, like this:
my $guess = Math::BigFloat->new( $num / $root );
Make a good initial guess, like this:
my $guess = Math::BigFloat->new( $num ** (1/$root) );
This uses the hardware floating-point arithmetic to calculate the right answer...
...to 53 bits of accuracy...
...instantaneously
Then use Newton's method to get even closer
After 4 iterations, you have 130 decimal places correct
Moral of the story: Stop fussing around with micro-optimizations
Second moral: The world is full of crappy optimization advice
Next | Copyright © 2003 M. J. Dominus |