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 CCS Technical Support

NEWBIE: Calculating percentages

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



Joined: 09 Apr 2010
Posts: 2

View user's profile Send private message

NEWBIE: Calculating percentages
PostPosted: Sun May 02, 2010 7:04 am     Reply with quote

I have a 2GB SD card with a byte size of 1967128576. Given the limitations of 32 bit arithmetic, does anyone know the best what to calulate percentages of this figure.

e.g. 100 / 1967128576 = A very small number which a 32 bit float cannot represent.

Any ideas would be most welcome!

Thanks
Ttelmah



Joined: 11 Mar 2010
Posts: 19506

View user's profile Send private message

PostPosted: Sun May 02, 2010 7:41 am     Reply with quote

It really depends how accurate you want the percentages to be.
If you look at the indicators on a PC for example, they normally only show the value to the nearest percent.
One solution, that is much quicker than fiddling with floats, is just to take your byte count, divide by 196713 in integer arithetmetic, and then print with:
Code:

int32 percent;
   percent=byte_count/196713;
   printf("%5.2LW",percent);

Which will give you effectively the percentage used, with 2 decimals. If you must go smaller, then use 196 or 197, and %8.5LW.

Best Wishes
spencerbray



Joined: 09 Apr 2010
Posts: 2

View user's profile Send private message

PostPosted: Sun May 02, 2010 7:59 am     Reply with quote

Great idea - many thanks!!
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