SINT / SINT into REAL - incorrect result

Biggles
2018-10-10
2018-10-10
  • Biggles - 2018-10-10

    Newbie Question!

    Hi all, I am returning to using PLC's after not having used them for some 20 odd years. My have things changed!
    I'm new to CodeSys and am writing a simple program in ST to learn the IDE and test my old skills.

    Clearly they're not very good.

    I am try to divide two declared signed integer variables and store in a REAL variable i.e.140 / 16
    The answer should be 8.75 but when I run the program in simulation the real variable shows only the 8 with no decimal place or anything thereafter.

    Can anyone help with what am I doing wrong please?

     
  • alwoso - 2018-10-10

    Hi Biggles!

    To get a REAL result, you have to do the calculation in REAL. So just convert the SINTs into REAL and do the calculation then:

    rResult: REAL;
    siInput1: SINT;
    siInput2: SINT;

    rResult:= SINT_TO_REAL(siInput1) / SINT_TO_REAL(siInput2);

    That should work!

    Alfred

     
  • Biggles - 2018-10-10

    Thanks Alfred!

    Embarrassingly I just realised the same just before you posted your kind reply!
    Its been a while and sometime it's a case of "If you don't use it.....you lose it!"

    Hopefully with more practice some of it will come back.

     

Log in to post a comment.