# NEAREST

Elemental Intrinsic Function (Generic): Returns the nearest different number (representable on the processor) in a given direction.

result = NEAREST (x, s)

x | (Input) Must be of type real.
s | (Input) Must be of type real and nonzero.

## Results

The result type and kind are the same as x. The result has a value equal to the machine representable number that is different from and nearest to x, in the direction of the infinity with the same sign as s.

## Example

If 3.0 and 2.0 are REAL(4) values, NEAREST (3.0, 2.0) has the value 3 + 2 -22, which equals approximately 3.0000002. (For more information on the model for REAL(4), see Model for Real Data.

The following shows another example:

     REAL(4) r1
     REAL(8) r2, result
     r1 = 3.0
     result = NEAREST (r1, -2.0)
     WRITE(*,*) result           ! writes 2.999999761581421

     ! When finding nearest to REAL(8), can't see
     ! the difference unless output in HEX
     r2 = 111502.07D0
     result = NEAREST(r2, 2.0)
     WRITE(*,'(1x,Z16)') result  ! writes 40FB38E11EB851ED
     result = NEAREST(r2, -2.0)
     WRITE(*,'(1x,Z16)') result  ! writes 40FB38E11EB851EB
     END

Parent topic: M to N

## See Also

EPSILON