Fortran 올림/반올림

Fortran에서 real값을 integer로 변환하는 경우 int/nint/floor/ceiling 중 하나를 사용하는데 각각의 특성은 간략하게 아래와 같다.

! int : 내림
! nint : 반올림
! floor : 내림
! ceiling : 올림

! https://software.intel.com/en-us/node/526352
! https://software.intel.com/en-us/node/580895
! https://software.intel.com/en-us/node/526179
! https://software.intel.com/en-us/node/580418

   program     test_rounding
   use, intrinsic :: iso_fortran_env
   implicit none

      print "(a,i3)", "    int(+4.2) = ",     int(4.2D+0)
      print "(a,i3)", "   nint(+4.2) = ",    nint(4.2D+0)
      print "(a,i3)", "  floor(+4.2) = ",   floor(4.2D+0)
      print "(a,i3)", "ceiling(+4.2) = ", ceiling(4.2D+0)
      print "(a)", " "

      print "(a,i3)", "    int(+4.6) = ",     int(4.6D+0)
      print "(a,i3)", "   nint(+4.6) = ",    nint(4.6D+0)
      print "(a,i3)", "  floor(+4.6) = ",   floor(4.6D+0)
      print "(a,i3)", "ceiling(+4.6) = ", ceiling(4.6D+0)
      print "(a)", " "

      print "(a,i3)", "    int(-4.2) = ",     int(-4.2D+0)
      print "(a,i3)", "   nint(-4.2) = ",    nint(-4.2D+0)
      print "(a,i3)", "  floor(-4.2) = ",   floor(-4.2D+0)
      print "(a,i3)", "ceiling(-4.2) = ", ceiling(-4.2D+0)
      print "(a)", " "

      print "(a,i3)", "    int(-4.6) = ",     int(-4.6D+0)
      print "(a,i3)", "   nint(-4.6) = ",    nint(-4.6D+0)
      print "(a,i3)", "  floor(-4.6) = ",   floor(-4.6D+0)
      print "(a,i3)", "ceiling(-4.6) = ", ceiling(-4.6D+0)
      print "(a)", " "

   end program test_rounding

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s