Re: Best way to compare CGFloats (Rick B.)

2010-06-30 Thread Frederick Bartram

I am stuck with floats (doubles, to be precise) comparison.


Machine 'real-numbers' such as floats and doubles should be thought of  
as intervals or neighborhoods near the mathematical number. Tests for  
'equality' of machine reals should never use machine equality '=='  
operators. 'Equality' of two machine reals , in this case, means that  
the two numbers are separated by a distance less than some small  
number, such as '(fabs(a-b)  epsilon)'. Exactly what the value of  
epsilon should be gets a little tricky.


Hope that I am not being too pedantic but imo you should never test  
for 'equality' ('==') when using machine real data types.


Rick B.
*-
* Stop spam before it gets to your mailbox! Support blocklists.
* I use http://www.spamcop.net/.
* my PGP key id: 0x63fa758 keyserver: http://keyserver1.pgp.com
*/

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Best way to compare CGFloats (Rick B.)

2010-06-30 Thread Alastair Houghton
On 30 Jun 2010, at 15:56, Frederick Bartram wrote:

 Hope that I am not being too pedantic but imo you should never test for 
 'equality' ('==') when using machine real data types.

Sometimes you really do want binary equivalence, and in that case == may be the 
right thing to use.  Normally, though, you're quite right, you need to be 
careful with floating point comparisons (not just equality tests, actually; 
sometimes for greater-than or less-than comparisons you might want two floats 
to be un-ordered if they're close in value).

I thought it'd be useful to link to the excellent What Every Computer 
Scientist Should Know About Floating-Point Arithmetic, since that's usually 
the best thing to give to people who are worrying about this kind of thing:

http://dlc.sun.com/pdf/800-7895/800-7895.pdf
http://docs.sun.com/source/806-3568/ncg_goldberg.html

Anyone who hasn't read it should.

Kind regards,

Alastair.

-- 
http://alastairs-place.net



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Best way to compare CGFloats (Rick B.)

2010-06-30 Thread Michael Ash
On Wed, Jun 30, 2010 at 11:11 AM, Alastair Houghton
alast...@alastairs-place.net wrote:
 On 30 Jun 2010, at 15:56, Frederick Bartram wrote:

 Hope that I am not being too pedantic but imo you should never test for 
 'equality' ('==') when using machine real data types.

 Sometimes you really do want binary equivalence, and in that case == may be 
 the right thing to use.

While we're being pedantic, note that == is not always the same as
binary equivalence. For example, 0.0 == -0.0, and x != x when x =
NAN.

Using == can make sense when you know that your values are exact
integers, which is not an uncommon scenario, but neither is it the
usual one.

Mike
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Best way to compare CGFloats (Rick B.)

2010-06-30 Thread Alastair Houghton
On 30 Jun 2010, at 17:37, Michael Ash wrote:

 While we're being pedantic, note that == is not always the same as
 binary equivalence. For example, 0.0 == -0.0, and x != x when x =
 NAN.

That's a good point.  I spoke inaccurately.

 Using == can make sense when you know that your values are exact
 integers, which is not an uncommon scenario, but neither is it the
 usual one.

It may also make sense sometimes with floating point values.  An example might 
be where you have an object that you're using as the key for an NSDictionary; 
in that case, your -isEqual: method may well want to do a proper equality test.

These are, however, special situations.

Kind regards,

Alastair.

--
http://alastairs-place.net




___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com