delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/03/06/06:43:44

From: Frodo Baggins <l41273 AT alfa DOT ist DOT utl DOT pt>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Floating point accuracy
Date: Thu, 6 Mar 1997 10:29:27 +0000
Organization: Instituto Superior Tecnico
Lines: 26
Message-ID: <Pine.OSF.3.91.970306102523.3822A-100000@alfa.ist.utl.pt>
References: <5ff7e6$974 AT cronkite DOT ocis DOT temple DOT edu>
NNTP-Posting-Host: alfa.ist.utl.pt
Mime-Version: 1.0
In-Reply-To: <5ff7e6$974@cronkite.ocis.temple.edu>
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp


On 3 Mar 1997, David Tucker wrote:

> I have a question.
>=20
> When I do a calculation such as:
>=20
> =09X =3D a / b; // where int a =3D 994 and int b =3D 1000
> =09=09or
> =09cout << "X =3D " << a / b << endl;
>=20
> my result is 0.
>=20

You are doing the operation with ints! If you divide 994 by 1000 you will=
=20
get 0.994  .... now try to cast that to (int) and see what you get, you=20
will get zero, because that=B4s the way computers truncate!

You should then do like this:

 X =3D (float)a / (float)b  // and X MUST be a float or double=20

Hope it helped!  :)

- Raw text -


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