Here is a mind-blowing program to find the fractional power of any number (no matter whether it is float or integer) without using any predefined function in C, or not including any header file that contains functions to perform this task.
For my previous post on finding fractional power visit this link.
One specific thing that one should keep in mind is that this program is working only for the cases when the power of number is having not more than 4 decimal digits.
Source Code :-
/********************************************************************************/
#include<stdio.h>
#include<math.h>
#include<conio.h>
long double roundoff(long double);
long double reduceit(long double num,long double counter,
long double z,long double i);
long double power(long double,long double);
long double cal(long double num,long double p)
{
long
double i=0,j,answer,counter=4,z=1,ans;
for(j=0;j<20;j++)
{
i=reduceit(num,counter,z,i);
z/=10;
}
p=(p*power(10,counter));
p=roundoff(p);
answer=power(i,p);
return
answer;
}
long double reduceit(long double num,long double counter,
long double z,long double i)
{
while(1)
{
if((power(i,power(10,counter)))
> num)
{
i=i-z;
break;
}
i=i+z;
}
return
i;
}
long double roundoff(long double num)
{
long
double limit;
limit=(int)num+0.5;
if(num>limit)
return
((int)num + 1 );
else
return
((int)num);
}
long double power(long double num,long double i)
{
long
double j;
long
double product=1;
for(j=0;j<i;j++)
{
product*=num;
}
return
product;
}
int main()
{
long
double num,p,a,b,pi,pf,answer;
clrscr();
printf("ENTER
ANY NUMBER : ");
scanf("%Lf",&num);
printf("ENTER
THE POWER : ");
scanf("%Lf",&p);
pi=(int)p;
pf=p-pi;
a=cal(num,pf);
b=power(num,pi);
answer=a*b;
printf("\n\nmy
answer = %Lf\n"
"co answer =
%f",answer,pow((double)num,(double)p));
getch();
return
1;
}
/*******************************************************************************/
The following output even gives an accurate result as compared to the pow() function:-
please explain a little bit on what your method does
ReplyDeleteFor god sake use comment line.
ReplyDelete