Troubleshooting Data Types
What follows is a list of problems that can occur when you use intrinsic data types.
Floating-point values do not compare as equal
When you use floating-point numbers (Real32, Real64, Real, and Real128), know that they are stored as binary fractions.
They can hold an accurate quantity only when it is a binary fraction with
k / (2 ^ n), where
n are integers.
0.5 = 1/2 and
0.1875 = 3/16.
Thus these are accurate values.
0.3 = 3/10 and
0.15 = 3/20, thus these are approximate values.
Because of this, you cannot be sure of accurate values as a result of floating-point operations. You cannot think that two given values will be equal, although mathematics shows that they are.
To compare floating-point values, follow these steps:
- Select a satisfactory maximum tolerance for the given comparison. All values in this range will become equal.
Toldirectly after the end of the comparison expression.
- Follow that with the selected tolerance value.
This can be a floating-point literal or a declared constant (See
See Tol Clause for more information.