I have been wracking my brains trying to get a PLC-5/40E processor using RSLogix5 7.10 to truncate a floating point number into an integer.
For those that don't know, when converting a float to a integer, PLC5 (at least the enhanced and newer versions) have round to the nearest even number. This plays out as follows.
4.0 -> 4
3.5 -> 4
3.0 -> 3
2.5 -> 2
2.0 -> 2
1.5 -> 2
1.0 -> 1
0.5 -> 0
0 -> 0
-0.5 -> 0
-1.5 ->-1
-2.0 ->-2
My application was converting a 32bit unsigned integer to a float and back. I am trying to convert a 32 bit unsigned integer to a float and back. I don't want rounding, just truncate. Some people call this a floor function as well.
CPT to the rescue!
I didn't realize you could put MOD commands in CPT blocks. It isn't listed in the CPT documentation, but it worked.
To get the fraction part of a real number use the following in a CPT block.
CPT F8:1 F8:0 MOD 1.0
Then subtract the faction from the original real, and send it directly to a integer
SUM F8:0 F8:1 N7:0
Any comments ?
Hope this helps.
Mark Sluser
Calgary, AB
Oops posted a mistake, subtract is SUB not SUM.
Then subtract the faction from the original real, and send it directly to a integer
SUB F8:0 F8:1 N7:0