Logarithm in c++
#include<iostream>
#define log10 2.30258509
using namespace std;
double p(double x,int y) //power function
{
int i;
double res=1.0;
for(i=1;i<=y;i++)
{
res*=x;
}
return res;
}
int dton(double *b) //double to normalised to 10 form
{
int a = *b;
int bits = 0;
while (a != 0) {
bits = bits + 1;
*b = *b / 10;
a= *b;
}
*b=(*b)*10;
//cout<<"\n b in normalised form \n"<<b<<"* 10^"<<bits-1;
return bits-1;
}//end function
void dton1(double b) //double to normalised to 10 form
{
int a = b;
int bits = 0;
while (a != 0) {
bits = bits + 1;
b = b / 10;
a= b;
}
b=b*10;
cout<<"\n b in normalised form \n"<<b<<"* 10^"<<bits-1;
}//end function
void dton(double b) //double to normalised to 2 form
{
int a = b;
int bits = 0;
while (a != 0) {
bits = bits + 1;
b = b / 2;
a= b;
}
b=b*2;
cout<<"\n b in normalised form \n"<<b<<"* 2^"<<bits-1;
}//end function
double log(double x) //works good if x<=400
{
int i,m;
double t=(x-1)/(x+1);
double sum=0;
for(i=0;i<1000;i++)
{
m=2*i+1;
sum=sum+(p(t,m)/m);
}
return 2*sum;
}
int main()
{
double x;
char ch='c';
int bits;
while(ch=='c')
{
cout<<"\n enter x ";
cin>>x;
if(x>0)
{
bits=dton(&x);
// cout<<"\n log"<<x<<"base e is = \n "<<log1(x);
cout<<"\n log("<<x<<"*10^"<<bits<<") base e is = \n "<<(log(x)+(bits*log10));
}
if(x<=0)
{
cout<<"invalid input";
}
cout<<"\n \n press e to exit and c to continue";
cin>>ch;
}
return 0;
}
Comments
Post a Comment