On Tue, 2004-04-27 at 22:29, Matt Fowles wrote: > All~ > > Perhaps it is just the mathematician in me speaking, but I think that a > sqrt op might be superfluous and could be replaced with exp as sqrt(x) > == exp(x,0.5). I cannot say anything about numerical stability of > precision, though, so feel free to shoot me down. > > Matt
Then it's the processor in me speaking that it's much faster to optimize for the sqrt() case. At least on the x86 and some PowerPC chips, there's an actual fsqrt op because the processor can just shift the exponent left or right. Even if an fsqrt op doesn't exist, Newton's method is very easy for a sqrt function. Pow() is harder to implement, since you can't get away with Newton's method (you run into a chicken-and-egg problem); I believe you have to take either a square root or a square for every bit in the desired exponent and then multiply the results together (corrections anyone?). The result is enormously large and slow (and perhaps imprecise as well?) compared to sqrt(); even using the native pow() it takes nearly 15x as long as the native sqrt() on my machine. Hence, if you are taking lots of square roots, it's definitely in your best interests to use a sqrt() instead of pow(). Peter