www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/30/08:41:32

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: <qb2z7BAkbVZzEwJA AT jenkinsdavid DOT demon DOT co DOT uk>
NNTP-Posting-Host: sable.ox.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019