delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/23/05:42:37

Sender: tim AT ipi DOT ac DOT ru
Message-ID: <34F15244.613633AF@ipi.ac.ru>
Date: Mon, 23 Feb 1998 13:41:08 +0300
From: "Timofei V. Bondarenko" <tim AT ipi DOT ac DOT ru>
MIME-Version: 1.0
To: "Alfredo J. Cole" <ajc AT acyc DOT com>
CC: djgpp AT delorie DOT com
Subject: Re: Round off errors
References: <01bd3e0c$200881c0$LocalHost AT alfredoc>

Alfredo J. Cole wrote:
> 
> Compiling the following code with DJGPP gives the erroneous result
> (5551.20). The same code compiled with Borland's bcd class gives the
> correct result (5551.22). I am developing an accounting system with
> DJGPP and it requires that the results be exact. 

Yes, it is because type 'float' contains only 20bit mantisse.
Somewhere in <float.h> (or <math.h>) you can see declaration
FLT_DIG - the precesion of float in decimal digits
For ieee compliant 32 bits float it will be only 6, so your is
quite  predictible.

> Is there a similar
> class or option under DJGPP that will eliminate these round off
> errors?

Use 'double': it will be faster and increase precesion up to 
15 decimal digits (DBL_DIG).

OR try to use fixed-point arythmetic - it will works like this:
int val = 555122;
printf("val = %d.%02d\n", val / 100, val % 100);

regards

tim.

- Raw text -


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