# Easier way to round off Float Value in AB SLC CPU?

C

#### Chris Elston

Anyone know of an easy way to round to three decimals points using a Floating Point? I have a load cell reading into a F register. But I'd like to round to only three decimals. The value from the Analog card is giving me six decimals, but I'd like to round that to three places. I can't seem to think how to do it. Anyone got any great ideas? I know I could subract from 1, then find the difference, and subtract that value from the original. But I was wondering if there was a setting somewhere to round instead either in the analog card, or a function in the SLC I have never used.

Thanks,
Chris
http://www.mrplc.com

C

#### Chris Hale

I need to know a little more about the setup. What card are you using, what is the connection between load cell and Ai (4 - 20mA, 0 - 10V, etc).

Regards

Chris Hale
UK

B

#### Bob Hines

Why do you want to round to three decimal points?Are you going to display the value on an operator interface? If it's an issue of displaying on an operator interface, you should be able to program the it to display as many or a little digits as you desire.

I'm a bit confused. All analog cards that I know of convert an electical signal, whether it's voltage or current, into an integer value. The range of values you can obtain is dependent on the number of bits used by the analog card's analog to digital converter. For example, I believe most AB cards use a 12 bit converter which give you possible raw input values from 0 (i.e., 0V or 4mA) to 4095 (i.e., 10V or 20mA). Some other manufacturers have more or less
values (e.g., Simantic TI uses raw values between 6400 and 32000). In any case, the value is an integer so you must be converting it to a floating point value somewhere in your code. The floating point value is stored internally in the
PLC in either 4 or 8 bytes, depending on the standard used by the processor manufacturer. The respresentation of the floating point value is subject to error like any value in a computer and even decimal notation (e.g. you can't represent 2/3 precisely, it gets rounded to 0.6666667) So even if you stripped insignificant digits off the floating point value, it would still be converted
back to a 4 or 8 byte representation internally and could "re-acquire" insignificant digits.

So, if you need to display the value with only 3 decimal points, your best bet is to alter the way the analog value is displayed - not how the number is stored in the processor.

If you are concerned with the value "changing" rapidly, try using a low pass filter (e.g. Value to Display = 0.9 * Value to Display + 0.1 * Value from Analog Card) The two multipliers (i.e., 0.9 and 0.1) can be different so long as there sum is 1.0. Decreasing the first value and increasing the second value makes the "Value to Display" more responsive and vice versa. The two values can
be determined emperically and can depend on the scan rate and/or how frequently the "Value to Display" is calculated.

Hope this helps.

T

#### Terry DeWick

I would use SCP - Scale with parameters, you can change the input value into a useable ranged value, real easy to change 0 to 32000 into 0 to 100 percent.

J

#### Johan Bengtsson

Well, you could multiply it with 1000, add 0.5, convert it to integer, convert it back to float and divide by 1000. If that is the easiest solution in your case is something I don't know
but with the lack of any other idea I would go for that one.

/Johan Bengtsson

Do you need education in the area of automation?
----------------------------------------
P&L, Innovation in training
Box 252, S-281 23 H{ssleholm SWEDEN
Tel: +46 451 49 460, Fax: +46 451 89 833
E-mail: [email protected]
Internet: http://www.pol.se/
----------------------------------------

B

#### Blunier, Mark

Multiply by 1000, convert to int, convert back to float, divide by 1000. But this doesn't do exactly what you want, since floating point
numbers don't hold an exact number like integers do. 1/1000 in floating point could be stored as 0.00099999997
Mark

J

#### Joe Jansen

If it is a 5/03 or higher processor, use the CPT function. Example assumes that the input is on I:1.0 and the result goes to F8:1....

MUL I:1.0 1000 N7:1

DIV N7:1 1000

By storing the result of the MULtiply into an integer register, it automatically drops the decimal. Then DIVide it back out and drop it into

--Joe Jansen

www.plcarchive.com

C

#### Chris

Amazing input.

You guys never cease to amaze me.

The reason for the rounding is that there is a value lets say 1.775 is a good part. Anything BELOW that number is GOOD, and anything ABOVE

If I get reading of 1.775282, the PLC seems to assume no different using a limit test and FAILS that part, because it was above 1.775000, which in fact, by the customer SPEC, rounding is acceptable and this part would indeed FAIL per PLC standards when really it would PASS per
worldly standards of round up or down after or below "5". I the limit was only looking at three decimal places it would PASS at 1.775

Hope that clears up the WHY's.

I should just take the better part of your solution using the mathemical approach, as I already have done so. But as asked orginally, I
wondered if there was a setting I missed that could be easliy set to round three decimals in the analog card or a PLC function that I never
used. That's all I was asking, rather than writing hash and hack code to extract the information I really need, which can be done easily enough I suppose with a few extra rungs.

Thanks for the many tips and insides. Maybe after so of you read my "why's" you'll have another great idea...

Chris
http://www.mrplc.com

B

#### Bob Peterson

IF your fp number is less then 32.767 you can just multiply it by 1000, and move the result to an integer, then divide the integer by 1000 and move the result back to a FP. Examples:

10.000456 initial value
10000.456 X1000
10000 integer
10.000 converted value

The worst that would happen is that the last digit might be one off, but the level of accuracy of the value is likely not upto the level of precision so its unlikely that that one digit is all that important anyway.

10.000656 initial value
10000.656 X1000
10000 integer
10.000 converted value

Bob Peterson

J

#### Johan Bengtsson

As Jiri pointed out just compare to 1.7755 instead, as a side note most people have suggested multiplying with 1000, storing in an integer and dividing by 1000. This would probably NOT round the number however but rather cut the decimals. That is why you have to add those 0.5 I suggested - if you really would like to have the number rounded, however there is no need in this case... (just modify the compare value and you are right on track).

/Johan Bengtsson

Do you need education in the area of automation?
----------------------------------------
P&L, Innovation in training
Box 252, S-281 23 H{ssleholm SWEDEN
Tel: +46 451 49 460, Fax: +46 451 89 833
E-mail: [email protected]
Internet: http://www.pol.se/
----------------------------------------