Re: NSLog with %f and comparisons using ==
Re: NSLog with %f and comparisons using ==
- Subject: Re: NSLog with %f and comparisons using ==
- From: James Maxwell <email@hidden>
- Date: Sat, 11 Apr 2009 10:44:05 -0700
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...
cheers,
J.
On 11-Apr-09, at 10:38 AM, Greg Titus wrote:
If what you really want is to see whether the result is the same as
%f previously printed, then you could always make your array of
constants an array of constant _strings_ and then compare the
strings. Not terribly efficient, maybe, but straightforward.
- Greg
On Apr 11, 2009, at 10:26 AM, James Maxwell wrote:
oooh, damn... I was afraid someone was going to say that. I just
hoped there might be some way to force a float to conform to what
NSLog %f prints... That seems like it might be a useful function -
something like pround(aFloat), for "print-round", to force any
float number to round as the printf %f would round it. Anyway,
that's obviously not available.
Is there any C function that can compare within a given margin of
error? (I seem to remember seeing something like that, as some
point...??)
If not, no big deal - I'll just calculate the comparison within a
margin of error by hand.
thanks,
J.
On 11-Apr-09, at 10:15 AM, Michael Ash wrote:
On Sat, Apr 11, 2009 at 12:46 PM, James Maxwell
<email@hidden> 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 (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
_______________________________________________
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
_______________________________________________
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