Thursday, February 10, 2011

Fortran - problem with double precision.

I have a small program that read some data from binary file and stores it into normal (unformatted) files. Here is the source:

  Program calki2e
  IMPLICIT NONE
!
       DOUBLE PRECISION VAL
      INTEGER P,Q,R,S
    INTEGER  IREC2C
      PARAMETER( IREC2C=15000)
      INTEGER  AND,RSHIFT,LABEL,IMBABS,NX,IB,NFT77
      INTEGER  IND
      DIMENSION IND(IREC2C)
    DOUBLE PRECISION  XP
      DIMENSION XP(IREC2C)
      CHARACTER(LEN=12) :: FN77 = 'input08'
    CONTINUE
    NFT77=77
!----------------------------------------------------------------------
2   CONTINUE
c
    open(unit=NFT77,file=FN77,STATUS='OLD',
     +ACCESS='SEQUENTIAL',FORM='UNFORMATTED')
    open(unit=13,file='calki2e.txt')
    REWIND(77)
4100    continue
     READ(77) NX,IND,XP
       IMBABS=IABS(NX)
       DO 100 IB=1,IMBABS
            LABEL=IND(IB)
            P= AND(RSHIFT(LABEL, 24),255)
            Q= AND(RSHIFT(LABEL, 16),255)
            R= AND(RSHIFT(LABEL,  8),255)
            S= AND(       LABEL     ,255)
      VAL=XP(ib) 
            IF(P.EQ. Q) VAL=VAL+VAL                               
            IF(R .EQ. S)  VAL=VAL+VAL                                    
            IF((P .EQ. R).AND.(Q .EQ. S)) VAL=VAL+VAL 
    write(13,*)P,Q,R,S,val
100 CONTINUE
    IF (NX.GT.0) GOTO 4100
CRB
      CLOSE(UNIT=NFT77)
!
    END

When I compile it using gfortran I obtain double precision in output file but with g77 I get only single precision. What it wrong and how to change it?

  • your numbers are double precision but you are printing them in free format. You have to specify an explicit format

  • Hi

    I would be tempted to set the format on your write statement to something explicit, rather than use * in write(13,*)P,Q,R,S,val.

    Regards

    Mark

  • Do you mean the "write (13, *) statement. This is "list directed" output. It is a convenience I/O with few rules -- what you get will depend upon the compiler -- it is best used for debugging and "quick and dirty" programs. To reliably get all the digits of double precision, change to a formatted output statement, specifying the number of digits that you need. (It is probably best to switch to gfortran anyway, as g77 is no longer under development.)

    From M. S. B.
  • Thanks all, problem was solved.

    From Michał

0 comments:

Post a Comment