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

Click For Summary
SUMMARY

The discussion revolves around reading a dataset with variable decimal places in Fortran and the challenges of preserving the original formatting when writing to a new file. The user initially used the READ and WRITE statements with a specified format that altered the decimal precision. The solution involves reading the data as character strings, converting them to binary numeric data using Fortran's "internal read" technique, and then writing the original character strings back out to maintain the original decimal formatting. This approach effectively resolves the issue of altered decimal places in the output file.

PREREQUISITES
  • Understanding of Fortran programming language
  • Familiarity with Fortran I/O operations, specifically READ and WRITE statements
  • Knowledge of Fortran's "internal read" technique
  • Basic concepts of data types, particularly character and real types in Fortran
NEXT STEPS
  • Research Fortran character string manipulation techniques
  • Learn about Fortran's internal read and write operations
  • Explore advanced formatting options in Fortran for output
  • Investigate data type conversions in Fortran
USEFUL FOR

Fortran developers, data scientists working with numerical datasets, and anyone involved in data formatting and output precision in scientific computing.

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.
 
We have many threads on AI, which are mostly AI/LLM, e.g,. ChatGPT, Claude, etc. It is important to draw a distinction between AI/LLM and AI/ML/DL, where ML - Machine Learning and DL = Deep Learning. AI is a broad technology; the AI/ML/DL is being developed to handle large data sets, and even seemingly disparate datasets to rapidly evaluated the data and determine the quantitative relationships in order to understand what those relationships (about the variaboles) mean. At the Harvard &...

Similar threads

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K