Double.Equals question

I’m learning about Double.Equals, this is the example here:

Dim Pi As Double = 3.14159265358979323846264338327950 If Pi.Equals(22 / 7, 1) Then ' Close enough! Else ' Not close enough End If

But I can’t figure it out how to make it True. I always get False. I tried:

  • change 1 in Pi.Equals(22 / 7, 1) to other numbers
  • Dim Pi with less decimals

Can someone tell me how to make it report as Close enough?

you might be confusing what maxUps really is…
it does NOT compare the DIGITS in the number… it compares the BYTES in the internal representation of the number.

so it is not as straight forward as you might be thinking…

The problem is that that’s a poorly designed example. Pi and 22/7 are just too different. You would have to round to 2 or 3 decimal places to get them to be equal.

Yes I hoped for an easy way to compare doubles.

Thank you.

function CloseEnough( a as double, b as double) as boolean
const tolerance = 0.0001
return (abs(a-b) < tolerance)
end function

[quote=370654:@Alberto De Poo]Yes I hoped for an easy way to compare doubles.

Thank you.[/quote]
That’s what Double.Equals is for. It is an easy way to compare doubles. Unfortunately, the example in the documentation is wrong.

Thank you Jeff. Your code works, I just want to learn what Xojo offer. From what I read, your code and some math, I think is possible to create almost any function that we need to solve what we want.

Thank you Tim. The problem is that I don’t understand the example, so I can’t file a Feedback case. I have opened a couple (wrong documentation) and Paul fixed them quickly.

Edit: Paul is always looking to fix things :slight_smile:

I hate that! Can you suggest an example that would be correct and more appropriate?

It looks like for this function to really make sense and to be “useable” it would require an in-depth knowledge of the IEEE standard for Doubles.

  // we have 2 double values that vary by a tiny bit
  Dim d1 As Double = 3.1415926535897935
  Dim d2 As Double = 3.1415926535897933
  
  if d1 = d2 then
     // will not get here because they are not an exact match
  end
  
  if d1.equals(d2, 1) then
     // will get here because they are "close enough"
  end

I had the same experience that Alberto did at one point and finally retreated from this method which is, as S points out, not for the faint-hearted.

I think that this is a confusing topic and I think that the documentation should be updated to make this fact clearer. For one thing, the word maxUlps should be defined.

Maximum number of representable floating point that are allowed to be between the two values.

maxUlp: maximum Units in the last place

This might be enough to keep the non-experts who stumble across this method away.

The URL

might be included just to further point out this is not for amateurs. Nice discussion.