Marten Posted May 7, 2020 Report Share Posted May 7, 2020 Hi, I've a routinewhitch add 0.1 every 100ms to variable counter(Real). And every time machine is idle it is reset to 0. When running this the counter value is not correct, it is 1.1001 9.9999 , but adding 0.1 and no other value this shouldn;t be possible. Now i add every 100ms a 1 to an integer, then convert it to real, then multiply *0,1, and this works, Link to comment Share on other sites More sharing options...
AlexUT Posted May 7, 2020 Report Share Posted May 7, 2020 Hi Marten, This is a Real rounding error when you make sequential operations. Use integer and convert it one-time is a right solution (like you doing). B.R. Link to comment Share on other sites More sharing options...
Marten Posted May 7, 2020 Author Report Share Posted May 7, 2020 Thanks for you answer, I would like to know more, I haven't seen this in for instance Omron or Siemens. Why is this happening? In general if I want to use a calculation where the output is being used and linked at the input, i cannot use this? What if we use Real on a measured value, what is max rounding error then? Link to comment Share on other sites More sharing options...
MVP 2023 Flex727 Posted May 7, 2020 MVP 2023 Report Share Posted May 7, 2020 There is no reason to use floating point numbers in this situation. No digital computer or PLC can handle floating point numbers perfectly and it's best to avoid them when possible. This is due to the conversion from decimal to binary and then back to decimal. Without infinite precision, which is of course impossible, there will be rounding errors which can be significant. In your case, use integers (add 1, not 0.1) and display the number with one decimal place. If you need to perform calculations, just keep track of where the decimal place is and make sure it matches on all the other numbers in the calculation. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now