Fortran Problem on reading dataset with variable decimal places as it is using fortran

AI Thread Summary
The discussion centers on reading and writing datasets in Fortran while preserving the original decimal formatting. The user initially faced an issue where the output data was altered to three decimal places due to the specified format in the WRITE statement. It was clarified that when data is read into a binary format, the original decimal precision is lost. To maintain the original formatting, it was suggested to read the data as character strings, convert them to numeric values for calculations, and then write the original character strings back to the output file. The user successfully implemented this approach after understanding the advice given, resolving the formatting issue.
fortranuser
Messages
7
Reaction score
0
Hi all,
I have attached my text file(tab delimited).
I am planning to read those dataset into real array of (30,38). The dataset has variable decimal places like 312.66, 0, 535.696 ,456122.758.
With the following codes,
READ(12,*)((X(I,J),I=1,30),J=1,38)

WRITE(*,20)((X(I,J),i=1,30),j=1,38)

20 format(58(f12.3x,2x))

I was able to read the original txt file and stored those data into real array of (30,38) and then it was written in new txt file.

The problem is that because of the format(58(f12.3x,2x)) the data set in latter file got changed(now each data set has 3 decimal places)
Is it possible to make data in latter file similar to original text file
 
Technology news on Phys.org
When you read the original data, it's converted from decimal to binary and stored as binary inside your program. The binary data has no "memory" of the original number of decimal places.

If you really need to preserve the original number of decimal places on output, you should read the data as character strings, then convert the strings to binary numeric data using Fortran's "internal read" technique. Do your calculations using the converted data, but write the original data back out using the character strings.
 
jtbell said:
When you read the original data, it's converted from decimal to binary and stored as binary inside your program. The binary data has no "memory" of the original number of decimal places.

If you really need to preserve the original number of decimal places on output, you should read the data as character strings, then convert the strings to binary numeric data using Fortran's "internal read" technique. Do your calculations using the converted data, but write the original data back out using the character strings.

hi jtbell,
Thanks for writing me. I have written the program just to test which is as follow
PROGRAM CHAR_ARRAY

character(10):: data
real::a
read(*,*)data
read(data,*)a
print*,a
END
I gave 45.2 to data and output was 45.200001.
so still the problem is same.
 
Well, what jtbell said was to write the original data back out by using the original 'character' variable you read it into...not using your internal 'real' variables.
 
gsal said:
Well, what jtbell said was to write the original data back out by using the original 'character' variable you read it into...not using your internal 'real' variables.

hi gsal
I carefully read to jtbell again and finally understood what you and he wants me to do.
Thanks both of you.Problem is no more.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
12
Views
3K
Replies
1
Views
3K
Replies
5
Views
2K
Replies
2
Views
1K
Replies
22
Views
4K
Replies
2
Views
2K
Replies
10
Views
4K
Replies
6
Views
2K
Back
Top