www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/12/01/03:25:46

Date: Sun, 1 Dec 1996 10:03:58 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Francois Charton <deef AT pobox DOT oleane DOT com>
Cc: djgpp AT delorie DOT com
Subject: Re: Float code bug [Was: Re: Problems with DJGPP V2.01 - atof() function]
In-Reply-To: <32A0C63A.7D17@pobox.oleane.com>
Message-Id: <Pine.SUN.3.91.961201095635.12293X-100000@is>
Mime-Version: 1.0

On Sun, 1 Dec 1996, Francois Charton wrote:

> As explained in a previous message, there seem to be some trouble with 
> the way DJGPP handles casts from floats to ints.
[snip]
> Could anyone explain why is this? and how it should be fixed?

There is no point IMHO to dig too deep into this ``mystery''.  First, the 
original poster (not you, Francois) just didn't seem to realise that when 
a double or a float is cast or assigned to an int, it is truncated, so to 
get it rounded, you need to add a 0.5 before the assignment.  If you 
don't do that, you will sometimes get surprises, because FP math is never
exact on a computer.

Second, different sequences of FP operations *always* produce slightly 
different results (this is just one face of the inexactness of FP math).  
While there is always an explanation how and why do two results differ, I 
generally find it pointless to try to understand this to the utmost.  
Just remember that you never get an exact result and write your code 
accordingly, that's all.

(Btw, one of the reasons why the results differ is that, internally, x87 
uses a 80-bit representation of an FP number, so any load/store from/to 
memory will generally change the number.)

- Raw text -


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