This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

feature or a bug with long double (quad-precision) in gcc (6.2.1)


Hello,

I faced strange problem with long double (C) in comparison with fortran
(gfortran) on my system:
Archlinux x86_64, gcc 6.2.1 20160830.

I prepared two small programs on C and Fortran (see attached files):
c-double-ldouble.c, gfortran-real-quad.f95.

For comparison I write to the file (in raw format) number 1/3 as double,
long double, real(kind=8) and real(kind=16), correspondingly files
'bin-double.bin', 'bin-ldouble.bin', 'bin-double.dat', bin-ldouble.dat.
Output of hexdump for double and real(kind=8) are identical

  $ hexdump bin-double.bin
  0000000 5555 5555 5555 3fd5
  0000008
  $ hexdump bin-double.dat
  0000000 5555 5555 5555 3fd5
  0000008

and in accordance with
https://en.wikipedia.org/wiki/Double-precision_floating-point_format
(except the order, but I suppose this is expected), but for long double
and real(kind=16) and different

  $ hexdump bin-ldouble.bin
  0000000 aaab aaaa aaaa aaaa 3ffd 99fb 7ffe 0000
  0000010
  $ hexdump bin-ldouble.dat
  0000000 5555 5555 5555 5555 5555 5555 5555 3ffd
  0000010

I suppose that hexdump for fortran case is correct, according to
https://en.wikipedia.org/wiki/Quadruple-precision_floating-point_format
so result of long double is wrong, besides it depends on options passed
to compiler.

Could someone shed a light on situation and that is the correct result?

I used the following lines to compile

  $ gcc -Wall -Wextra -pedantic -o FILE FILE.c
  $ gfortran -Wall -Wextra -pedantic -o FILE FILE.f95

Adding option -O2 to gcc changed content of raw file.

---
Vladimir Lomov

-- 
Q:	What lies on the bottom of the ocean and twitches?
A:	A nervous wreck.
#include <stdio.h>
#include <stdint.h>

int main(void)
{
  FILE *bin;

  double fd, ad, bd;

  ad = 1.;
  bd = 3.;
  fd = ad/bd;

  bin = fopen("bin-double.bin", "w");
  fwrite(&fd, sizeof(double), 1, bin);
  fclose(bin);

  long double fq, aq, bq;

  aq = 1.L;
  bq = 3.L;
  fq = aq/bq;

  bin = fopen("bin-ldouble.bin", "w");
  fwrite(&fq, sizeof(long double), 1, bin);
  fclose(bin);

  return 0;
}

Attachment: gfortran-real-quad.f95
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]