• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Best way to compare CGFloats
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Best way to compare CGFloats


  • Subject: Re: Best way to compare CGFloats
  • From: Alastair Houghton <email@hidden>
  • Date: Wed, 30 Jun 2010 10:14:48 +0100

On 30 Jun 2010, at 09:35, Rimas M. wrote:

> So question is, what is the most efficient (performance side) and
> simplest (coding side) way to compare those bastards?
>
> At the moment I am thinking about:
>
> a) BOOL _floatsOrDoublesAreEqual = ( (float)C.x == (float)D.x);
>
> b) BOOL _floatsOrDoublesAreEqual = ( fabs(C.x - D.x) <= _epsilon ); ,
> where _epsilon is something like 0.0001.

fabs is the right way to do this in most cases, *but* with co-ordinate values it may be that you are actually more interested in the distance of one point from the other---i.e. you might in fact be interested in

  CGFloat dx = C.x - D.x, dy = C.y - D.y;
  BOOL pointsAreEqual = (dx * dx + dy * dy) < epsilonSquared;

It rather depends on what you want, and that's going to differ from one application to another.

Kind regards,

Alastair.

--
http://alastairs-place.net



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Best way to compare CGFloats
      • From: "Rimas M." <email@hidden>
References: 
 >Best way to compare CGFloats (From: "Rimas M." <email@hidden>)

  • Prev by Date: Re: Best way to compare CGFloats
  • Next by Date: CALayer-hosting view and child NSView's
  • Previous by thread: Re: Best way to compare CGFloats
  • Next by thread: Re: Best way to compare CGFloats
  • Index(es):
    • Date
    • Thread