Path: utzoo!attcan!uunet!cs.utexas.edu!usc!snorkelwacker!apple!agate!usenet From: deadman@garnet.berkeley.edu (Ben Haller) Newsgroups: comp.sys.mac.programmer Subject: Re: Why can't the Mac add? Message-ID: <1990Sep28.013600.598@agate.berkeley.edu> Date: 28 Sep 90 01:36:00 GMT Sender: usenet@agate.berkeley.edu (USENET Administrator) Organization: Stick Software Lines: 24 This topic is getting really old, but one more thing: I've seen several people so far say that the "correct" version of the loop for (x = 0.0 ; x != 10.0 ; x += 0.2) is for (x = 0.0 ; x < 10.0 ; x += 0.2) Now I don't claim to know much about floating point stuff, but it seems to me that the internal representation of 0.2 could be either slightly greater or slightly less than 0.2 (the reason why it isn't *exactly* 0.2 has been made quite clear, I think). If the value used as 0.2 is slightly less than 0.2, this loop will execute 51 times. If the value used is slightly greater, this loop will execute 50 times. This is obviously wrong. What you need to do is either use an epsilon value, like: for (x = 0.0 ; abs(x - 10.0) > 0.1 ; x += 0.2) or you need to use a constant that is not a multiple of 0.2 to test against: for (x = 0.0 ; x < 9.9 ; x += 0.2) (or, as has already been mentioned, there are various ways of doing the equivalent loop with integers, although they are all slower than a pure FP loop like the last one above) Or am I wrong? -Ben Haller (deadman@garnet.berkeley.edu)