Skip to content

Converting real to 2 words (or one dword)

edited February 2019 in PLCnext Engineer

For example:

real_num := 123.456;

dword_var := REAL_TO_DWORD(real_num);

dword_var == 123

My question is how correctly convert my real to dword without losses.

Comments

  • Hello,

    If I understand your question correctly, you want to take the whole part of the real number and save that as a 32-bit integer, without the decimal part.

    In this case, the TO_DINT function should do this. This will only work properly if the whole part of the real number is in the range of a DINT (-2,147,483,648 to +2,147,483,647). The number will be rounded to the nearest integer and the decimal part lost, so this conversion won't be entirely without losses.

    By contrast, the TO_DWORD function only copies *bits* from the input to the output variable, so this (probably) does not do what you want.

    Does this help?

    - Martin.

  • Thank you for reply and sorry that i missed with description.

    The task it s convert REAL to 2 WORDS (or one DWORD) and reversly from 2 WORDS get one REAl basing on IEEE754.

    On other platforms it getting done by pointers to float, I would be very grateful if you tell something like this on pc worx

    Thank you,

  • Hello,

    The screen shot below shows the result of the two functions I mentioned.

    In this case:

    • real_num has the value 123.456 (rounded by the IDE)
    • dword_var is a DWORD (32-bits) containing the IEEE754 representation of the REAL value.
    • dint_var is the integer part of the real number.

    06 03 2019 13 04 08

    You can convert a DWORD containing the IEEE754 representation to a REAL value using the TO_REAL function (not shown here).

    - Martin.

  • Where I'm wrong? I have to get equals "wrong_dword" and "right_dword"


    Pic

    ("mant" here was got from "a" and "b" vars and it equals to right_dword, litterally all of this vars in debug must be equals, but i cant do this) 

  • Seems that i wrote in wrong subforum, anyway i would be very grateful if you help me 

  • OK, so you're using PC Worx 6.

    In this case, please look at the function REAL_TO_BUF, which should do what you want.

    For further questions on PC Worx 6, and for any other questions not related to PLCnext Technology, please contact the Phoenix Contact office in your country.

    - Martin.

Sign In or Register to comment.