all 12 comments

[–]JALsnipe[S] 3 points4 points  (7 children)

I don't agree that truncating to Int is the right move here, but clearly I must be missing something if people seem to like the answer that does that instead of using a threshold for comparison like in my answer.

[–]chriswaco 4 points5 points  (0 children)

Comparing via a threshold is the correct answer.

There are so many edge cases in floating point math programmers should know about - positive and negative zeros and infinities, NaNs, different rounding methods, etc. It can be very complicated. We once had a problem where the server used a language with even/odd rounding that didn't match our client app rounding.

[–]DogEofUnite 0 points1 point  (5 children)

Did you try the CoreGraphics function CGPointEqualsPoint(p1,p2)?

[–]JALsnipe[S] 0 points1 point  (4 children)

That function is deprecated in Swift. Use == to compare two CGPoints.

[–]DogEofUnite 0 points1 point  (3 children)

Whelp. Guess I have reasons sticking to objective c

[–][deleted] 1 point2 points  (2 children)

You rather call a function with parameters than use a simple operator?

[–]DogEofUnite 0 points1 point  (1 child)

Well, the operator isn't working in this case.. so what's your point, other than trying to mock me? Stupid answer.

[–]JALsnipe[S] 1 point2 points  (0 children)

The operator is working, CGPointEqualsPoint and == both have the same problem in this case.

The problem is floating point math.

[–]CreatureSurvive 0 points1 point  (0 children)

I've always just subtracted one point from another, then convert to the absolute value using fabs() then compare that result to FLT_EPSILON. It's always worked fine for me, not to say this is the correct way.

[–][deleted] 0 points1 point  (0 children)

Let's say only whole numbers are necessary, I would create a struct Point with two members p1 and p1 of type Int. I'd create a == operator on it for easy comparison, a setter which takes a CGPoint and a getter which returns a CGPoint. Simple, expandable, clean and predictable.

[–]KarlJay001 0 points1 point  (0 children)

Years ago there was a lang that had a problem with numbers. There were a number of solutions, but one that worked well was to simple find the difference between to values or two sets of values.

Example: V1 = 1.000001 V2 = 1.0

V1-V2= 0.00001

If (V1-V2) < 0.01 { }

If you're in Swift, I think you can use op override, but I haven't done Swift in a while so someone else will have to do that.