Can't read float number from file

  • Context: Fortran 
  • Thread starter Thread starter bou
  • Start date Start date
  • Tags Tags
    File Float
Click For Summary

Discussion Overview

The discussion revolves around issues related to reading float numbers from a file in FORTRAN, specifically focusing on runtime errors encountered when the input file contains real numbers. Participants also explore problems related to modifying data values in a separate conversion program.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant reports a runtime error when attempting to read float numbers from a file, suggesting that the format used may not be compatible with the input file's structure.
  • Another participant suggests that the presence of tabs in the text file might be causing the reading issue.
  • A participant shares a conversion program and seeks assistance with a specific line of code that is intended to replace a placeholder value (-9999) with another value (9999), but it does not appear to work as expected.
  • One participant advises against using equality checks with floating-point numbers due to potential rounding errors, suggesting a different approach to handle the comparison.
  • A later reply points out that the original code does not loop over the correct index, indicating that the logic for modifying the values of Q needs to be adjusted to include a double loop.

Areas of Agreement / Disagreement

Participants express differing views on the causes of the issues encountered, particularly regarding the reading of float numbers and the handling of floating-point comparisons. No consensus is reached on the best approach to resolve these problems.

Contextual Notes

Participants mention potential limitations related to file formatting and the handling of floating-point precision, but these remain unresolved within the discussion.

Who May Find This Useful

Individuals working with FORTRAN and dealing with file input/output operations, particularly those encountering issues with floating-point numbers and data manipulation in programming.

bou
Messages
3
Reaction score
0
Hi everyone I need help for reading file that contain float number, I don't understand the problem or what should I have to fix; the code seem to be fine if the read file is integer number but if the number is real number the FORTRAN give me error Runtime Error: debug_input.f(12): Invalid character in real input field
Fortran:
      PROGRAM test
      PARAMETER (NX=928,NY=775)
     
      REAL IFDY (NX,NY)
      DO J=1,NY
        DO I=1,NX
          IFDY(I,J)=0
          ENDDO
      ENDDO    
      open(100, FILE="100y_dem2.txt",
     & STATUS='OLD', ACTION='READ',RECL=25000)
      read(100,'(928f7.4)')((IFDY(I,J),I=1,NX),J=1,NY)
      close(100)
      PRINT *, ifdy
      END PROGRAM
the code can read file if i change from format(928f7.4) to default (*) but i need to use format to read file for calculate, sorry for my poor explanation.
thank in advance
 

Attachments

Technology news on Phys.org
Your text file contains tabs. I believe that is the problem.
 
  • Like
Likes   Reactions: bou
thank you for your answer, now i try to covert the input file format to the format that fortran can read without use excel , but i have a problem with changing the data value, can you help me have a look
Fortran:
      PROGRAM CONVERT
      PARAMETER (IG=775,JG=928)
      INTEGER I,J
      REAL Q(775,928)
      CHARACTER FN*40, FN2*3, FNOUT*45, ANS*1
  200 CONTINUE
      WRITE(6,*) 'INPUT FILE NAME'
      READ(5,*) FN
      FN2="new"
      FNOUT=FN2//FN
      OPEN(2,FILE=FN,STATUS='OLD',RECL=25000)
      DO J=1,JG
       READ(2,*)(Q(I,J),I=1,IG)
      ENDDO
      CLOSE(2)
      OPEN(10,FILE=FNOUT, RECL=25000)
      DO J=1,JG
       IF(Q(I,J) .EQ. -9999.) Q(I,J)= 9999.
        WRITE(10, '(775F8.1)')(Q(I,J),I=1,IG)
       ENDDO
      CLOSE(10)
     
      WRITE(6,*)' ANOTHER DATA ? (Y/N)'
      READ(5,*) ANS
      IF(ANS .EQ. 'Y') GOTO 200
     
      STOP
      END
it seem this line not work, the output file still have -9999 value
Fortran:
IF(Q(I,J) .EQ. -9999.) Q(I,J)= 9999.
any suggestion?
thank you in advance
 
bou said:
it seem this line not work, the output file still have -9999 value
Fortran:
IF(Q(I,J) .EQ. -9999.) Q(I,J)= 9999.
any suggestion?
Don't use equalities with floating-point numbers. You have to allow for rounding errors.
 
  • Like
Likes   Reactions: jim mcnamara and bou
DrClaude said:
Don't use equalities with floating-point numbers. You have to allow for rounding errors.
thank you for your reply, i did try .LT. -1 but also same reuslt -9999 value not change.
 
I just realized that you are not looping over I. You will have to take the IF out of the print loop and have a double loop to change the values of Q.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
1
Views
3K
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K