令人沮丧的代码在列表输入的第1项中给出了不好的真实数字

时间:2017-05-28 12:40:58

标签: fortran nan

我正在尝试使用下面的输入值运行以下代码。现在当我运行它我得到以下错误,我读到一些地方,这是由于我的2,txt文件看起来很乱,如果我不保存在UTF-16位,这是可能的吗?

At line 11 of file bndry.f (unit = 5, file = '2.txt')
Fortran runtime error: Bad real number in item 1 of list input

主要代码:

      IMPLICIT NONE
      INTEGER MS,NS,JS,N,I
      PARAMETER(MS=50000,NS=50000)
      REAL*8 S,K,TAU,SIGMA,R,DELTA,SMIN,SMAX,DTAU,ALPHA,BETA,LAM
      REAL*8 V(0:MS),BD(0:NS),T(0:NS)
      COMMON/OUTPUT/V,BD,T

      OPEN(UNIT=5, FILE='2.f')

      WRITE(6,*) S,K,TAU,SIGMA,R,DELTA,DTAU
      READ(5,*) S,K,TAU,SIGMA,R,DELTA,DTAU 


      N = TAU/DTAU
      ALPHA = 2.0D0*R/SIGMA**2
      BETA = 2.0D0*(R-DELTA)/SIGMA**2
      LAM = (BETA-1.0D0) + DSQRT((BETA-1.0D0)**2+4.0D0*ALPHA)
      LAM = LAM/2.0D0
      SMIN = K/(1.0D0+1.0D0/LAM)               ! PERPETUAL BOUNDARY
      SMAX = 10.0D0*K
      CALL EXP_DIFF(S,K,TAU,SIGMA,R,DELTA,SMAX,SMIN,DTAU,JS)

      WRITE(6,*) 'PRICE: ', V(JS)
      DO I = 0, N
       WRITE(2,10) T(I),BD(I)
      ENDDO
10    FORMAT(1X,2F14.8)

      STOP
      END
C=======================================================================
      SUBROUTINE EXP_DIFF(S,K,TAU,SIGMA,R,DELTA,SMAX,SMIN,DTAU,JS)
      IMPLICIT NONE
      INTEGER MS,NS,JS,M,N,I,J,IEARLY
      PARAMETER(MS=50000,NS=50000)
      REAL*8 S,K,TAU,SIGMA,R,DELTA,XMIN,XMAX,DTAU,DX,ALPHA,SMIN,SMAX,
     &       P1,P2,P3,VC,A,B
      REAL*8 VE(0:MS),V(0:MS),BD(0:NS),T(0:NS)
      COMMON/OUTPUT/V,BD,T

      IF (S.GT.SMAX) THEN
       STOP 'THE OPTION IS WORHTLESS'
      ENDIF

      IF (S.LT.SMIN) THEN
       STOP 'THE OPTION WORTHS K-S FOR CERTAIN'
      ENDIF

      XMIN = DLOG(SMIN)
      XMAX = DLOG(SMAX)
      DX = SIGMA*DSQRT(3.0*DTAU) 
      JS = (DLOG(S)-XMIN)/DX
      DX = (DLOG(S)-XMIN)/FLOAT(JS)
      ALPHA = R - DELTA - SIGMA**2/2.0
      P1 = SIGMA**2*DTAU/(2.0*DX**2) + ALPHA*DTAU/(2.0*DX)
      P2 = 1.0 - SIGMA**2*DTAU/DX**2
      P3 = 1.0 - P1 -P2
      P1 = P1/(1.0+R*DTAU)
      P2 = P2/(1.0+R*DTAU)
      P3 = P3/(1.0+R*DTAU)
      WRITE(6,*) 'P1,P2,P3',P1,P2,P3
      IF (P1.LT.0.0.OR.P2.LT.0.0.OR.P3.LT.0.0) STOP 'DECREASE DTAU'

      M = (XMAX-XMIN)/DX
      N = TAU/DTAU
      IF (M.GT.MS.OR.N.GT.NS) STOP 'INCREASE MS AND NS'
      DO J = 0, M
       VE(J) = MAX(K-DEXP(J*DX+XMIN),0.0)
       V(J) = VE(J)
      ENDDO

      BD(0) = K
      T(0) = 0.0      
      DO I = 1, N
       IEARLY = 0
       A = V(M)
       B = V(M-1)
       DO J = M-1, 1, -1
        VC = P1*A+P2*B+P3*V(J-1)
        IF (VC.LT.VE(J).AND.IEARLY.EQ.0) THEN
         BD(I) = DEXP(XMIN+J*DX)
         T(I) = DTAU*DFLOAT(I)
         IEARLY = 1
        ENDIF
        V(J) = MAX(VC, VE(J))
        A = B
        B = V(J-1)
       ENDDO
      ENDDO

      RETURN
      END

2.f文件中的数据:

S   =   100.0DO
K   =   100.0D0
TAU =   3.0D0
SIGMA   =   0.2D0
R   =   0.08D0
DELTA   =   0.04D0
DTAU    =   0.03D0

2 个答案:

答案 0 :(得分:1)

JS即将归零。然后你除以它。

为什么JS是一个INTEGER?

答案 1 :(得分:0)

您正在尝试读取变量的名称。输入文件可以正常工作:

100.0D0
100.0D0
3.0D0
0.2D0
0.08D0
0.04D0
0.03D0

第一行的最后一个是资本O.