Manual Reference Pages  - nint (3fortran)

NAME

NINT(3) - [TYPE:NUMERIC] Nearest whole number

SYNOPSIS

result = nint( a [,kind] )

         elemental integer(kind=KIND) function nint(a, kind )

real(kind=**),intent(in) :: a integer(kind=**),intent(in),optional :: KIND

CHARACTERISTICS

o a kind designated as ** may be any supported kind for the type
o A is type real of any kind
o KIND is a scalar integer constant expression
o The result is default integer kind or the value of KIND if KIND is present.

DESCRIPTION

NINT(3) rounds its argument to the nearest whole number with its sign preserved.

The user must ensure the value is a valid value for the range of the KIND returned. If the processor cannot represent the result in the kind specified, the result is undefined.

If A is greater than zero, NINT(A) has the value INT(A+0.5).

If A is less than or equal to zero, NINT(A) has the value INT(A-0.5).

OPTIONS

o A : The value to round to the nearest whole number
o KIND : can specify the kind of the output value. If not present, the output is the default type of integer.

RESULT

The result is the integer nearest A, or if there are two integers equally near A, the result is whichever such integer has the greater magnitude.

The result is undefined if it cannot be represented in the specified integer type.

EXAMPLES

Sample program:

    program demo_nint
    implicit none
    integer,parameter   :: dp=kind(0.0d0)
    real,allocatable    :: in(:)
    integer,allocatable :: out(:)
    integer             :: i
    real                :: x4
    real(kind=dp)       :: x8

! basic use x4 = 1.234E0 x8 = 4.721_dp print *, nint(x4), nint(-x4) print *, nint(x8), nint(-x8)

! elemental in = [ -2.7, -2.5, -2.2, -2.0, -1.5, -1.0, -0.5, -0.4, & & 0.0, & & +0.04, +0.5, +1.0, +1.5, +2.0, +2.2, +2.5, +2.7 ] out = nint(in) do i=1,size(in) write(*,*)in(i),out(i) enddo

! dusty corners ISSUES: block use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64 integer :: icheck ! make sure input is in range for the type returned write(*,*)’Range limits for typical KINDS:’ write(*,’(1x,g0,1x,g0)’) & & int8,huge(0_int8), & & int16,huge(0_int16), & & int32,huge(0_int32), & & int64,huge(0_int64)

! the standard does not require this to be an error ... x8=12345.67e15 ! too big of a number icheck=selected_int_kind(ceiling(log10(x8))) write(*,*)’Any KIND big enough? ICHECK=’,icheck print *, ’These are all wrong answers for ’,x8 print *, nint(x8,kind=int8) print *, nint(x8,kind=int16) print *, nint(x8,kind=int32) print *, nint(x8,kind=int64) endblock ISSUES

end program demo_nint

Results:

     >               1          -1
     >               5          -5
     >      -2.700000              -3
     >      -2.500000              -3
     >      -2.200000              -2
     >      -2.000000              -2
     >      -1.500000              -2
     >      -1.000000              -1
     >     -0.5000000              -1
     >     -0.4000000               0
     >      0.0000000E+00           0
     >      3.9999999E-02           0
     >      0.5000000               1
     >       1.000000               1
     >       1.500000               2
     >       2.000000               2
     >       2.200000               2
     >       2.500000               3
     >       2.700000               3
     >     Range limits for typical KINDS:
     >     1 127
     >     2 32767
     >     4 2147483647
     >     8 9223372036854775807
     >     Any KIND big enough? ICHECK=          -1
     >     These are all wrong answers for   1.234566949990144E+019
     >        0
     >          0
     >     -2147483648
     >      -9223372036854775808

STANDARD

FORTRAN 77 , with KIND argument - Fortran 90

SEE ALSO

AINT(3), ANINT(3), INT(3), SELECTED_INT_KIND(3), CEILING(3), FLOOR(3)

fortran-lang intrinsic descriptions (license: MIT) @urbanjost


Nemo Release 3.1 nint (3fortran) April 28, 2024
Generated by manServer 1.08 from d676aebe-bca2-43a4-9089-88d886a3e2d0 using man macros.