delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/09/30/14:45:14

From: Martin Ambuhl <mambuhl AT earthlink DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Float and djgpp
Date: Wed, 30 Sep 1998 14:31:51 -0400
Organization: Nocturnal Aviation
Lines: 62
Message-ID: <36127917.767B2903@earthlink.net>
References: <6utkig$mef$1 AT nnrp1 DOT dejanews DOT com>
NNTP-Posting-Host: 1cust225.tnt9.nyc3.da.uu.net
Mime-Version: 1.0
X-Posted-Path-Was: not-for-mail
X-ELN-Date: Wed Sep 30 11:30:57 1998
X-Mailer: Mozilla 4.06 [en] (Win95; U)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

jc DOT laroche AT mailcity DOT com wrote:
> 
> Hi ,
> 
> A little programm where floats don't work very well..
> 
> #include <stdio.h>
> #include <stdlib.h>
> #include <float.h>
> #include <math.h>
> 
> main()
> {
> float cf3;
> cf3=97+0.04/100;
> printf("%f = %f ? \n",cf3,97+0.04/100);
> }
> 
> I use :  cc -lm
> 
> and the result is : 97.000397 = 97.000400 ?
> 
> Any idea or advise ???

Let's change this a little:

#include <stdio.h>

int main(void)
{
    REAL cf3;
    cf3 = 97 + 0.04 / 100;
    printf("%Lf = %Lf ? \n", 
       (long double)cf3, (long double)97 + 0.04 / 100);
    return 0;
}

Note the results:
gcc a.c -DREAL="float" -lm
a
97.000397 = 97.000400 ? 

gcc a.c -DREAL="double" -lm
a
97.000400 = 97.000400 ? 

gcc a.c -DREAL="long double" -lm
a
97.000400 = 97.000400 ? 
 
When you ask a limited data type to produce more digits of precision
than it has, you should expect the worse.

Notice that your error is .0000003/97.000400 or 1 part in
32,333,433.  The standard requires a float to have at least 6 digits of
precision, or a smidgeon worse than 1 part in 2,000,000.  You outdid the
standard by a factor of more than 16 and you still complain?


-- 
Martin Ambuhl (mambuhl AT earthlink DOT net)
Note: mambuhl AT tiac DOT net will soon be inactive

- Raw text -


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