Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output each step of a DO-LOOP to a single .txt file 5

Status
Not open for further replies.

MrPranz

Programmer
Apr 18, 2012
5
GB
Hi,

I've got a script with a do while loop that performs a function until a given condition. It performs it about 5000 times, and produces a result on each iteration. I've managed to get fortran to output the data to a .txt file, but it only outputs the result of the last iteration, but I need to to output every iteration to the same file so I get a whole table of data, and not just one row.

This is a coursework assignment, so I don't want to post the code I have, nor do I want any code written for me, but as we're allowed some help, I just want a pointer in the right direction.

Any help is much appreciated.

Thanks. :)
 
The easiest way is to put in print statements before your conditions and see why it is not hitting the print statement.
Code:
! This will print every iteration
do ii = 1, 10
   print *, ii
end do

! This will print only when condition is [b]not[/b] satisfied
do ii = 1, 10
   print *, 'debug ii = ', ii
   if (ii .lt. 5) cycle
   print *, ii
end do

! This will only print when condition is satisfied
do ii = 1, 10
   print *, 'debug ii = ', ii
   if (ii .lt. 5) then
      print *, ii
   end if
end do
 
MrPrantz said:
but it only outputs the result of the last iteration, but I need to to output every iteration to the same file
...
I don't want to post the code I have
It's difficult to give you advice, if you cannot post the relevant part of the code.

Try to analyse what's going wrong.
You can use debugger or simply print out what you need. For example, try to find out if result is computed only in the last iteration or in the other iterations too.
 
If you open the file inside the loop body then you get exactly this undesired effect...

Next time ask telepathic help: there are lots of methods to get "only outputs the result of the last iteration" in unknown code...
 
Thanks for the help. The code inside the loop works fine. It outputs all the data into terminal fine, but only the last result gets output into the text file.

I've written a test code for illustrative purposes.

Code:
program test
implicit none
integer,parameter :: file_no=2
integer :: a
real :: n,n_step

a=0
n=0
n_step=0.5

do while(n<=50)
	print*,a,n
	
	open (unit=file_no,file="test.txt",action="write")
	write (file_no,*) a,n
	close (file_no)
	
	a=a+1
	n=n+n_step

end do

end program test
 
That's what ArkM says: you open the file inside of the loop.
Open and close it only once - i.e change it to:
Code:
open   (unit=file_no,file="test.txt",action="write")    
do while(n<=50)    
  print*,a,n        
  write (file_no,*) a,n    
  a=a+1    
  n=n+n_step
end do
close (file_no)
 
This is just the cause that ArkM pointed to. You open the file under the same name within the loop. That is, every execution of the loop creates the file anew and writes just one record - before it is superceded by the next loop.

Move the open-statement before the loop and the close statement behind it and you are set.

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
ArkM
I do not want to spam - You were faster in typing.
Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Oh, now I understand, that makes a lot of sense. I was opening and closing the file every single time in the loop, hence overwriting the data in the file on every loop iteration. I tried putting the whole open,write,close statements outside of the loop which didn't quite work either.. but opening and closing just once makes perfect sense now.

ArkM and mikrom, many thanks for the help and pointing out where I was going wrong. Much appreciated. :)

MrPranz
 
It was an idea of ArkM.
I'm impressed: ArkM are you clairvoyant to know about the errors without seeing the code ?
:)
 
Its not really necessary in the actual code I have, but just out of curiosity, why does fortran 90 read 0.1 (as in 1/10) as 0.100000001? And is there a way of making it read 0.1 as its supposed to? I tried it as 1/10 but that didn't work..
 
All the numbers are stored in binary: that is base 2. So numbers like 0.5, 0.25, 0.125, 0.0625, 0.03125 will be represented correctly. Numbers like 0.1 are a combination of these so you might get some sort of rounding error.

With a real number, typically 32 bits, you have about 6-7 digits precision. Anything beyond the 6 digits is totally random. With double precision or real*2 or real*8 on some machines, there is more precision but after the drop off point, everything is random.

It does its best to read 0.1. If you don't print so many digits, then you will get 0.1.
 
micron, that's one of the basic software engineering principles - remember Duck test:
If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.
Thank you.
Happy end (good luck to you and I, programmers;)
 
MrPranz (and everybody else)

Hope this is not too out of topic, but I just wanted to share that although I use fortran extensively, I have started to learn Python as a wrapper and on its own and always researching what's availabel in python modules...


...there is one called "decimal" which deals with decimal number in a way that preserves them, e.g.
0.1 = 0.1
0.1 + 0.2 = 0.3 and not 0.30000000000000003

anyway...I thought MrPanz may want to know...

Germán
 
Hi salgerman,
Who is interested in decimal arithmetics could try REXX. It has build in decimal arithmetics with arbitrary precission - example:
precision.rex
Code:
rslt [COLOR=#804040][b]=[/b][/color] 1[COLOR=#804040][b]/[/b][/color]6
[COLOR=#804040][b]say[/b][/color] [COLOR=#ff00ff]"1/6 with"[/color] [COLOR=#008080]DIGITS()[/color] [COLOR=#ff00ff]"digits precission:"[/color]
rslt [COLOR=#804040][b]=[/b][/color] 1[COLOR=#804040][b]/[/b][/color]6
[COLOR=#804040][b]say[/b][/color] rslt
[COLOR=#804040][b]say[/b][/color]

[COLOR=#804040][b]NUMERIC DIGITS[/b][/color] 31
rslt [COLOR=#804040][b]=[/b][/color] 1[COLOR=#804040][b]/[/b][/color]6
[COLOR=#804040][b]say[/b][/color] [COLOR=#ff00ff]"1/6 with"[/color] [COLOR=#008080]DIGITS()[/color] [COLOR=#ff00ff]"digits precission:"[/color]
rslt [COLOR=#804040][b]=[/b][/color] 1[COLOR=#804040][b]/[/b][/color]6
[COLOR=#804040][b]say[/b][/color] rslt
[COLOR=#804040][b]say[/b][/color]

[COLOR=#804040][b]NUMERIC DIGITS[/b][/color] 66
[COLOR=#804040][b]say[/b][/color] [COLOR=#ff00ff]"1/6 with"[/color] [COLOR=#008080]DIGITS()[/color] [COLOR=#ff00ff]"digits precission:"[/color] 
rslt [COLOR=#804040][b]=[/b][/color] 1[COLOR=#804040][b]/[/b][/color]6
[COLOR=#804040][b]say[/b][/color] rslt
Output:
Code:
c:\Users\Roman\Work>rexx precision.rex
1/6 with 9 digits precission:
0.166666667

1/6 with 31 digits precission:
0.1666666666666666666666666666667

1/6 with 66 digits precission:
0.166666666666666666666666666666666666666666666666666666666666666667
 
MrPranz (and everybody else;)

Don't forget that sqrt(2) is not a fraction in decimal or binary arithmetics. Don't forget that it's impossible to get exact value of 1/3 in decimal (or binary) arithmetics...

Better think about real or double precision data as approximations of any numbers and take into account this fact when implementing all numerical algorithmes.

Never use .EQ. ops with floating point operands (except .NE.0),

Never use low precision (REAL) data in math calculations (may be except special cases - it's the other song;).

Never use REAL(any kind) data in banking software until you stop to be surprised at: (1.0/10.0 .EQ. 0.1) may be true or false on different computers or compilers...
 
mikrom...the way you stated your first sentence "Who is interested in decimal arithmetics could try REXX", I thought you were going to enlighten me with a Fortran library; otherwise, you were contributing with another piece of software that can handle decimals. I do appreciate the link and I learned something new today.

Interestingly enough, REXX seems to be a precursor of Tcl and Python...I used tcl for many years and grew tired of it...replaced it with Python...python is much better that Tcl all the way around even for actual math...tcl is dying, python is being adopted as a scripting language in many software, even proprietary ones.

In REXX, as just read, everything is a string (same in Tcl), so I pressume doing math may be a bit of a pain.

By the way, your link to Wikipedia indicates that the last stable release was 16 years ago...really? If you know better, you'd better go fix it :)

Cheers,

Germán



 
salgerman said:
Wikipedia indicates that the last stable release was 16 years ago...really?
The language is old - developed cca 1980+ by IBM, but there are actual open source interpreters available.
In REXX, as just read, everything is a string (same in Tcl), so I pressume doing math may be a bit of a pain
Math libraries are available:
You can do vectors and matrices with stems (associative arrays)

However REXX is primarily designed as a scripting language, so it isn't large scale numbers cruncher like Fortran.
If you are interested, you can give it a try. If you have other questions ask here in the REXX forum:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top