Phil Steitz wrote:
I am also a little confused by J's analysis. Do we know definitively that using
J2SE, there is precision loss in Math.pow(x,n) vs x*x*...*x (n terms) for small
integer n?

Actually no. I didn't know they use a specific library routine which is stunningly good. Some tests showed d*d equals pow(d,2) uniformly for the whole range from 1E-10 to 1E11, and for cubes there is one bit difference, which might well be a roundoff error in d*d*d rather than precision loss in pow.

Mind you, testing exp(2*log(d)) showed expected behaviour, roughly
one bit difference for numbers in 1..10, logarithmically growing
with abs(exponent) to the expected 4 bits at 1E7.

The other issue is performance. I used loops which store i*i in
an integer array, i*(double)i and pow(i,2) in a double array. If
the first was 100%, the second used 120% and the third 1500% (yes,
more than an order of magnitude slower). For the first two results,
time is still swamped with loop overhead, array range check and
other stuff, using cubes instead of squares increased the first
loop time to 110%, second perhaps to 125%, while pow(d,3) nearly
tripled to ~4500%. The inlined HW-supported code I was familar
was not nearly as bad, in particular performance was basically
independent of the exponent.

A few attempts at eliminating the loop and other overhead from
measurement failed badly. Summing up the result instead of storing
it in an integer resulted in similar relative times, except that i*i
and i*(double)i had the same performance. I suspect that storing
32000 doubles (~256kB) hit harder on memory bandwidth than storing
the same number of integers.

 If the answer is yes, we should establish the guideline that for
integer n < 4, we use explicit products instead of Math.pow(-,n).

Well, there is no precision loss, and a few performance hogs here and there are hardly noticed in the overall performance of an algorithm. The latter is especially true for Java, where achiving the maximal possible numerical performance for the hardware or even getting in the same ballpark is illusionary anyway. Nevertheless, I find x*x easier to read than Math.pow(x,2), in particular if "x" is really a short-named variable.

J.Pietschmann


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to