Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Can't CodeSys count right?

k2teknik
2015-03-19
2015-03-21
  • k2teknik - 2015-03-19

    I have a strange problem, I have a variable, declared as a REAL.
    A line in my code goes like this:
    MyVarA:=MyVarA + 0.1;

    That should, in my world, gives:
    0.0
    0.1
    0.2
    0.3
    0.4
    0.5
    0.6
    0.7
    Aso.

    But for some reason it goes like:
    0.04
    0.14
    0.25
    0.35
    0.45
    0.55
    0.65
    0.75
    0.86

    What is going on?

     
  • shooter - 2015-03-20

    A real is not exact, so it is close but can have a difference in result.
    As the printing is not nicely rounded but just cut off, you can have strange results, i do expect more problems like is it a global var or do you do anything starnge with it, the only answer is when you send the complete program.
    counting is best done as an Unsigned Integer and the transfered to real.
    so your formula is better when adding one
    and then the result Int_to_real and div by 10

     
  • k2teknik - 2015-03-21

    shooter hat geschrieben:
    A real is not exact, so it is close but can have a difference in result.
    As the printing is not nicely rounded but just cut off, you can have strange results, i do expect more problems like is it a global var or do you do anything starnge with it, the only answer is when you send the complete program.
    It is a global var, and I just add 0,1 to it every time my water counter tells me to do so. First I was afraid that the switch in the water counter was bouncing due to the fact that I was counting more that the mechanically meter showed on the water counter, but when I looked into the var during execution, and I see this strange counting rhythm/gait and was thinking WTF is this?

    Zitat:
    counting is best done as an Unsigned Integer and the transfered to real.
    so your formula is better when adding one
    and then the result Int_to_real and div by 10
    This was my next approach, I will try that out.

     

Log in to post a comment.