Hello!
I have just written a fortran 90 program and willing to convert it into C language.
I have almost done it but when the fortran 90 program asks me to enter the upper limit, I enter the upper limit as 1 and the answer computed by the program is 0.3637185172 which is correct but when I enter the upper limit as 1 in the C conversion of the program ,the answer comes out to be 1.1741024473 which is wrong. I am posting the fortran 90 program and my attempt to convert it into C.
fortran 90 code:
Code:
PROGRAM integraltest
IMPLICIT NONE
INTEGER, PARAMETER :: num = SELECTED_REAL_KIND(15,160)
REAL(KIND=KIND(1.0d0)) :: zeta = 24.886266123440878231952771_num, frac(5), term, integral
REAL (KIND=KIND(1.0d0)) ::p
INTEGER :: v
INTRINSIC EXP
DO
write(*, '(A)' , ADVANCE = "NO") "Enter the Upper Limit : "
read(*,*) p
integral = (p**5)/5
integral = integral + zeta
v = 1
DO
frac(1) = p**4/(1.0_num*v)
frac(2) = (4_num*(p**3))/(1.0_num*(v**2))
frac(3) = (12_num*(p**2))/(1.0_num*(v**3))
frac(4) = (24*p)/(1.0_num*(v**4))
frac(5) = (24_num)/(1.0_num*(v**5))
! sum of fraction terms
term = (frac(1) + frac(2) + frac(3) + frac(4) + frac(5))
term = term*EXP(-1.0_num*v*p) !sum of fraction multiplied by the exp(-v)
integral = integral - term !substracting the terms from earlier computed value
IF(term < 1.0e-14) EXIT !exit if value good enough
v = v + 1 !next value for v
END DO
WRITE(*,'(I5, A31)') v, 'terms used in computing serie'
WRITE(*,'(A40, F100.10)') 'an approximation for the integral : ', integral
END DO
END PROGRAM integraltest
And here is my attempt to convert it into C Language:
Code:
#include<stdio.h>
#include<math.h>
int main()
{
double zeta = 24.886266123440878231952771 , a , b , c , d , e = 2.7182818284590452353602874713527 , f , term , integral , p , v ;
printf("Enter the upper limit : ");
scanf("%lf" ,&p);
integral = ((pow(p,5))/5);
integral = integral + zeta ;
v = 1 ;
while(1)
{
a = (pow(p,4))/v ;
b = (4*(pow(p,3))/(pow(v,2))) ;
c = (12*pow(p,2))/(pow(v,3)) ;
d = (24*p)/(pow(v,4)) ;
f = (24)/(pow(v,5)) ;
term = a + b + c + d + f ;
term = term*(pow(e,(-1*v*p))) ;
integral = integral - term ;
v = v + 1 ;
printf("\nAn approximation for the integral is : %.10lf\n\n",integral) ;
return;
}
}