Created
April 9, 2020 23:15
-
-
Save jasonknight/075dcdabeb5a5b29717db8d8c804bddb to your computer and use it in GitHub Desktop.
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
program simple | |
implicit none | |
real a | |
real b | |
write(*,fmt='(a)',advance='no') 'Set a to: ' | |
read*, a | |
write(*,fmt='(a)',advance='no') 'Set b to: ' | |
read*, b | |
write(*,*) 'sq(a)=',sq(a),'sq(b)=',sq(b) | |
write(*,*) 'average(a,b)=',average(a,b) | |
write(*,*) 'improve(a,b)=',improve(a,b) | |
write(*,*) 'absolute(-2.5)=',absolute(-2.5) | |
write(*,*) 'good_enough(25.0,5.000001)=',good_enough(25.0,5.00001) | |
write(*,*) 'heron(a,b)=',heron(sq(a) + sq(b),1.) | |
contains | |
real function sq(n) result(x) | |
real n | |
x = n * n | |
end function sq | |
real function average(a,b) result(x) | |
real a,b | |
x = (a + b) / 2. | |
end function average | |
real function improve(n,g) result(x) | |
real n,g | |
x = average(g,(n/g)) | |
end function improve | |
real function absolute(n) result(x) | |
real n | |
x = n | |
if ( n .lt. 0. ) then | |
x = n * (-1.) | |
end if | |
end function absolute | |
integer function good_enough(n,g) result(x) | |
real n,g | |
x = 0 | |
if ( absolute(sq(g) - n) .le. 0.001 ) then | |
x = 1 | |
end if | |
end function good_enough | |
real recursive function heron(n,g) result(x) | |
real n,g | |
x = g | |
if ( good_enough(n,g) .eq. 0 ) then | |
x = heron(n,improve(n,g)) | |
end if | |
end function heron | |
end program simple |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment