I decided to learn the fortran95 language (reason why is not important). However being a beginner I ran into a weird problem I really can't explain, therefor I need help.
I have the insertion sort algorithm:
subroutine insertion_sort_REAL4(array, array_len)
implicit none
!parameners
integer :: array_len
real (kind=4), dimension(array_len) :: array
!variables
integer :: i,key,hole_pos
do i = 0,array_len
key = array(i)
hole_pos = i;
do while ((hole_pos > 0.0) .and. (key < array(hole_pos - 1)))
array(hole_pos) = array(hole_pos - 1)
hole_pos = hole_pos - 1
end do
array(hole_pos) = key
end do
return
end
And there is the main program (excerpt):
real (kind = 4), dimension(3) :: x
x(1) = 3.1
x(2) = 4.3
x(3) = 5.4
write(*,*) 'Array = ',x
call insertion_sort_REAL4(x,3)
write(*,*) 'Array = ',x
The first write
statement prints out
Array = 3.09999990 4.30000019 5.40000010
Why have the numbers been slightly changed? Does fortran95 not use the IEEE754 standard by default?
But let's say I can live with the slight alteration; the second write
statements prints out
Array = 3.00000000 4.00000000 5.00000000
Why have the numbers been rounded up? It's really bugging me and formatting the 'write' statement isn't doing any good and the Google searches haven't really helped. I guess there is not that many things on the internet about fortran as it is of C. I am a decent C programmer so any parallels to it are appreciated. Thanks you for your help!