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

Need help understanding a line of code

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



Joined: 07 Feb 2008
Posts: 17
Location: Canada

View user's profile Send private message

Need help understanding a line of code
PostPosted: Thu Mar 27, 2008 9:19 am     Reply with quote

I'm currently in the middle of converting a C program written for another type of micro controller to PICC. Everything is going quite smoothly except that now I'm stuck at this line of code:

ucCount = abs(fAltitude % 10);

Ok I know that the abs() function computes the absolute value of a number, but what type of formula is fAltitude % 10 ? Confused

Perhaps it's the value of fAltitude * 10%...I'm not totally sure.

Can anyone enlighten me? Wink
timer0
Guest







PostPosted: Thu Mar 27, 2008 9:29 am     Reply with quote

var % 10
that's the module(remaining) of var/10
so for example:

N = 185
N/10 = 18
N%10 = 5

so (18*10) + 5 = 185
RageOfFury



Joined: 07 Feb 2008
Posts: 17
Location: Canada

View user's profile Send private message

PostPosted: Thu Mar 27, 2008 9:38 am     Reply with quote

timer0 wrote:
var % 10
that's the module(remaining) of var/10
so for example:

N = 185
N/10 = 18
N%10 = 5

so (18*10) + 5 = 185

Ah, ok now I understand. Thanx for clearing that up. Smile
newguy



Joined: 24 Jun 2004
Posts: 1903

View user's profile Send private message

PostPosted: Thu Mar 27, 2008 9:45 am     Reply with quote

If fAltitude is always positive, it can be done very quickly:

Code:
ucCount = (int16)fAltitude % 10;


I'm assuming that ucCount is int16 - if it is an 8 bit int, just change the cast to int8. If fAltitude isn't always positive, just use the CCS abs() function. However, if you "feed" abs() a float, it will return a float. You'll still have to cast the result to an int:

Code:
ucCount = (int16)abs(fAltitude) % 10;
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