UniLogic INT/REAL conversion blocks

I don’t think the documentation for the Real to Int and Int to Real blocks in Unitronics UniLogic is very clear, so here’s my explanation:

Real to Int

r2i_REAL_in (Data type: REAL):
The real number to convert.
r2i_Precision_in (Data Types: UINT8, UINT16, UINT32):
Number of decimal places to add to the fractional part.
r2i_INT_out (Data Types: INT8, INT16, INT32):
this is the integer part of r2i_REAL_in, no matter what the value of r2i_Precision_in is.
r2i_frac_out: (Data Types: INT8, INT16, INT32):
This is the fractional part of r2i_REAL_in, rounded to the specified number of decimal places, multiplied by 10^( r2i_Precision_in ).

There’s a special case when r2i_Precision_in is 0 (ie. round to the nearest integer): when the magnitude of th fractional part is less than 0.5, it will be 0, otherwise it will be 1 or -1, (the same sign as r2i_REAL_in). To get the correctly rounded number, you need to add r2i_frac_out to r2i_INT_out

The behaviour when the number is too big to fit into either of the integers seems to be inconsistent. If r2i_INT_out is an INT32, then it will 2 147 483 647 if r2i_REAL_in is bigger than that, or -2147483648 if r2i_REAL_in is smaller than that. INT16s and INT8s sometimes overflow instead. (i.e. 128 becomes -128 if r2i_INT_out is an INT8), but it doesn’t do this for all values). I’d avoid using this function in this situation if at all possible.

Examples:

r2i_REAL_inr2i_Precision_inr2i_INT_outr2i_frac_out
123.4563123456
123.50011235
123.50001231
-0.0320-3
-12.753-12-750
-12.750-12-1

Int to Real

i2r_INT_in (Data Types: INT8, INT16, INT32):
Integer part of value. Not actually necessary, because you can do everything without it.
i2r_FRAC_in (Data Types: INT8, INT16, INT32):
This is the only way to populate it the fractional part of the output, but it will also contribute to the fractional part.
i2r_DECPOS_in (Data Types: UINT8, UINT16, UINT32):
This controls how i2r_FRAC_in is intepreted. Values above 7 result in the fractional part being ignored.
i2r_REAL_out (Data type: REAL):
The Real output value. This is calculated as:
i2r_INT_in + i2r_FRAC_in / 10 ^ ( i2r_DECPOS_in )

Examples

i2r_INT_ini2r_FRAC_ini2r_DECPOS_ini2r_REAL_out
1564563156.456000
045624.560000
13214215.140000
021474836477214.748400
0214748364780

Leave a Reply

Your email address will not be published.