NEAREST(3) - [MODEL:COMPONENTS] Nearest representable number
result = nearest(x, s)
elemental real(kind=KIND) function nearest(x,s)real(kind=KIND),intent(in) :: x real(kind=**),intent(in) :: s
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
NEAREST(3) returns the processor-representable number nearest to X in the direction indicated by the sign of S.
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.
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.
Sample program:
program demo_nearest implicit noneResults:real :: x, y x = nearest(42.0, 1.0) y = nearest(42.0, -1.0) write (*,"(3(g20.15))") x, y, x - y
! write (*,"(3(g20.15))") & ! 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 (*,"(3(g20.15))") & ! 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
> 42.0000038146973 41.9999961853027 .762939453125000E-05
Fortran 95
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
Nemo Release 3.1 | nearest (3fortran) | November 02, 2024 |