Floating point: just 20 digits of precision

Axel Freyn axel-freyn@gmx.de
Fri Nov 26 15:03:00 GMT 2010

Hi Elias,

On Fri, Nov 26, 2010 at 10:25:29AM -0300, Elias Gabriel Amaral da Silva wrote:
> Hello,
> This simple program
> #include <stdio.h>
> int main()
> {
>        long double c, d, e;
>        c = 1.0/7.0;
>        d = 1.0; d /= 7.0;
>        e = 1.0L/7.0L;
>        printf("sizeof(7.0) = %u, sizeof(7.0L) = %u\n",sizeof(7.0),sizeof(7.0L));
>        printf("%3.60Lf %3.60Lf\n", c, c*7.0);
>        printf("%3.60Lf %3.60Lf\n", d, d*7.0);
>        printf("%3.60Lf %3.60Lf\n", e, e*7.0);
>        return 0;
> }
> Seems to yield the same precision for long double on both ia32 and
> amd64 - that is, just 20 digits. This seems too few, looking there:
> http://en.wikipedia.org/wiki/IEEE_754-2008#Basic_formats
> Shouldn't I expect at least more than 30 digits, on amd64?
I think it's not specified for C what exactly is "long double" -- only
some minimal requirements are "proposed".
The implementation-dependend values are defined in <float.h> as
DBL_EPSILON and LDBL_EPSILON (which is the difference between 1 and the
smallest value bigger than 1 that can be represented in double and long
double): There, 1e-9 is given as lower limit ==> you can only trust in 9
digits of precision, both for double AND long double! However, most
implementation will use something better here).

So try first on your machine a code like
#include <float.h>
#include <stdio.h>
int main()
  printf("%g %Lg\n",DBL_EPSILON, LDBL_EPSILON);

On my machines, this gives "2.22045e-16 1.0842e-19" both for i386 and
amd64. This is the highes precision you can obtain using "long double"
on those machines.


More information about the Gcc-help mailing list