From: mert0407 AT sable DOT ox DOT ac DOT uk (George Foot) Newsgroups: comp.os.msdos.djgpp Subject: Re: Floating point bug??? Date: 30 Apr 1997 10:17:24 GMT Organization: Oxford University, England Lines: 29 Distribution: world Message-ID: <5k767k$j15@news.ox.ac.uk> References: NNTP-Posting-Host: sable.ox.ac.uk To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk David Jenkins (me AT jenkinsdavid DOT demon DOT co DOT uk) wrote: : for (d = 0; d < 600; d++) { : if (current < target) : current += SPEED; : if (current > target) : current -= SPEED; : if (current == target) : target = rand() % 100; [snip] The chances of target ever equalling current are extremely remote, given the granularity of floating point numbers and the fact that each addition introduces rounding errors - although you've set up current and target so that target is a multiple of current, the rounding errors will throw current off ever so slightly, meaning it is never strictly equal to target. What I would suggest is to define a certain region around target, say the interval between target-SPEED/2 and target+SPEED/2 and replace current==target with (abs(current-target)<=SPEED/2). There is still perhaps the possibility that current could go from (target-SPEED/2-delta) to (target+SPEED/2+delta) where delta is a really small number, and then your test would still fail, but this is either highly unlikely or impossible. -- George Foot Merton College, Oxford