Sign in to follow this  
Followers 0
Mark Sluser

Truncate Float to Integer for PLC5 Enhanced

8 posts in this topic

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

Share this post


Link to post
Share on other sites
You can get the same results by subtracting 0.5 from the real, and then moving to an integer, may be less processor intensive. 4.0 - 0.5=3.5 > 4 3.9 - 0.5=3.4 > 3 3.2 - 0.5=2.8 > 3 Edit: I've never used it with negative numbers, so I can't say for sure how that works out.

Share this post


Link to post
Share on other sites
Are you sure you just need to subtract 0.5? 3.0 - 0.5 = 2.5 Because PLC-5 has round to nearest even, the result of rounding 2.5 is 2.0, not 3.0. In the case of 4.0 - 0.5 = 3.5 Rounding 3.5 rounds to the nearest even, the result is 4.0 so it works this time. Please get back to me, I would like to know if I am wrong! -Mark

Share this post


Link to post
Share on other sites
HMMM, I just tried it and you are right. I also used 3.000001 and it rounded to 3. I have been using this method for years and never knew this or had any problems. So, what are the odds of having exactly 3.0000000 with floating point numbers.Not likely I think, but interesting.

Share this post


Link to post
Share on other sites
The problem for me was using floating point numbers for data converstion. I have bit of logic that converts a 32bit unsigned integer (stored as 2 integers) to a float, and then back to a 32bit unsigned int. I am not interested in rounding, just data conversion so the problem had to be solved. Thank you for your help PLC Moderator. At least I can say I am not totaly crazy. -Makr

Share this post


Link to post
Share on other sites
An easier solution I found was to round the float to an int using a regular move block. Then compare the float to the new int. For positive numbers: If the new int is greater than the original float, the number rounded up If the new int is less than the original float, the number rounded down. For negative numbers, the exact opposite. So for positive numbers, if there was a rounding up, subtract 1. For negative numbers, if there was a rounding down, add 1. Cheers -Mark

Share this post


Link to post
Share on other sites
Thanks for the post I am sure others will use it.

Share this post


Link to post
Share on other sites
Ken was close. You can use something like .4999 and it should work. For negative numbers, use a LES instruction to check sign and add the .4999 instead.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0