forked from miladsade96/FCS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_08_d.f90
More file actions
54 lines (51 loc) · 1.69 KB
/
Exercise_08_d.f90
File metadata and controls
54 lines (51 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
! Created by EverLookNeverSee@GitHub on 6/6/20
! For more information see FCS/img/Exercise_08_d.png
function fact(n) result(factorial)
integer, intent(in) :: n
integer :: i
real :: factorial, temp
if (n < 0) then
factorial = -1.0 ! error
else if(n == 0) then
factorial = 1.0 ! factorial of zero
else
temp = 1.0
do i = 2, n
temp = temp * i
end do
factorial = temp
end if
end function fact
program main
implicit none
! x --> given value that we're gonna calculate its arcsin
! total --> sum of the elements in series
! previous --> sum of elements in previous step
! fact --> declaration of fact function
! Ea --> approximation error
! declaring and initializing variables
integer :: k = 0
real :: x = 0.5, total = 0.0, Ea, previous, fact
do
! Adding up first element of series -> `x` itself
if (k == 0) then
total = total + x
k = k + 1
else ! Calculating and adding up rest of elements of series
previous = total
total = total + (fact(2 * k) * x ** (2 * k + 1) &
/ (2 ** (2 * k) * fact(k) ** 2 * (2 * k + 1)))
Ea = total - previous
! Exit whenever approximation error is less than 10e-10
if (Ea < 10e-10) then
exit
else ! keep going to calculate and add up next element of series
k = k + 1
cycle
end if
end if
end do
! printing and comparing results
print *, "Total(aka arcsin):", total
print *, "Fortran intrinsic arcsin function:", ASIN(x)
end program main