Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Intel Community
- Software Development Tools (Compilers, Debuggers, Profilers & Analyzers)
- Intel® Fortran Compiler
- Powers of two gives wrong results ?

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Highlighted
##

WSinc

New Contributor I

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

06-27-2020
01:41 AM

169 Views

Powers of two gives wrong results ?

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

Highlighted
##

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

andrew_4619

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

06-27-2020
01:48 AM

165 Views

Highlighted
##

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.

andrew_4619

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

06-27-2020
01:58 AM

163 Views

Highlighted
##

WSinc

New Contributor I

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

06-27-2020
04:35 AM

154 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 !

Highlighted
##

andrew_4619

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

06-27-2020
04:43 AM

151 Views

I refer you back to me previous reply.

Highlighted
##

mecej4

Black Belt

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

06-27-2020
05:11 AM

146 Views

Please read what Andrew wrote again.

Look at the statement

`bits2=5**ibit`

In Fortran, there are rules for establishing the type of an expression, which you will find in a Fortran manual or tutorial. The expression is evaluated using these (and other applicable) rules without any concern for the type/kind of the variable on the left of the '='. If necessary, the result of the evaluation is converted to the type of the variable to the left of '=' and then the variable is updated to have that value.

Both 5 and **ibit** are of type default integer, i.e., they are 4-byte integers. Now look up the rules and follow them to their logical consequence.

For more complete information about compiler optimizations, see our Optimization Notice.