[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Q]: ID analog to FORTRAN "sign" function



"Dick Jackson" <dick@d-jackson.com> wrote in message
8MkF5.12$953.172@read1">news:8MkF5.12$953.172@read1...
> Do I dare offer one more? Subscript lookups seem faster than arithmetic
> operations, making this one faster, more compact and no less cryptic! :-)
>
>     Return, Abs(a) * ([-1, 1])[b GE 0]

Good one!

> A bit faster still, if you know the expected type of a and b, to avoid an
> extra type conversion:
>
>     Return, Abs(a) * ([-1.0, 1.0])[b GE 0]

I think that's a little *too* clever. I just tried multiplying a float array
with 10^7 elements by 1 and then by 1.0. Time taken = 0.52 seconds in both
cases.

---
Mark Hadfield
m.hadfield@niwa.cri.nz  http://katipo.niwa.cri.nz/~hadfield/
National Institute for Water and Atmospheric Research
PO Box 14-901, Wellington, New Zealand

Hi! I'm a .signature virus! copy me into your .signature file to help me
spread!