Comp Sci Real step-size in do loop-fortran

  • Thread starter Thread starter Antonija
  • Start date Start date
AI Thread Summary
Creating a real step-size in a Fortran do loop is not supported, as the language requires integer indices for do loops. The error encountered is due to the use of a real variable as the loop index, which defaults to integer types unless specified otherwise. Although theoretically possible, using real indices can lead to issues with round-off errors and improper termination of loops. A recommended approach is to use an integer index and calculate the real equivalent within the loop. The discussion also highlights that real index loops were made obsolete in Fortran 95, suggesting a shift to alternative looping structures like do while loops for such tasks.
Antonija
Messages
18
Reaction score
0

Homework Statement



I'm trying to find out is it possible to make a real do loop in fortran. As I read online, it's possible, and there is written how to do it but when I copy/paste it into my fortran editor, it' doesn't work.
At the moment I'm writing the code for minimize a function of a single variable, and one of the things I need is the real step do loop.

Homework Equations

The Attempt at a Solution


This is how my part of code looks like:

real:: a,b
real::delta=0.0001
do i=a,b, delta
variablesvalues(1)=x+(i-1)*delta
call {some function}
answer=evaluate(...)
end do

and this is the code for which is written that it's example for real loop:
program xytab
implicit none
!constructs a table of z=x/y for values of x from 1 to 2 and
!y from 1 to 4 in steps of .5
real :: x, y, z
do x = 1,2
do y = 1,4,0.5
z = x/y
print *, x,y,z
end do
end do
end program xytabin both examples I get the same error: Deleted feature: Step expression in DO loop at (1) must be integer.

I'm using Fortran 90.

Also, is it possible to have only step-size real, and other integer type?
 
Physics news on Phys.org
There's a couple of things here.
1. If you're going to post source code, please use CODE tags to enclose the code. These allow for the preservation of spacing, etc. of the code as written by you.

2. In your variable declarations, you have declared a and b as real variables, but in the first DO-loop, the index variable i is assumed to be an INTEGER type (variables starting with I-N are automatically declared INTEGERs unless the program is otherwise instructed.), which is one reason why the compiler returns the error message.

3. In theory, real index variables for DO-loops may be programmed, but you should be careful using this approach. Floating point arithmetic always is subject to round-off error, so your real-valued DO-loops may not terminate properly when you expect them to.
 
SteamKing said:
3. In theory, real index variables for DO-loops may be programmed
In the Fortran 90 standard, do loops with real indices were declared obsolete, and eliminated in Fortran 95.

To the OP: loop with an integer index, and calculate the real equivalent inside the loop:
Fortran:
do i = 1, 1000000
   real_i = real(i) * 1e-5
   ...
end do
will loop from 1 to 10 in steps of 0.00001
 
DrClaude said:
In the Fortran 90 standard, do loops with real indices were declared obsolete, and eliminated in Fortran 95.

Actually, at the moment I'm not sure if I have fortran 90 or fortran 95, because my linux crashed a month ago and then I had to build everything new... However, I tried it with do while loop and it worked...

It's like this:
Code:
real:: a, x, delta= 0.0001
do while(x<=a)
x=x+delta
call [function]
...
end do
 
Back
Top