Equals
From Xojo Documentation
Tests whether two Single or Double numbers are equal within a specified tolerance.
Usage
result = expression.Equals(numValue, maxUIps)
Part | Type | Description |
---|---|---|
expression | Single or Double | The numeric expression that your are testing against NumValue. |
maxUlps | Int32 | Maximum Units in the last place.
The number of units in the last position that are used to denote the acceptable range in the test of equality. The default is 1. If you pass zero, then the test is the same as with =. |
numValue | Single or Double | The numeric expression that Expression is being compared to. |
Result | Boolean | True if Expression and NumValue are equal within the tolerance specified by MaxUlps. |
Notes
Use Equals rather than = when you need to determine whether two floating point numbers are close enough in value to be considered “equal.” This can be used to account for the imprecision of floating point division on computers, for example. It allows for a user-specified rounding error.
For maxUIps, the last position refers to the the last byte in the binary representation of the mantissa. maxUIps is the amount of difference between the last byte of the 2 numbers that is still acceptable. For example, consider these 2 numbers:
3.141592653589795 // last byte value is 0x1C
A maxUIps of 3 will result in "not equal", while a maxUIps of 4 will result in "equal".
More information: https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
Sample Code
Compare two values:
Var d1 As Double = 3.1415926535897935
Var d2 As Double = 3.1415926535897933
If d1 = d2 Then
// Will not get here because they are not an exact match
End If
If d1.Equals(d2, 1) Then
// Will get here because they are "close enough"
End If
See Also
= operator; String.Compare method.