Contact Us

If you still have questions or prefer to get help directly from an agent, please submit a request.
We’ll get back to you as soon as possible.

Please fill out the contact form below and we will reply as soon as possible.

  • Contact Us
  • Home
  • Products
  • Automation Systems
  • Machine Automation Controllers

Comparing REAL values as EQUAL

It is often hard to get two REALS to be exactly the same, hence are not EQUAL

Written by Toby Kilroy

Updated at February 26th, 2022

Contact Us

If you still have questions or prefer to get help directly from an agent, please submit a request.
We’ll get back to you as soon as possible.

Please fill out the contact form below and we will reply as soon as possible.

  • Products
    Automation Systems Control Components Motion & Drives Quality control & Inspection Robotics Safety Sensing Services Software Switching Components
  • Amplify e-Store
    e-Store public
  • Public CSC
+ More

REAL datatypes are very useful in programming. They allow us to store large and small values and include a useful amount of precision. There are REALS (4 byte) and LREALS (8 byte) which vary in the precision and range of values they can represent.

However when working with REAL data types it is important to remember that they are an approximation of a number. As such, they are often not quite equal to the number we expect, but they are so close, that the error between our expected value and the actual value is so small as to be inconsequential. 

However when comparing two REAL values, we sometimes don't get the result we expect. This is because for computers, 1.99999999 is not equal to 2. For all intends and purposes it is, the error is so minute we do not care. But computers, including Machine Controllers care, these two values are NOT equal.

To get around this problem, we often compare a number to another number using an upper and lower bound of precision. For instance we can determine if two numbers are practically equal by the following code. The fixed value of 0.0001 can easily be a precision level we pass to a Function which does this comparison and returns TRUE if realVal1 is practically equal to realVal2

equal:= ((realVal1 + 0.0001) > realVal2 AND (realVal1 - 0.0001) < realVal2);

 

Was this article helpful?

Yes
No
Give feedback about this article

Related Articles

  • Setting up a Routing Table for a NJ and CJ1W-DRM21 DeviceNet Master
  • Common Batteries Used in OMRON Appliances
  • Do I need Power Supply units for my NX CPU?
  • Major or Minor Revision mismatch for EtherNet/IP Error 0116

Definition by Author

0
0
Expand