Separating whole and mixed values

RickParrot

Member
Join Date
Nov 2015
Location
Iowa
Posts
68
Hi, all. I seemed to have hit a wall on my decimal to fraction forum. Therefore, anyone know how to separate a number like 17.33333 into 17 and .3333. I don't know what the returned value will be. But if it has decimal places, I need to separate the two. I can't round the whole number up. Even if the returned value is 83.9876, I cannot use 84 after the mixed number is divided up.
 
Subtract .5 from the number and use an integer as the destination.

17.33333 - .5 = 16.83333 this will be rounded to 17 when put in an integer.
83.9876 - .5 = 83.4876 this will be rounded to 83 when put in an integer.

Then take the results of the subtraction and subtract it from the orginal floating point number.

17.33333 - 17=.33333
83.9876 - 83 = .9876
 
Most systems have functions blocks or have math function in structured text.

The functions you are looking for are usually called "floor" and "fraction". There is also something called "ceiling" that is related.

If you don't have the fraction function you can do:
fraction = x - floor(x)

If you can't find the floor function maybe you have something called "trunc" which truncates the number. It's almost the same as floor but doesn't work for negative numbers.

.
 
Last edited:
Subtract .5 from the number and use an integer as the destination.

17.33333 - .5 = 16.83333 this will be rounded to 17 when put in an integer.
83.9876 - .5 = 83.4876 this will be rounded to 83 when put in an integer.


Then take the results of the subtraction and subtract it from the orginal floating point number.

17.33333 - 17=.33333
83.9876 - 83 = .9876

Logix5000 systems do not Round fractional numbers the same way as most other systems.

If the number to be rounded is exactly XXXX.5 (which you would get by subtracting .5 from a REAL that is XXXX.0), then Logix5000 rounds to the nearest EVEN number.

Examples

1.5 stored in an integer destination becomes 2
2.5 stored in an integer destination becomes 2
3.5 stored in an integer destination becomes 4
4.5 stored in an integer destination becomes 4

A better way to separate the number in Logix5000 is to use the MOD function.

MOD(17.33333, 1) = 0.33333
SUB(17.33333, 0.33333) = 17
 
Logix5000 systems do not Round fractional numbers the same way as most other systems.

That is just plain stupid but I'm not surprised. Excel couldn't implement the floor function correctly until 2010.

A better way to separate the number in Logix5000 is to use the MOD function.

MOD(17.33333, 1) = 0.33333
SUB(17.33333, 0.33333) = 17


I like the MOD 1 idea and while I use modulus all the time I've never thought of using it that way.

In some programming languages the modulus operator only works on integers though and not floating point.
 
An even easier way to get the whole number part is TRN (truncate).

As for the decimal, MOD will give you the decimal part on it's own as mentioned above. To get it into an integer as a whole number, decide how many decimal places you want it rounded to, multiply it by that power of 10, and truncate it again. For example, if you want four decimal places:

12.3456789

Truncate the original number and store it in DINT_1 (12)

MOD the original number and store it in Temp_Real_1 (0.3456789)

Multiply the original number by 10^4 (10000) and store it in Temp_Real_2 (3456.789)

Truncate Temp_Real_2 and store it in DINT_2 (3456)

DINT_1 = 12
DINT_2 = 3456

Of course, you could wrap all of this into one or two CPT instructions for simplicity, but at least this way spells out the process a bit more and makes it clearer what's happening it each step.

Also, as you'll have probably noticed, this method just takes the first however many decimal places and ignores the rest (truncates). If you need to round it - well, you're up against the "nearest even number rounding" issue mentioned above for a start. Rockwell do have rounding AOI's if you search the knowledge base for them, or with a little bit of thought you can build your own quite easily.
 
you can do this also with converting first from real to dint and then to real back (you have whole number without decimal)

For decimals, sub this whole number from original number.

If you need to cut decimals, you can do it with mul/convert/divide commands
 
That is just plain stupid but I'm not surprised. Excel couldn't implement the floor function correctly until 2010.

Stupid how?
Round-To-Even is very common, and it eliminates a tendency to "Round Up" an average on large data sets.

It is also symmetrical when rounding negative numbers.
 
Stupid how?
Round-To-Even is very common, and it eliminates a tendency to "Round Up" an average on large data sets.

It is also symmetrical when rounding negative numbers.

The absolutely most common rounding is round half up or round half away from zero. Which are the same, except for negative numbers.
Most programming language that has multiple ways of rounding has this as their default.

It's not stupid to offer something uncommon as an option, only stupid to offer it as the ONLY option. If you could change the default rounding method to something else that would work as well.

I don't think PLCs have to worry about large data sets because PLCs are not used for that.
 
Last edited:
...........I don't think PLCs have to worry about large data sets because PLCs are not used for that.

I believe it is about being IEC61311 compliant....

But whatever the reason, that's the way Logix5000 does it, and you are stuck with it, unless you want to investigate the AOIs.

It does mean that most common ways of dealing with REAL to DINT conversion will suffer at the XX.5 "knee", giving different results to other platforms.

Using MOD to get the fractional part as a DINT is the way to go, the destination tag should be a REAL, and you would MUL that to a DINT by 10, 100, 1000, etc., depending on how much precision you need.
 

Similar Topics

I am in process of implementing a new network to separate the manufacturing floor from the Enterprise network. See attached basic diagram. I have...
Replies
0
Views
933
i have a customer who is pushing back on something i have not encountered before. the cabinet is a local control panel consisting of PL, PB, HMI...
Replies
14
Views
4,522
Hello everyone ! Is there a way to get a variable, like a Word Memory (MW) and separate the integer part and the float part and save them in...
Replies
5
Views
1,601
I have heard a lot about separating the high voltage (>50V) from the lower control voltage in an electrical control panel, but have never run...
Replies
14
Views
7,206
Has anyone read a long 2D data matrix (200+ characters) and separated out the data in to different fields (~twenty)? I plan on adding a comma...
Replies
0
Views
1,387
Back
Top Bottom