I don’t understand the documentation about Double.Equals. The documentation says that “maxUlps” gives “the number of units in the last position that are used to denote the acceptable range”. What is the “last position”? Is this the least significant digit? What are “units”? In the example where d = 10000, it says that d.Equals(x,1) will return true if 10000.000000000002, 10000.0 and 9999.999999999998. That looks like 2 in the least significant digit, not 1. In the other example, PI is compared to 22/7 and the implication is that this will return true, but these numbers are different long before the last digits, so it should return false. Can anyone give some insight about this?

Maybe “last position” means the last bit of the fraction of the binary representation of the value.

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.1415926535897932 // last byte value is 0x18

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”.

Thanks for the responses. Tim, your response makes some sense. But this does not completely help me decide what value to give to MaxUlps. It seems to me that a series of floating operations might make a value many bits off in the last byte. Any advice on how to meaningfully choose this value?

I find this post, if somewhat long, does a good job explaining ULP

https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

Personally, I prefer the concept of “equal to N decimal places”, but that’s a relatively expensive calculation.

+1

I thought about comparing strings to be able to set the number of digits, but str gives such erratic results pass 14 digits, it could not do better than Equals. And ToText returns only 14 digits, which seems to be the threshold of Equals with zero maxUlps.

This is what i do to compare equality:

Function IsEqual(a As Double, b As double, N As UInt8) As Boolean

Dim div As Double = Val(“0.”+Left(“0000000000000000000000000”,N)+“1”)

If Abs(Abs(a) - Abs(b)) <=div Then

Return True

Else

Return False

End If

End Function

call the function with this example:

Dim a As Double = Val(textfield1.Text)

Dim b As Double = Val(TextField2.Text)

Dim n As UInt8 = Val(TextField3.Text)

MsgBox str(IsEqual(a,b,n))

Why the use of the Abs function within the parenthesizes?

I read the post that Shank pointed to on the meaning of ULP.

I got confused when the topic of zero gets explained.

Can someone who has read that article and feels reasonably comfortable with the article and with Xojo tell me:

Using Xojo, for someValue where someValue is a Double

If I use the expression someValue.Equals(0,5) going to return true for all values that are “close” to zero or do I have to “worry” because ULPs differences for values close to zero can be large.

This is done to compare just the number without the sign:

0.00000000000001 and -0.00000000000001 are in fact the same number when comparing the number but not the value. To calculate the difference between the 2 numbers you need the absolute value when subtracting them from each other. This example will give true in my function, if however the second number = -0.000000000000015 it will give as result ‘true’ up to N=10 but for N=11 it will be ‘false’.