Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Data type when doing ** operation

onkelhotte
New Contributor II
264 Views
Hi there,

I was wondering what data type should I use when I do a ** operation.

Is that fromcompiler point of view a difference when I calculate real**integer or should I do real**real?

For example 2.5**4 or 2.5**4. ?

Markus
0 Kudos
4 Replies
Les_Neilson
Valued Contributor II
264 Views
Quoting - onkelhotte
Hi there,

I was wondering what data type should I use when I do a ** operation.

Is that fromcompiler point of view a difference when I calculate real**integer or should I do real**real?

For example 2.5**4 or 2.5**4. ?

Markus

I believe integer exponentiation (where known)is better because of optimization eg (from the help) M=J**2 is optimized to M=J*J

Les

0 Kudos
rase
New Contributor I
264 Views
I think it is good practice to write always a=b*b instead of a=b**2 or a=b*b*b instead of a=b**3, if possible, of course. Even if the compiler optimizes the exponentation with integers I prefer to control the optimization.
0 Kudos
Steven_L_Intel1
Employee
264 Views
I agree with Les. Use an integer exponent if it is integer-valued.
0 Kudos
TimP
Honored Contributor III
264 Views
Quoting - onkelhotte

Is that fromcompiler point of view a difference when I calculate real**integer or should I do real**real?

For example 2.5**4 or 2.5**4. ?

2.5**4 would likely be evaluated as (2.5*2.5)*(2.5*2.5). 2.5**4. might be evaluated as something resembling exp(4.*log(2.5)), or it might be evaluated the same as 2.5**4
The integer exponent is certain to be at least as efficient and accurate as the real exponent. In this case, the result with the integer exponent would be exact, while that is not guaranteed with the real exponent.
If you want to be certain of a specific order of evaluation, such as ((2.5*2.5)*2.5)*2.5, you must write it out with parentheses, and, with ifort, include the option /assume:protect_parens. This prevents optimization to a potentially faster or slightly more accurate version. I don't know of any good reason to avoid the integer exponentiation.
0 Kudos
Reply