Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!

## Powers of two gives wrong results ?

New Contributor I
253 Views

If I am generating bits within an 8 byte integer, I should be able to use any power of two up to 63 for positive results, right ?

Ex:

integer (8) ix

ix=2**40

but that gives a wrong answer.

apparently the math library cannot do powers of two higher then 31.

Is there a way around this problem, other than using a 4 byte integer.

5 Replies
Honored Contributor I
249 Views

I didn't look at an example but I will note that you are calculating an 8 byte integer using 4 byte constants. Try 2_8**40_8

Honored Contributor I
247 Views

I should have added that the expression with 4 byte integers will be evaluated with 4 byte precision and then the result will be transferred to the 8 byte variable on the left hand side. Doing 2_8**40 would also work because the 8 byte constant would cause the 40 to get promoted to the higher kind before the calculation. This is standard conforming, your code isn't. The is a common error. I would recommend specifying kind on all constants at all times other then where default kind is being used.

New Contributor I
238 Views

Here is a code fragment:

integer(8) bits(0:63),bits2
bits(0)=1
do ibit=1,28
bits(ibit)=5*bits(ibit-1)
bits2=5**ibit
print 114,ibit,bits(ibit),bits2
114 format(i4,2I24)
enddo

The bits2 is correct, until the answer is more than 32 bits.

Then it goes beserk !

Honored Contributor I
235 Views

I refer you back to me previous reply.

Black Belt
230 Views

``bits2=5**ibit``