On Sat, Apr 11, 2009 at 12:46 PM, James Maxwell <jbmaxw...@rubato-music.com> wrote: > I've got a strange problem. > I have a list of float constants that I need to compare to the result of a > new calculation. > > I derived the constants by performing the calculation, and printing using > NSLog with %f, then just writing down the results in my array of constants. > So, the result of the calculation used to derive the float constants was run > through NSLog, using the %f token. > > Now, when I run the calculation "live" and try to compare to my stored > constants, I'm not getting matches. I'm assuming this is because the result > of the calculation *isn't* actually what I stored in my constants, since the > constants were rounded during the NSLog. So, how do I make sure my "live" > calculation returns a value that will be rounded in the same way as an > NSLog(@"%f", aFloat)? > > That's a confusing question, I know... I hope it makes sense to somebody.
Read "What Every Computer Scientist Should Know About Floating-Point Arithmetic": http://docs.sun.com/source/806-3568/ncg_goldberg.html But basically, never depend on floating point calculations to be exact. As a consequence, never compare floats or doubles for equality. Instead, compute a delta from the desired value, and see if the delta is within an acceptable range. 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