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

In summary, In jtbell's text file there are different number of decimal places for every data set. The problem is that when he wants to write the data set back out as a new text file, the original number of decimal places is lost. He suggests reading the data in as character strings and then converting them to binary numeric data using Fortran's "internal read" technique. This would allow him to do his calculations using the original data, but write it back out in character form.
  • #1
fortranuser
7
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5
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.
 

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

What is the cause of the problem on reading dataset with variable decimal places using Fortran?

The problem is caused by the way Fortran handles floating-point numbers. Fortran uses a fixed format for numbers, which means that all numbers are represented with a specific number of digits before and after the decimal point. This can cause issues when trying to read a dataset with variable decimal places, as the numbers may not fit into the fixed format.

How does this problem affect data analysis using Fortran?

This problem can affect data analysis in two ways. First, it can lead to inaccurate results if the numbers are not read and stored correctly. Second, it can cause errors and crashes in the program if the code is not written to handle variable decimal places.

Is there a way to solve this problem?

Yes, there are several ways to solve this problem. One option is to use a different programming language that is better suited for handling variable decimal places, such as Python or R. Another option is to use a more modern version of Fortran, such as Fortran 90 or later, which has improved capabilities for handling floating-point numbers.

Can this problem be prevented in the future?

Yes, this problem can be prevented by being aware of the limitations of Fortran when it comes to floating-point numbers. When working with datasets that have variable decimal places, it is important to carefully consider the data types and formats used, and to use appropriate methods for reading and storing the data.

Are there any best practices for handling this issue in Fortran?

Yes, there are some best practices for handling this issue in Fortran. One approach is to define a data type specifically for handling numbers with variable decimal places, and to use this data type consistently throughout the program. Another approach is to use functions or subroutines to read and store the data, rather than relying on built-in Fortran functions that may not be suitable for handling variable decimal places. It is also important to thoroughly test the code and handle potential errors and exceptions appropriately.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
22
Views
3K
  • Programming and Computer Science
Replies
4
Views
643
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top