On Sun, Jul 27, 2008 at 9:41 PM, Ashley Perrien <[EMAIL PROTECTED]> wrote: > What is the most common/accepted way of dealing with the inaccuracies of > floating point math? I'm trying to find out 1) Do 2 NSBezierPath lines > intersect and then 2) What is the point of the intersection? > > Given a couple points on a line I can find the intersection point but since > 2 line segments may not intersect, I then check: > if([lineOne containsPoint: intersectionPoint]) > > That will nearly always fail unless the math works out to very nice, neat, > round numbers. I've seen one possible workaround of multiplying everything > by 100 and then doing integer math but considering I've got some division > involved, not sure exactly if that would work. I guess I could just check > the values myself (if point1 is +- .001 of point2) but that seems kinda > hackish. > > What's the most common way of dealing with these types of problems?
Seeing if one number is equal to another number within some margin of error is very much not hackish, so long as you come up with the margin of error in a reasonable way rather than just picking a number out of the air. If you have an idea of just how much inaccuracy exists in your calculations then this is a fine way to do it. I recommend that you (and everybody else) read What Every Computer Scientist Should Know About Floating-Point Arithmetic: http://docs.sun.com/source/806-3568/ncg_goldberg.html It walks through the source of such errors, how to reduce them, and how to deal with them. It is probably far more than you ever wanted to know about floating point arithmetic, but it's all very good information. For this particular situation, though, I'd recommend ditching NSBezierPath altogether and doing your own line intersection code. You have two line segments defined by a total of four points, and you want to see if the two segments intersect. This is a fairly straightforward geometric operation, and you ought to be able to find a fair amount of code for it on the internet if you need it. You'll still suffer from inaccuracies, but they will be either very near misses which translate into hits, or very near hits which are considered misses. In *most* situations, having such close calls be occasionally wrong is entirely acceptable, since the inputs are also not 100% accurate, so either outcome is reasonable in those close cases. Mike _______________________________________________ Cocoa-dev mailing list ([email protected]) 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 [EMAIL PROTECTED]
