CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

how to write the power equation

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
farizah



Joined: 26 Dec 2009
Posts: 5

View user's profile Send private message

how to write the power equation
PostPosted: Sun Jan 10, 2010 4:01 am     Reply with quote

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







PostPosted: Sun Jan 10, 2010 5:37 am     Reply with quote

The same as in any standard C.....
pow

Best Wishes
farizah



Joined: 26 Dec 2009
Posts: 5

View user's profile Send private message

PostPosted: Sat Jan 16, 2010 11:59 pm     Reply with quote

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







PostPosted: Sun Jan 17, 2010 3:58 am     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Tue Aug 31, 2010 10:54 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 31, 2010 11:36 pm     Reply with quote

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;
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group