Created
August 31, 2014 09:16
Demonstration of various rounding/truncation methods in Fortran
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! 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