Friday 24 May 2013

CALCULATE FRACTIONAL POWER OF A NUMBER - A VERY EFFICIENT ALGORITHM

In one of my previous post I mentioned an algorithm, regarding the calculation of fractional power of a number. But that algorithm was not that efficient as I wanted and it even gave some great deviations for specifically large numbers. So on the request of some of my friends I tried hard and ended up with an amazing thing, I was now able to develop such a good algorithm that now this thing takes no time to calculate even big powers. You may find a large number of algorithms on other sites, but definitely you will not find any such similar algorithm with such a concept.
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:-


2 comments: