1

With gfortran compiler, the flag -fdefault-real-16 helps to set the default real type to an 16 byte wide type. for example

program qp_test
implicit none 
      double precision  :: a    
      a  = 123456789012345678.123456789012345678
      write(*,'(a,g41.36)') 'my a    = ',a
      write(*,'(a)')        'exact a = 123456789012345678.123456789012345678'
end program 

When is complied as

gfortran qp_test.f90

The result is looks like

my a    = 123456790519087104.000000000000000000    
exact a = 123456789012345678.123456789012345678 

And when is compiled as

gfortran qp_test.f90 -fdefault-real-16

The result is looks like

my a    = 123456789012345678.123456789012345677    
exact a = 123456789012345678.123456789012345678 

So, I tried to do the same with c program

#include<stdio.h>
#include<string.h>
int main() {
   double a;
   a  = 123456789012345678.123456789012345678;           
   printf("my a    = %36.18f\n", a);
   printf("exact a = 123456789012345678.123456789012345678\n");
   return 0;
}

The problem is : Even the c program is compiled with -m64 flag, such as

gcc qp_test.c -o qp_test_c -m64

It gives the same result

my a    = 123456789012345680.000000000000000000
exact a = 123456789012345678.123456789012345678

The question is : Is there some how to tell gcc to compile "double" as "long double"?

francescalus
  • 30,576
  • 16
  • 61
  • 96
  • long double is not guaranteed to be any more precise than double. https://en.m.wikipedia.org/wiki/Long_double – AKX Jan 06 '21 at 10:00
  • @AKX According to the title Mohammed is clearly interested in 16-byte floating point types, no matter how they happen to be named. – Vladimir F Героям слава Jan 06 '21 at 10:05
  • Fortran has the luxury of independence on the operating system. C is interconnected with the standard C library used also for the OS and the system services and libraries through various headers and therefore one usually fixes all the numerical types inn the compiler (also the integer ones, like specifying the ILP64 system). – Vladimir F Героям слава Jan 06 '21 at 10:07
  • Why not just use `long double` in your C program? If gfortran supports 80-bit reals (padded to 16 bytes) on your system, gcc's `long double` will probably be the same type on your system too. – Ian Abbott Jan 06 '21 at 10:21
  • @IanAbbott Gfortran also supports true 16 byte reals. – Vladimir F Героям слава Jan 06 '21 at 10:24
  • What I have seen in numerical programs that wanted the freedom you get with Fortran was to declare some `#define my_float double` in the top-most header and use `my_float` instead of anything else throughout the program. It is also great to switch between float and double. Too bad you than still have to distinguish which functions you call because there are no generics in C. C++ is much better for generic programming. – Vladimir F Героям слава Jan 06 '21 at 10:26
  • 1
    Related [question](https://stackoverflow.com/questions/59894562/how-to-use-gnu-gcc-flag-mpc32-mpc64-and-mpc80) ? – Weather Vane Jan 06 '21 at 10:27
  • 1
    @VladimirF *declare some `#define my_float double`* A `typedef` is a better solution in many ways. – Andrew Henle Jan 06 '21 at 11:19
  • @AndrewHenle Absolutely, I am not a C programmer in any way I and just wrote the first thing that came to my mind. Sorry if it looked like I am posing as some kind of C authority which I am definitely not in any way at all whatsoever. The codes I wes refering to did in fact used a typedef. Just wrong reporting on my part. Sorry for the harm caused (if any). The point remains the same though. – Vladimir F Героям слава Jan 06 '21 at 11:47
  • @AndrewHenle The fact is that you do actually the `typedef` to be in a macre, just not ina `define` but in a `#if` or `#ifdef` so as not to have to edit the code itself. Again sorry for any harm done by confusing people when trying to recall somehing in the very short time available to edit comments... – Vladimir F Героям слава Jan 06 '21 at 11:52

0 Answers0