inum0 Function

public function inum0(inline, ierr)

NAME

  inum0(3f) - [M_calculator] return integer value from calculator expression
  (LICENSE:PD)

SYNOPSIS

integer function inum0(inline,ierr)

character(len=*),intent(in)  :: inline
integer,optional,intent(out) :: ierr

SYNOPSIS

INUM0() evaluates a CHARACTER argument as a FORTRAN-like
calculator expression and returns an integer.

 o INUM0() uses the calculator routine CALCULATOR(3f)
 o Remember that the calculator treats all values as DOUBLEPRECISION.

Values returned are assumed to be very close to being whole integer
values. A small value (0.01) is added to the result before it is
returned to reduce roundoff error problems. This could introduce
errors if INUM0 is misused and is not being used to calculate
essentially integer results.

DESCRIPTION

inline  INLINE is a CHARACTER variable up to 255 characters long that is
        similar to a FORTRAN 77 numeric expression. Keep it less than 80
        characters when possible.
ierr    zero (0) if no error occurs

DEPENDENCIES

     All programs that call the calculator routine can supply their
     own substitute_subroutine(3f) and substitute_C(3f) procedures. See
     the ../html/Example.html">example program for samples.

EXAMPLES

Sample program:

   program demo_inum0
   use M_calculator, only : inum0
   implicit none
   integer :: i,j,k
      i=inum0('20/3.4')
      j=inum0('CI = 13 * 3.1')
      k=inum0('CI')
      write(*,*)'Answers are ',I,J,K
   end program demo_inum0

SEE ALSO

   The syntax of an expression is as described in
   the main document of the Calculator Library.

See CALCULATOR(), RNUM0(), DNUM0(), SNUM0(), EXPRESSION()

AUTHOR

John S. Urban

LICENSE

Public Domain

AUTHOR: John S. Urban

VERSION: 19971123

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: inline
integer, intent(out), optional :: ierr

Return Value integer


Source Code

integer function inum0(inline,ierr)

! ident_16="@(#) M_calculator inum0(3f) resolve a calculator string into a whole integer number"

!  The special string '*' returns -99999, otherwise return 0 on errors
character(len=*),intent(in)  :: inline
integer,optional,intent(out) :: ierr
!-----------------------------------------------------------------------------------------------------------------------------------
integer,parameter            :: IBIG=2147483647               ! overflow value (2**31-1)
integer                      :: iend
real,parameter               :: SMALL=0.0001                  ! and epsilon value
doubleprecision              :: dnum1
character(len=iclen_calc)    :: cdum20
integer                      :: ierr_local
integer                      :: ilen
!-----------------------------------------------------------------------------------------------------------------------------------
ierr_local=0
if(inline.eq.' ')then                                      ! return 0 for a blank string
   dnum1=0.0d0
elseif(inline.eq.'*')then                                  ! return -99999 on special string "*"
   dnum1=-99999d0
else                                                       ! parse string using calculator function
   iend=len(inline)
   call expression(inline(:iend),dnum1,cdum20,ierr_local,ilen)
   if(ierr_local.ne.0)then
      dnum1=0.0d0
   endif
endif
if(present(ierr))then
   ierr=ierr_local
endif
!-----------------------------------------------------------------------------------------------------------------------------------
! on most machines int() would catch the overflow, but this is safer
if(dnum1.gt.IBIG)then
   write(*,g)'*inum0* integer overflow 2**31-1 <',dnum1
   inum0=IBIG
elseif(dnum1.gt.0)then
   inum0=int(dnum1+SMALL)
else
   inum0=int(dnum1-SMALL)
endif
!-----------------------------------------------------------------------------------------------------------------------------------
end function inum0