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; }
program write_bin_data implicit none real(kind=8) :: fd, ad, bd real(kind=16) :: fq, aq, bq ad = 1. bd = 3. fd = ad/bd open(unit=10,file="bin-double.dat", form="unformatted", access="stream", status="replace") write(10) fd close(10) aq = 1. bq = 3. fq = aq/bq open(unit=10,file="bin-ldouble.dat", form="unformatted", access="stream", status="replace") write(10) fq close(10) end program write_bin_data