Tech Tip: Use SET REAL COMPARISON LEVEL for more exact comparisons
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: December 3, 2019
There are situations where more precision is required for mathematical comparisons. Take for the example comparing 1 against 0.9999999. The expected result should be that 1 is greater than 0.9999999 and the "Greater" alert will appear.
C_REAL($exactly_r;$almost_r) $exactly_r:=1 $almost_r:=0.9999999 Case of : ($exactly_r=$almost_r) ALERT("Equal") : ($exactly_r>$almost_r) ALERT("Greater") : ($exactly_r<$almost_r) ALERT("Less") End case |
However, the actual result is the "Equal" alert. Why is that?
4D takes the difference of two real numbers and compares it to this default epsilon value of 10^-6. If the difference is greater than 10^-6, the comparison returns not equal. Otherwise the comparison returns equal.
In the case above, the difference is 0.0000001 (or 10^-7). Since 10^-7 is less than the default epsilon value 10^-6, 1 and 0.9999999 are considered equal. To resolve this issue, the epsilon value must to be set to a value that is less than the difference. In this case, the command SET REAL COMPARISON LEVEL(10^-8) can be used for a more accurate comparison which now returns the correct alert "Greater".
Just be aware of this SET REAL COMPARISON LEVEL command when working with precise calculations.