delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/05/10/11:20:31

From: "A. Sinan Unur" <Sinan DOT Unur AT cornell DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: float
Date: Sun, 10 May 1998 11:08:44 -0400
Organization: Cornell University (http://www.cornell.edu/)
Lines: 61
Sender: asu1 AT cornell DOT edu (Verified)
Message-ID: <3555C2FB.E6530DFA@cornell.edu>
References: <6j2crt$i0$1 AT orudios DOT magnet DOT at>
NNTP-Posting-Host: cu-dialup-1606.cit.cornell.edu
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Elias Pschernig wrote:
> 
> I've a problem:
> I'm working on a game, which uses "float"-type for
> x- and y-coordinates of an object. Sometimes the
> object starts moving very, er, bulky, and i found out,
> this is the case, when either y or x coordinate go
> over 100000; the larger, the more bulky the movement.
> I'm now sure this has to do with the float-type, but
> it's very strange, because there occurs no other
> error, i.e. the position always is correct, even with
> large numbers, just it moves not smoothly any more.

it is in general not a good idea to use float or even double variables
for such purposes. floating point representations are usually not
necessarily exact, and addition is subject to large errors if you are
addin a very small number to a very large number. in essense, the
smaller number becomes indistinguishable from zero. note that this has
nothing to do with djgpp, but is a basic fact of life when you are
dealing with floating point arithmetic. as an example, you can try:

#include <stdio.h>

int main(void)
{
  int i;
  float sum1 = 0.0, sum2 = 0.0;
  float a[] = { 10e6, -10e6, 10e-2};

  for(i = 0; i < 3; i++)
    sum1 += a[i];

  for( ; i; i--)
    sum2 += a[i-1];

  printf("%g\t%g", sum1, sum2);

  return 0;
}

using doubles only delays the onset of the problem. you should keep
track of the coordinates in some integral type, and do conversions as
needed. for more on this, you can search for the document "what every
computer scientist needs to know about floating point arithmetic" on
altavista.

> 
> I would like it very much, if somebody could
> explain to me how DJGPP's float-type works.
> (also if it's the i86's float-type)

for the record, this is _NOT_ peculiar to djgpp.

-- Sinan
----------------------------------------------------------------------
A. Sinan Unur
Department of Policy Analysis and Management, College of Human Ecology,
Cornell University, Ithaca, NY 14853, USA

mailto:sinan DOT unur AT cornell DOT edu
http://www.people.cornell.edu/pages/asu1/

- Raw text -


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