• 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
Best way to compare CGFloats
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Best way to compare CGFloats


  • Subject: Best way to compare CGFloats
  • From: "Rimas M." <email@hidden>
  • Date: Wed, 30 Jun 2010 11:35:32 +0300

Hello,

I am stuck with floats (doubles, to be precise) comparison.
In part of my app, I am dealing with NSPoint components (x, y)
comparison. On 32bit architecture NSPoint components are floats, and
direct comparison ( float1 == float2 ) works fine. But on 64bit, they
are doubles and '==' does not work due to precision.

Lets take an example: C and D are NSPoints. Lets say C is
(1.000000000000000444089209850063, whatever) and D is
(1.000000000000000000000000000000, whatever).
If my code is: if (C.x == D.x) thenDoSomething; , then thenDoSomething
is not called. Of course.

But on my needs, float is more than enough for precision.

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.

Regards,

Rimas M.
_______________________________________________

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: Alastair Houghton <email@hidden>
    • Re: Best way to compare CGFloats
      • From: Jean-Daniel Dupas <email@hidden>
    • Re: Best way to compare CGFloats
      • From: Roland King <email@hidden>
  • Prev by Date: Re: OpenAL and OGG
  • Next by Date: Re: NSImage drawing on 10.5 issue
  • Previous by thread: Re: How to retrieve a vCard during an ISyncSession?
  • Next by thread: Re: Best way to compare CGFloats
  • Index(es):
    • Date
    • Thread