Lahey/Fujitsu Fortran

NEAREST Function


The NEAREST function returns the nearest number of a given data type in a given direction.


NEAREST (x, s)


x is an INTENT(IN) scalar or array of type REAL.

s is an INTENT(IN) scalar or array of type REAL. It must be non-zero.


The result is REAL and of the same kind as x.

If both x and s are scalar, the result value is the distinct number nearest to x. in the direction indicated by the sign of s.


real(kind(1.e0)) :: r1=1.e0 real(kind(1.d0)) :: r2=1.d0 write(*,*) r1 ! writes 1.00000000 write(*,*) nearest(r1,1.) ! writes 1.00000012 write(*,*) nearest(r1,-1.) ! writes 0.99999994 write(*,"(3z10.8)") r1 ! writes 3f800000 write(*,"(3z10.8)") nearest(r1,1.) ! writes 3f800001 write(*,"(3z10.8)") nearest(r1,-1.) ! writes 3f7fffff write(*,*) r2 ! writes 1.00000000000000000 write(*,*) nearest(r2,1.) ! writes 1.00000000000000000 write(*,*) nearest(r2,-1.) ! writes 0.99999999999999999 write(*,"(z18.16)") r2 ! writes 3ff0000000000000 write(*,"(z18.16)") nearest(r2,1.) ! writes 3ff0000000000001 write(*,"(z18.16)") nearest(r2,-1.)! writes 3f7fffffffffffff