You can move a REAL data type into a DINT data type using a MOV instruction and rounding will occur, but you need to be aware of how the Logix processor rounds the fractional part of a Floating Point value while converting to integer data types, or performing arithmetic operations on fractional Floating Point values.
For the Logix Platform, the REAL data type is of a 32 bit Single Precision Binary Floating Point format.
It consists of...
1 Sign bit (+/-) ¦ 8 exponent bits (whole number) ¦ 23 Mantissa bits (fractional part).
For any fractional part of Floating Point values above or below N.
5 the Logix processor will round UP or DOWN accordingly. But for any fractional part equal to N.
5 the Logix processor handles the rounding according to the default rounding method set out in the IEEE Standard for Floating Point Arithmetic (IEEE 754). This rounding method is simply known as "Round half to even", but has been called many things such as "Unbiased Rounding", "Gaussian Rounding" and even "Broken Rounding".
If a Floating Point value has a N.
5 fractional part, then the Logix processor will always round to the nearest EVEN number. This means that for an N.
5 fractional part the rounding is dependent on the whether the
N.X value is ODD or EVEN to begin with. This is very important to remember as normally one might expect an N.
5 fractional part to always round UP to the nearest WHOLE number.
If a Floating Point value has any other fractional part besides N.
5, such as N.
2, N.
8, N.
45, etc., then the Logix processor will always round to the nearest WHOLE number, whether UP or DOWN, ODD or EVEN.
So...
Any Floating Point value of N.
5 may round UP or DOWN to the nearest EVEN number depending on the WHOLE value being ODD or EVEN.
Any Floating Point value with a fractional part less than N.
5 will always round DOWN to the nearest WHOLE number.
Any Floating Point value with a fractional part greater than N.
5 will always round UP to the nearest WHOLE number.
Example 1: Move with N.
5 fractional part
MOV REAL 123.5 > DINT = 124 <<<Nearest EVEN number (Rounds UP)
MOV REAL 124.5 > DINT = 124 <<<Nearest EVEN number (Rounds DOWN)
Example 2: Move with non N.
5 fractional part
MOV REAL 123.6 > DINT = 124 <<<Nearest WHOLE number (Rounds UP)
MOV REAL 123.4 > DINT = 123 <<<Nearest WHOLE number (Rounds DOWN)
MOV REAL 123.45 > DINT = 123 <<<Nearest WHOLE number (Rounds DOWN)
Example 3: Move N.
5 fractional part either side of zero
MOV REAL 0.5 > DINT = 0 <<<Nearest EVEN number (Rounds DOWN)
MOV REAL -0.5 > DINT = 0 <<<Nearest EVEN number (Rounds UP)
The same principles apply when performing arithmetic operations on Floating Point values and storing them in non REAL data types such as SINT, INT and DINT integers.
Example 4: Addition - WHOLE number is EVEN
ADD REAL 10 + CONSTANT 8.5 > DINT = 18 <<<Nearest EVEN number (Rounds DOWN)
ADD REAL 10 + CONSTANT 7.5 > DINT = 18 <<<Nearest EVEN number (Rounds UP)
Example 5: Addition - WHOLE number is ODD
ADD REAL 9 + CONSTANT 8.5 > DINT = 18 <<<Nearest EVEN number (Rounds UP)
ADD REAL 9 + CONSTANT 7.5 > DINT = 16 <<<Nearest EVEN number (Rounds DOWN)
These are just some examples of perhaps unexpected rounding results from performing move and arithmetic operations on Floating Point values while storing them in integer values.
If you specifically need to round in a particular way other than the default, such as always DOWN, you can use these Add On Instructions from Rockwell to select which rounding method to use...
55148 - Round Up and Round Down Add-On Instructions for RSLogix5000
Access Level: TechConnect
Regards,
George