Mail Archives: djgpp/1997/04/30/08:41:32
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 <mert0407 AT sable DOT ox DOT ac DOT uk>
Merton College, Oxford
- Raw text -