View previous topic :: View next topic |
Author |
Message |
RageOfFury
Joined: 07 Feb 2008 Posts: 17 Location: Canada
|
Need help understanding a line of code |
Posted: Thu Mar 27, 2008 9:19 am |
|
|
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 ?
Perhaps it's the value of fAltitude * 10%...I'm not totally sure.
Can anyone enlighten me? |
|
|
timer0 Guest
|
|
Posted: Thu Mar 27, 2008 9:29 am |
|
|
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
|
|
Posted: Thu Mar 27, 2008 9:38 am |
|
|
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. |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1909
|
|
Posted: Thu Mar 27, 2008 9:45 am |
|
|
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; |
|
|
|
|