Convert 2 diff type of int to real type

lior
2021-08-15
2021-08-17
  • lior - 2021-08-15

    Hi,
    I want to convert 2 diff INT datatype TO 1 Real datatype.
    for example:
    int1: int
    int2:int
    real3:real
    How Can convert int1&int2 TO Real3?
    Thanks

     
    • Ingo

      Ingo - 2021-08-15

      Do you mean...?

      real3 := TO_REAL(int2 - int1);
      

      Didn't understand what exactly you meant with "diff" type.

       
      • lior - 2021-08-15

        I have 2 different variable data types INT : 2 Bytes from the first var =INTA and 2 Bytes from the second var=INTB, and GLUE them to REALC-float

         
        • Ingo

          Ingo - 2021-08-15

          OK, then you need to glue them into an LINT or DWORD first. The "glueing" is actually a shift left, by 16Bit, and an OR of the two types. The shift is necessary to move the bits of the upper to bytes to the correct position. And the OR "merges" the two datatypes into one.

          With an implicite 32 bit intermediate value:

          real3 := TO_REAL(SHL(int2, 16) OR int1);
          

          And if you want to make it very explicit:

          dword4 := SHL(int2, 16);
          dword4 := dword4 OR int1;
          real3 := TO_REAL(dword4);
          
           
          • lior - 2021-08-15

            Can I use it like:
            TYPE Convert_2_int_to_byte :UNION

            i: WORD;
            c:WORD;
            bytes: ARRAY [0..3] OF BYTE;

            END_UNION
            END_TYPE

            TYPE Convert_Real_to_byte :UNION

            a:REAL;
            bytes: ARRAY [0..3] OF BYTE;

            END_UNION
            END_TYPE

             
            πŸ‘
            1
            • Ingo

              Ingo - 2021-08-17

              Hey! Very good idea.
              Sure, you can do it like that.
              I don't know why I didn't come up with that. Because, this is the most straight use-case for union πŸ˜‰

               
  • hermsen

    hermsen - 2021-08-17

    Opened 2 times the same question, once in Engineering, once here ;-)
    The Engineering thread is also UNION solution.

     

Log in to post a comment.