2 Integers to 1 Real

darkesha

Member
Join Date
Nov 2007
Location
Calgary
Posts
107
I am wondering if I can move one Integer value right of the decimal point and another Integer value left of the decimal point of same Real number.
These two Integers are adjacent in the array. One is Int[1] and it represent integer value and the other one is Int[2] and represents decimal value.

So how can I make a new Real tag type that has:

X,Y

WHere X is read from Int[1] and Y from[2]


I know it would work if I do CPT instruction and then divide Y by 10 + X, but I am not sure if in the process this number ever goes above 10. I know it shouldnt but our production is stalled and can not verify it.
Besides, I am intrigued by this problem and it's solution.

Thanks in advance.
 
It depends on what decimal value Int[2] represents, number of tenths, number of hundreths ... what?

Your conversion would be Int[1] + (Int[2] / base which Int[2] represents). If Int[1] is very large then you may not keep all the significant digits of Int[2]. You can't just merge the two INTs together (as you probably know). How is int infotmation in Int[1] and Int[2] gathered? That may give a clue.
 
Here is what I responded darkesha in response to a PM inquiry.

It kinda depends on what exactly is in Int[1] and Int[2].

If the number you want is say 12.75, and Int[1] has 12 and int[2] has 75, then

RealTag := Int[1] + (Int[2]/100.0)

Whether you divide by 10, 100, or 1000 depends on how many digits are after the implied decimal place.


If what you want is 12.75 and Int[1] has 16716
and Int[2] has 0 then the integers are a binary representation of a 32 bit IEEE754 float stored in two consecutive 16 bit integers. In that case, simply copying the bit pattern to a float will automatically make the conversion, ie

COP INT[1] RealTag 1

This post is a little different and clarifies the problem a little more.

The simplest solution is to use several statements.
If Y < 10 then Z:= X+(Y/10.0)
Else if Y < 100 then X:= X+(Y/100.0)
Else....

Enter the constants as 10.0, 100.0, etc with an explicit decimal to force type casting as a float.

But what happens if Int[1] is 12 and int[2] is 5 and you really want 12.05? Thats why its better to determine a fixed number of decimal places and stick with it.

Also you should be aware that you might not get exactly what you expect with this method. For example, if X is 12 and y is 1 then you won't get 12.1, you will get 12.09999 because the decimal fraction .1 cannot be represented as exactly 0.10 in a computer.
 
Last edited:
The simplest solution is to use several statements.
If Y < 10 then Z:= X+(Y/10.0)
Else if Y < 100 then X:= X+(Y/100.0)
Else....

Enter the constants as 10.0, 100.0, etc with an explicit decimal to force type casting as a float.

But what happens if Int[1] is 12 and int[2] is 5 and you really want 12.05? Thats why its better to determine a fixed number of decimal places and stick with it.

Also you should be aware that you might not get exactly what you expect with this method. For example, if X is 12 and y is 1 then you won't get 12.1, you will get 12.09999 because the decimal fraction .1 cannot be represented as exactly 0.10 in a computer.


Well machine is not running, but I imagine the decimal only goes up to 9, and then it turns integer up by 1. I can not verify it since we dont run until Monday, but if that's the case then I the simple CPT will be enough. If there is more then 1 decimal place then I will need one IF statement.
Lack of documentation and the way addressing is done will make me do reverse engineering again, but I got used to it. Plus you learn your machine inside and out that way.
 

Similar Topics

Is there a way in CX-Programmer to ad UINTs together with an instruction without a compiler warning. Ladder Rung: Warning: Instructions...
Replies
2
Views
1,240
So I'm looking at replacing some obsolete Prosoft boxes that currently convert DH+ to Ethernet. I have a logix chassis that already has a...
Replies
10
Views
2,898
I'm looking at program for a air compressor (SLC 04). I'm trying to figure out why I can't change a setpoint that's located in a N: address. The...
Replies
10
Views
4,012
I have to take a 16 bit N register and split some of the bits into separate integers. example: Bits 0-1 = integer with a value from 0-3 Bit 2 is...
Replies
22
Views
5,832
Hi, I am using a Phoenix Contact PLC and trying to use it to write a Floating point over Modbus. To do this I need to convert it to two 16 bit...
Replies
24
Views
6,743
Back
Top Bottom