View previous topic :: View next topic |
Author |
Message |
farizah
Joined: 26 Dec 2009 Posts: 5
|
how to write the power equation |
Posted: Sun Jan 10, 2010 4:01 am |
|
|
I have another question, how to write mathematical equation of x power of y, in ccs compiler.
For example:
((A divide B)^C ) |
|
|
Ttelmah Guest
|
|
Posted: Sun Jan 10, 2010 5:37 am |
|
|
The same as in any standard C.....
pow
Best Wishes |
|
|
farizah
Joined: 26 Dec 2009 Posts: 5
|
|
Posted: Sat Jan 16, 2010 11:59 pm |
|
|
Thanks Ttelmah for helping me.
But is there another solution if I don't want to use the pow() method ? Because it is little bit slower. |
|
|
Ttelmah Guest
|
|
Posted: Sun Jan 17, 2010 3:58 am |
|
|
It is going to depend on a lot of things.
What the likely 'scale' of the numbers are, whether you are only using integers, etc. etc.
It is often quicker to simply perform multiplication.
Pow, is slow, becaue it is 'generic', and does the power, using log, and exp. Together these take the time of about twenty+ multiplications.
So if (for instance), you are only usng int32, and want a cube, it is much faster to just code this as:
Code: |
#define cube(x) (x*x*x)
|
Even if you want more flexibility in input number, so long as the likely powers are small, and are integer, a solution like:
Code: |
int32 ipow(int32 x,int8 y) {
int32 temp;
if (y==0) return 0;
if (y==1) return 1;
temp=x;
y--;
do {
temp*=x;
} while (y--);
return temp;
}
|
Will be far faster than pow....
Best Wishes |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Tue Aug 31, 2010 10:54 pm |
|
|
Ttelmah wrote: |
Code: |
int32 ipow(int32 x,int8 y) {
int32 temp;
if (y==0) return 0;
if (y==1) return 1; // return x;
temp=x;
y--;
do {
temp*=x;
} while (y--);
return temp;
}
|
|
There may be a typo. x^1 = x _________________ Read the label, before opening a can of worms. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Aug 31, 2010 11:36 pm |
|
|
y=0 is handled wrong as well. Should be correct this way
Code: | signed int32 ipow(signed int32 x,unsigned int8 y) {
signed int32 temp = 1;
while (y--) temp*=x;
return temp;
} |
|
|
|
|