rounded off after the decimal point in "UINT_TO_REAL"

Runtime
Anonymous
2020-01-05
2020-01-07
  • Anonymous - 2020-01-05

    Originally created by: phenixrb

    Hello everyone ,

    I need help with rounding, in "UINT_TO_LREAL" conversions.
    how is it possible to recover the unrounded values as in the upper table?

    and why, all are not rounded, for example the value "1.22"?

    at best, I would like to be able to use values to 2 decimal after the decimal point, like the value "1.22".

    thank you.

    IMG: arrondis MCP3008.jpg

     
  • dkugler

    dkugler - 2020-01-05

    Originally created by: D. Kugler

    you can use the TO_DINT funktion to throw all decimals away, so before 100.0 afterwords /100.0:
    AIN01R:= TO_LREAL(TO_DINT(AIN01
    100.0)/100.0);

     
  • dFx

    dFx - 2020-01-06

    Actually there is a type inconsistency in your formula :
    LREAL:=UINT_TO_LREAL(UINT (I beg))/INT*REAL

    Then, REAL/LREAL are stored and interpreted (IEEE float format https://en.wikipedia.org/wiki/Double-precision_floating-point_format). That's why your values can't be exactly the same.

    If you need to get back your precise value, then I would suggest working with DINT and using scaling (*100) for you math.

    The type you need is called DECIMAL, but as far as I know, isn't supported in automation systems.

     
  • Anonymous - 2020-01-07

    Originally created by: phenixrb

    HI,I-Campbell

    thanks for your answer, that's what I was looking for.

     

Log in to post a comment.