INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
Are you a Computer / IT professional? Join Tek-Tips now!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site

(Download This Button Today!)
Feedback
"...I have learned more through this forum than I did on a two day course. Thanks to everyone for their help and other postings that I have found useful..."
Geography
Where in the world do Tek-Tips members come from?
|
Output each step of a DO-LOOP to a single .txt file (5)
|
|
|
MrPranz (Programmer) |
18 Apr 12 15:09 |
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. :) |
|
|
xwb (Programmer) |
18 Apr 12 16:01 |
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 not 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 |
|
|
mikrom (Programmer) |
18 Apr 12 16:14 |
Quote (MrPrantz): 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. |
|
(2) ArkM (IS/IT--Management) |
18 Apr 12 16:59 |
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... |
|
|
MrPranz (Programmer) |
18 Apr 12 17:00 |
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. CODEprogram 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 |
|
mikrom (Programmer) |
18 Apr 12 17:05 |
That's what ArkM says: you open the file inside of the loop. Open and close it only once - i.e change it to: CODEopen (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. |
|
|
MrPranz (Programmer) |
18 Apr 12 17:11 |
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 |
|
|
mikrom (Programmer) |
18 Apr 12 17:33 |
It was an idea of ArkM. I'm impressed: ArkM are you clairvoyant to know about the errors without seeing the code ? |
|
|
MrPranz (Programmer) |
18 Apr 12 18:07 |
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.. |
|
xwb (Programmer) |
18 Apr 12 18:48 |
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. |
|
|
MrPranz (Programmer) |
18 Apr 12 19:15 |
I see. Thanks for the clarification. :) |
|
|
ArkM (IS/IT--Management) |
19 Apr 12 1:35 |
micron, that's one of the basic software engineering principles - remember Duck test: Quote: 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 |
|
|
mikrom (Programmer) |
19 Apr 12 16:21 |
Hi salgerman, Who is interested in decimal arithmetics could try REXX. It has build in decimal arithmetics with arbitrary precission - example: precision.rexCODErslt = 1/6 say "1/6 with" DIGITS() "digits precission:" rslt = 1/6 say rslt say
NUMERIC DIGITS 31 rslt = 1/6 say "1/6 with" DIGITS() "digits precission:" rslt = 1/6 say rslt say
NUMERIC DIGITS 66 say "1/6 with" DIGITS() "digits precission:" rslt = 1/6 say rslt Output: CODEc:\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 |
|
|
ArkM (IS/IT--Management) |
19 Apr 12 17:11 |
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 |
|
|
mikrom (Programmer) |
19 Apr 12 18:57 |
Quote (salgerman): 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. Quote: 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: http://www.tek-tips.com/viewthread.cfm?qid=1627582You 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: http://www.tek-tips.com/threadminder.cfm?pid=277 |
|
|
 |
|