Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C18 precentage calculation

Status
Not open for further replies.

hhhsssmmm

Member level 1
Joined
May 14, 2009
Messages
37
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,732
Hello

I wish to calculate the precentage of an integer number in C18 compiler. I understand that floating point can be used. However I have read elsewhere on the internet that floating point arithmatic should be avoided as much as possible as its very CPU intensive...especially for PICs.

However, due to my limited knowledge I can not understand other than to simply use floating point.

Below are few lines of C18 code which show my understanding so far of how the precentage can be calculated.

Please can someone look at my code and correct it. Also please provide me feed back for any better and more efficient way of calculating the precentage of a number.

Thank you

Haseeb


Code:
     float precentage = 0; //declaring float variable

     precentage = (0x5555/0x8888) * 100.0; //calculating the percentage
 

I agree that it's a good idea to avoid floating point. Floating point hardware on the micro may speed things up dramatically. Check the datasheet of the micro to see if it has floating point hardware.

I suspect, the fragment you've posted will always return zero, because the integer division (0x5555/0x8888) is always zero.

If you want to higher efficiency, I would recommend something like this:
Code:
unsigned int	iPart = 67,		// 16-bit integers
				iWhole = 257,
				iPercentage;

iPercentage = (iPart * 100) / iWhole;
Notice that multiplication is donefirst. Keep in mind the possibility of overflow.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top