Is there a standard way to check for Infinite and NaN in Fortran 90/95?

前端 未结 8 1189
小鲜肉
小鲜肉 2020-12-06 15:59

I\'ve been trying to find a standards-compliant way to check for Infinite and NaN values in Fortran 90/95 but it proved harder than I thought.

  • I tried to manua
8条回答
  •  自闭症患者
    2020-12-06 16:44

    I have used:

      PROGRAM MYTEST
      USE, INTRINSIC :: IEEE_ARITHMETIC, ONLY: IEEE_IS_FINITE      
      DOUBLE PRECISION :: number, test
      number = 'the expression to test'
      test = number/number
      IF (IEEE_IS_FINITE(test)) THEN
         WRITE(*,*) 'We are OK'
      ELSE
         WRITE(*,*) 'Got a problem'
      END IF         
         WRITE(*,*) number, test
      END PROGRAM MYTEST
    

    This will print 'Got a problem' for number = 0.0D0, 1.0D0/0.0D0, 0.0D0/0.0D0, SQRT(-2.0D0), and also for overflows and underflows such as number = EXP(1.0D800) or number = EXP(-1.0D800). Notice that generally, things like number = EXP(1.0D-800) will just set number = 1.0 and produce a warning at compilation time, but the program will print 'We are OK', which I find acceptable.

    OL.

提交回复
热议问题