calculating percents

Join Date
Apr 2002
Location
Just a bit northeast of nowhere
Posts
1,117
Does anybody know a clever way of using integers to calculate a percentage to display on an HMI, to the first decimal point?

The display I'm using (exor) can use a fixed decimal point, so an integer of 101 can be displayed as 10.1%

The best I've been able to manage is :

x = fail count
y = sum of all parts
z = integer

(x*1000) / y = z

example : (7*1000) / 19 = 368 (36.8%)

The problem? If you get more than 32 failures, the product of the first multiplication will overflow the integer and fault.

Yes, I know about long integers, ala AB, and double words on some other processors, but I want something general enough to be used anywhere, including processors without those features.

Besides, long integers have't been around that long, relatively-speaking. Somebody has to have found a way to do this before, using integers only.

Thanks!

TM
 
For your specific example, doing the 1000/19 first, then multiplying by 7 would get you further from the possibility of overruning a 16 bit int. Depending on the rounding model used in the processor you could end up with
52*7=364
or
53*7=371

Maybe this is close enought for your work, maybe not?
 
You can divide either the 'x' or the '1000' into 'n' number of parts do the math and then multiply back by 'n'.

IE.

x = fail count
y = sum of all parts
z = integer
n = number of divisions

((x*(1000/n)) / y)*n = z

example : ((7*(1000/4)) / 19) * 4 = 368 (36.8%)
 
Pretty good...

ND:

That's close, certainly closer than I was.

I wonder if there's a way to avoid the rounding, though? As you pointed out, there is 3/4 of a percentage point discrepency.


Icky:

That looks like just what I was after. I'll play around with it a bit and give it a shot. Thanks!


TM
 
Last edited:
Well, not that I know of, but of course I don't know it all by any means. ;) All of my other examples have more error than the one I gave you.

The trick with my example is to pick a divisor 'n' that will give you good resolution but avoid overflow. This is the problem with integer math, multiplying and dividing causes overflow more times then not. A person that is good with algebra can often avoid it, but sometimes you must sacrifice resolution.
 
Use your initial method for x values up to 32. For x values from 33 to 327, use the following:

(X * 100) / Y = Z1

(X * 100) MOD Y = Z2

(Z2 * 10) / Y = Z3

(Z1 * 10) + Z3 = Result

For an example of X = 37, Y = 125, actual percentage = 29.6

(37 * 100) / 125 = 29

(37 * 100) MOD 125 = 75

(75 * 10) / 125 = 6

(29 * 10) + 6 = 296
 
What Steve Bailey has described so well is the "Long Division" that we were taught oh so long ago.

Use your initial method for x values up to 32. For x values from 33 to 327, use the following:

(X * 100) / Y = Z1 is the quotient as a percent

(X * 100) MOD Y = Z2 is the remainder,

some PLC's don't have this function so the remainder will need to be calculated thus:

(X * 100) - (Y * Z1) = Z2

If you are working with Siemens S7 PLCs the remainder and quotient are convenientl;y placed in adjascent registers.

(Z2 * 10) / Y = Z3 10ths of a percent quotient

(Z1 * 10) + Z3 = Result as percent * 10

By processing the remainder of the Z3 equation,
the next place, 100ths, can be calculated etc...

For an example of X = 37, Y = 125, actual percentage = 29.6

(37 * 100) / 125 = 29

(37 * 100) MOD 125 = 75
- or -
(37 * 100) - (29 * 125) = 75

(75 * 10) / 125 = 6

(29 * 10) + 6 = 296
 

Similar Topics

This application has a motor with encoder feedback that drives a linear actuator that moves in/out, and is at roughly 45 degs from horiz. As the...
Replies
19
Views
1,361
I need to keep a running pass/fail yield of the previous 5,000 parts produced. I have used this formula before to calculate average: AvgValue =...
Replies
6
Views
2,151
Does anyone know how to calculate the savings from now needing to run an air compressor all day? Basically I have a design that replaced 6 * 1"...
Replies
26
Views
4,818
I would like to refer to this document of which I used some data ...
Replies
1
Views
1,472
Seems like this should be a simple thing but I’m struggling with it. The basis is that I am trying to detect if a linear assembly is stopped or...
Replies
6
Views
3,085
Back
Top Bottom