 "Mark R. Diggory" <mdiggory@latte.harvard.edu> wrote:
> Al Chou wrote:
> >> * @param values Is a double[] containing the values
> >> * @return the result, Double.NaN if no values for an empty array
> >> * or 0.0 for a single value set.
> >> @@ 168,10 +175,12 @@
> >> } else if (values.length > 1) {
> >> double mean = mean(values);
> >> double accum = 0.0;
> >> + double accum2 = 0.0;
> >> for (int i = 0; i < values.length; i++) {
> >> accum += Math.pow((values[i]  mean), 2.0);
> >> + accum2 += (values[i]  mean);
> >> }
> >>  variance = accum / (double)(values.length  1);
> >> + variance = (accum  (Math.pow(accum2,2)/(double)values.length)) /
> >>(double)(values.length  1);
> >
> >
> > Maybe I'm displaying my old Fortran programmer's bias or premature
> optimization
> > without first profiling, but is there a good reason to call Math.pow for
> the
> > square in this line rather than do a multiplication? I can kind of see why
> you
> > wouldn't want to introduce a new variable in the "accum +=" line above this
> > one, but I don't see the need to do a function call for a square on this
> line.
> >
>
> Shamefully, I've never programmed Fortran, so you have a unique
> perspective over me. We've gone back and forth on this subject before.
> In this case its probably fine to do either x*x or Math.pow(x,2).
Don't say "shamefully". Jeez, I don't apologize for never having written in
machine language (OK, actually I have for an undergraduate microprocessor
structures class, but it was really handassembly of assembly code into binary,
so I can't truly claim to have written binary machine code myself). I was just
saying that my habits from Fortran, strong as they are, may not be relevant in
Java.
> One also has to take into consideration that the Math/StrictMath.pow
> method is actually a native method and is thus also actually implemented
> in the native binary portion of the JVM. This is quite efficient and
> optimized from what I understand, but I'm no expert on this subject and
> this is just speculation. I tend to use the Math static library more
> from a moral standpoint, if we're building the same sort of static
> library, then its respectable for us to use the java Math library
> internally for consistency. But, thats just an opinion.
My understanding is that in Fortran, x**2.0 is done via logarithms, whereas
x**2 may as well be written x * x, and often is written explicitly that way.
That distinction could still hold in Java, regardless of whether the
implementation is native. Logarithms, being nontrivial functions, are more
expensive than multiplication.
Anyway, just food for thought. I had to bring it up at some point. <g>
Al
=====
Albert Davidson Chou
Get answers to Mac questions at http://www.MacMgrs.org/ .
__________________________________
Do you Yahoo!?
SBC Yahoo! DSL  Now only $29.95 per month!
http://sbc.yahoo.com

To unsubscribe, email: commonsdevunsubscribe@jakarta.apache.org
For additional commands, email: commonsdevhelp@jakarta.apache.org
