Value of natural log of a number

problem statement
Write a C program that reads a positive number x and computes an approximate value of ln x. The program should have two functions: int main() and double myLn(double x).
Do not use any mathematical library function or any array.
The iterative computation may be done 

ln(x)/2=(x-1)/(x+1)+(1/3)*((x-1)/(x+1))^3+(1/5)((x-1)/(x+1))^5+... so on

hence T(n)= y if n=1
                      
                      y^2T(n-1)   if n>1
#include<stdio.h>
#define M_LN10 2.30258509299404568402
double myln (double x);
int main()
{
  double n,y,x;
  int count;
  printf("enter the value of n whose lnx to be calculated\n");
  scanf("%lf",&x);
  n=x;
  if(n==0)
    {
      printf("- inf \n");
    }
  if(n<0)
    {
      printf("nan"  );
    }
  if(n==1)
    {
      printf("ln = 0 \n");
    }
  if(n>0&&n!=1)
    {
      count=0;
      while(n>1)
{
 n=n/10;
 count=count+1;
}
      y=(2*myln( n))+(count* M_LN10);
      printf("the approx value of ln%lf is %lf \n",x ,y);
    }
  return 0;
}

double myln(double n)
{
  double sum,term,m,oldsum,u,v;
  sum=(n-1)/(n+1);
  term=(n-1)/(n+1);
  m=2.0;
  do
    {
      u=(term*(n-1)*(n-1)*((2*m)-3));
      v=((n+1)*(n+1)*((2*m)-1));
      term=(u/v);
      oldsum= sum;
      sum= sum+term;
      m=m+1;        
    }
  while(sum!=oldsum);
  return sum;
}



No comments:

Post a Comment