[Thread Prev][Thread Next][Index]

Re: [ferret_users] r^n (real number raised to the power of an integer)



Hi Ryo,

 v^2 is computed as an exponent operation v**2.  As everything coming in from the command line is stored as real (double-precision) data, the code does check whether the value of the exponent is in fact an integer, explicitly asking for r**int(s) when s is an integer, so that happens regardless of what the compiler might do.

Because squaring data is such a common operation, it would probably be worthwhile for us to change the code to do the case of v^2 as v*v.

Ansley

On 8/28/2018 9:39 PM, Ryo Furue wrote:
Dear Ferret developers,

I have a minor question.  I was wondering how the exponentiation (the "^" operator) is implemented when the exponent is an integer?  as in

let v2 = v^2

Is it transformed to v*v or handled effectively as v*v ?

The exponentiation of real numbers is an expensive operation.  I found a casual benchmark (not a very scientific one) which said that v*v was 4 times faster than v^2.0 on her/his computer.  This difference could be significant for a large array.

Cheers,
Ryo
--------------
Aside 1: The Fortran language does treat r**n differently from r**s, where n is an integer and s is a real number.  There are even compilers that calculate r**s as r**int(s) when it detects s has an exact integral value.

Aside 2: I guess r**n is slower than r**s when n is large because the number of operations grows like log(n) .

[Thread Prev][Thread Next][Index]
Contact Us
Dept of Commerce / NOAA / OAR / PMEL / Ferret

Privacy Policy | Disclaimer | Accessibility Statement