View previous topic :: View next topic |
Author |
Message |
spencerbray
Joined: 09 Apr 2010 Posts: 2
|
NEWBIE: Calculating percentages |
Posted: Sun May 02, 2010 7:04 am |
|
|
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
|
|
Posted: Sun May 02, 2010 7:41 am |
|
|
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
|
|
Posted: Sun May 02, 2010 7:59 am |
|
|
Great idea - many thanks!! |
|
|
|