Rajesh Anand

Conversion from Integer to float and vice versa

4 posts in this topic

Hello, I'm using RSLogix 500 application for LAD programming. I would like to convert the float data type to integer data type and vice versa.

I tried with the COP instruction as some had mentioned in other forums, but it was not working. So any suggestions on solving that issue would be helpful.

Share this post


Link to post
Share on other sites

MOV is an instruction that is used to move data from one location to another within a project. COP is used to move an array of data from one location to another. So, COP is just a more powerful MOV, so to speak. Unless you're moving an array of Floats or INTS, COP is not likely to be the instruction you're looking for. 

When you mix and match data types in math or move instructions, you're always going to get rounding or truncating errors. These can be somewhat accounted for using other code before or after the transition, assuming the process you're controlling can tolerate the loss of detailed data (especially when going from Float to INT). Could you give a more detailed explanation of the process and the variables you need to change?

In the mean time, here are a couple of generic ideas that come to mind:

To go from Float to INT, simply use the truncate (TRN) instruction. Write a line of code with a TRN instruction that uses the Float data as a source, and an N7 file as the destination. This will remove the decimal point values only, so it will not round the value of the original Float data up or down.

To go from INT to Float, I think you should be able to use the MOV instruction with an N7 (or other INT) as the source, and an F8 file as the destination. When the INT value first "lands" in the F8 file, it will have a value of *.000000, and the decimal values will change from zeros as the code then acts upon the new data in the F8 word. 

Hope this helps.

Share this post


Link to post
Share on other sites

Slight clarification: the COP instruction does a bit-level copy of the data with no conversion. Its length parameter is the number of destination elements to overwrite so if you're copying from a float (32 bit) to an integer (16 bit), you'll just get the first 16 bits of the float, which will be gibberish. If you COP the float to 2 16-bit integer registers, you'll have the full bit pattern to interpret, but it'll be gibberish as-is. The PLC won't decipher the bit pattern as a usable number with out a bunch of custom code in between.

Using the MOV instruction will interpret the numerical value of the data and convert it. As I recall, MOV instruction (and other math instructions) will round before storing the result in the integer register. You'll want to experiment with your processor to see how it handles the x.5 boundary case. Some may round up, others round to even. I'm pretty sure the 5000 class processors round to even, but I don't remember how the 500 class handles it. It's been a while since I worked with having to round data values in a 500.

Ultimately, the correct path depends on  your desired destination and what you're trying to do with the data. In general, it's best to avoid rounding any numbers until the very last step of the calculation to avoid accumulated rounding errors.

 

Share this post


Link to post
Share on other sites

MOV transfers data and tries to cast/convert/fit result into target variable format. this generally results in a data los due rounding etc. for example transfer of value 3.14 to an integer would produce rounded result 3 since integers cannot handle decimals..

COP transfers data without parsing - straight bit by bit copy. so hexadecimal value of input and output is exactly the same. but the interpretation would depend on type of variable used. for example 3.14 would be 16#4048 _F5C3 which in an integer variable would be 1078523331 

1 person likes this

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