Skip to content

Instantly share code, notes, and snippets.

@pozdneev
Created August 31, 2014 09:16
Show Gist options
  • Save pozdneev/6394bbd0d151c08ccad7 to your computer and use it in GitHub Desktop.
Save pozdneev/6394bbd0d151c08ccad7 to your computer and use it in GitHub Desktop.
Demonstration of various rounding/truncation methods in Fortran
! Demonstration of various rounding/truncation methods in Fortran
!
! ## Compilation line:
! gfortran -Wall -Wextra rounding.F90
!
! ## Output:
! a: -1.6 -1.4 1.4 1.6
! int(a): -1 -1 1 1
! nint(a): -2 -1 1 2
! ceiling(a): -1 -1 2 2
! floor(a): -2 -2 1 1
program rounding
implicit none
real, dimension(1: 4) :: a
character (len = *), parameter :: real_fmt = '(A12, 3X, 4F8.1)'
character (len = *), parameter :: intg_fmt = '(A12, 3X, 4I8)'
a(:) = (/ -1.6, -1.4, +1.4, +1.6 /)
write (*, real_fmt) 'a:', a
write (*, intg_fmt) 'int(a):', int(a)
write (*, intg_fmt) 'nint(a):', nint(a)
write (*, intg_fmt) 'ceiling(a):', ceiling(a)
write (*, intg_fmt) 'floor(a):', floor(a)
end program rounding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment