Re: NSLog with %f and comparisons using ==
Re: NSLog with %f and comparisons using ==
- Subject: Re: NSLog with %f and comparisons using ==
- From: Greg Guerin <email@hidden>
- Date: Sat, 11 Apr 2009 12:02:18 -0700
James Maxwell wrote:
Yeah, that's an idea... But I do need it to be fairly efficient.
I've solved the problem by calculating fabs(val1 - val2) and only
allowing a very small margin of error (0.000001). It seems to be
working. If this is still going to be disaster-prone, I'd
appreciate a quick heads-up. But keep in mind, the floats on the
right-hand side of the comparison are constants, so I only need to
deal with any imprecision on the left side...
You haven't said what your range of values is. Remember that floats
are numbers with a floating decimal point, not numbers with a fixed
decimal point. If your margin of error is a fixed number like 1e-6
(0.000001), but the range of valid values goes as low as 1e-5, then
that's a 10% margin of error. Worse, if the valid values can be less
than 1e-6 in magnitude, and 'float' can certainly represent those
with its full precision, then your margin of error exceeds the
magnitude of your value.
I highly recommend reading "What Every Computer Scientist Should Know
About Floating-Point Arithmetic", or at least glancing through it. I
then recommend seeking out a reliable reference or code sample that
explains exactly how to do a proper "within a margin of error"
calculation. Spending the time to find reliable tested code will
likely pay off in the long run.
You also might want to post some of the actual constants you printed
out and then re-entered manually. One of the cardinal rules for
getting good answers is to be specific.
Finally, it's unclear to me if you're using the 'float' type or the
'double' type but referring to them both as "float", because %f
actually formats a 'double'. It's conceivable that simply increasing
the precision or format of the NSLog'ed print-format string will
improve things. E.g. %.9e or %.19e instead of %f.
http://developer.apple.com/documentation/Cocoa/Conceptual/Strings/
Articles/formatSpecifiers.html
http://www.opengroup.org/onlinepubs/009695399/functions/printf.html
-- GG
_______________________________________________
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