From: no_mail_from AT use DOT net (Brian Chapman) Newsgroups: comp.os.msdos.djgpp Subject: Re: Qustion on the casting of float's to int's... Message-ID: References: <199904131434 DOT QAA03789 AT acp3bf DOT physik DOT rwth-aachen DOT de> X-Newsreader: MicroPlanet Gravity v2.10 Lines: 35 NNTP-Posting-Host: 206.53.100.87 X-Trace: news15.ispnews.com 924041137 206.53.100.87 (Tue, 13 Apr 1999 18:05:37 EDT) NNTP-Posting-Date: Tue, 13 Apr 1999 18:05:37 EDT Date: Tue, 13 Apr 1999 17:07:17 -0500 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Ahh! I see. I'll be sure to watch my step from now on. Thank you very much! :-) Previously, broeker AT physik DOT rwth-aachen DOT de says... > You've just encountered a pitfall that surprised many beginners, > before they learn how floating point numbers actually work, and what > their limitations are. The basic problem is that '0.2' is not exactly > representable by *any* floating point value, in a PC. Instead, you'll > get something like 0.1999999.... or 0.20000001... In the case at > hand, it's the first kind. > > Adding up several of these values, you'll see that you won't ever > reach 1.0 exactly. Instead, you get 0.999999999. Because the FPU has > some hidden extra precision, the problem actually does not happen > around a sum of 1.0, but only when it's reached 5.0 or so. > > This whole issue is summed up in a nice quote: > > In computers, 10 times 0.1 is hardly ever 1.0 > > > i'm still new to C, but what is the compiler (gcc 2.8.1) doing??? > > why does (int)2.0=2 yet (int)8.0=7??? I have always assumed in the > > back of my mind that typecasting floats to ints just rounded down to > > the nearest whole number (ie: truncated the fraction). > > Typecasting does that. But *printing* doesn't truncate, it really > rounds. The real floating point value is 7.9999999.... which will be > printed as '8.0', but the truncated value is 7. > > > -- > Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de) > Even if all the snow were burnt, ashes would remain. >