Manual Reference Pages  - nearest (3fortran)

NAME

NEAREST(3) - [MODEL:COMPONENTS] Nearest representable number

SYNOPSIS

result = nearest(x, s)

         elemental real(kind=KIND) function nearest(x,s)

real(kind=KIND),intent(in) :: x real(kind=**),intent(in) :: s

CHARACTERISTICS

o X may be a real value of any kind.
o S may be a real value of any kind.
o The return value is of the same type and kind as X.
o a kind designated as ** may be any supported kind for the type

DESCRIPTION

NEAREST(3) returns the processor-representable number nearest to X in the direction indicated by the sign of S.

OPTIONS

o X : the value to find the nearest representable value of
o S : a non-zero value whose sign is used to determine the direction in which to search from X to the representable value.

If S is positive, NEAREST returns the processor-representable number greater than X and nearest to it.

If S is negative, NEAREST returns the processor-representable number smaller than X and nearest to it.

RESULT

The return value is of the same type as X. If S is positive, NEAREST returns the processor-representable number greater than X and nearest to it. If S is negative, NEAREST returns the processor-representable number smaller than X and nearest to it.

EXAMPLES

Sample program:

    program demo_nearest
    implicit none
    character(len=*),parameter :: g=’(*(g0,1x))’
    character(len=*),parameter :: ref=’(a,1x,*(g20.15,1x))’
    character(len=*),parameter :: lim=’(a,1x,*(g20.15,1x))’
    real                       :: x, y

write (*,g) ’The basics ...’

x = nearest(42.0, 1.0) y = nearest(42.0, -1.0) write (*,’(a,g20.15,a,g20.15,a,g20.15)’)’for 42 +’,x,’-’, y,’delta’,x-y

write (*,g) ’For reference ...’

write (*,ref) ’TINY ’,tiny(0.0) write (*,ref) ’HUGE ’,huge(0.0) write (*,ref) ’EPSILON ’,epsilon(0.0) write (*,ref) ’SPACING ’,spacing(tiny(0.0)),spacing(huge(0.0))

write (*,g) ’Tesing the limits ...’

write (*,lim) ’For TINY()’, & nearest(tiny(0.0),1.0), & nearest(tiny(0.0),-1.0), & nearest(tiny(0.0),1.0) -nearest(tiny(0.0),-1.0)

write (*,lim) ’For HUGE()’, & nearest(huge(0.0),1.0), & nearest(huge(0.0),-1.0), & nearest(huge(0.0),1.0)- nearest(huge(0.0),-1.0)

end program demo_nearest

Results:

       > The basics ...
       > for 42 +42.0000038146973 -41.9999961853027 delta.762939453125000E-05
       > For reference ...
       > TINY     .117549435082229E-37
       > HUGE     .340282346638529E+39
       > EPSILON  .119209289550781E-06
       > SPACING  .117549435082229E-37 .202824096036517E+32
       > Tesing the limits ...
       > For TINY() .117549449095213E-37 .117549421069244E-37
       > .280259692864963E-44
       > For HUGE()             Infinity .340282326356119E+39
       > Infinity

STANDARD

Fortran 95

SEE ALSO

DIGITS(3), EPSILON(3), EXPONENT(3), FRACTION(3), HUGE(3), MAXEXPONENT(3), MINEXPONENT(3), PRECISION(3), RADIX(3), RANGE(3), RRSPACING(3), SCALE(3), SET_EXPONENT(3), SPACING(3), TINY(3)

Fortran intrinsic descriptions