Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Using "long double" with gcc

I want to use long doubles (quad, 128-bit floating point variables) in C. I thought the following program would do it:

#include <stdio.h>

int main()

{
long double a, b, c;

b = 1./7.;
c = 7.;
a = 1./c;

printf("%.30e \n", a);
printf("%.30e \n", b);
printf("%.30e \n", c);

return 0;
}

But the program returns garbage when compiled and run with:

%gcc top2.c -o test
%./test
-1.391130315222827826529386765980e-220
-1.391130315222712834096824073018e-220
-2.681561585988519419914804999641e+154

(XCode 3.1, gcc 4.0.1 or 4.2.1, Intel Xeon, 10.5.4)

The program works for double, however.

So what am I doing wrong?

Matthias

MacPro Quad-Core Xeon, MacBook Pro, Mac OS X (10.5.4)

Posted on Sep 8, 2008 6:09 AM

Reply
3 replies

Sep 8, 2008 8:06 AM in response to mgott

I can't really help you much there. Maybe you should think about what you are doing and what you really need. I doubt you really need 128 bits. In Apple's Documentation it says:

-m96bit-long-double
-m128bit-long-double
These switches control the size of long double type. The i386 application binary interface specifies the size to be 96 bits, so -m96bit-long-double is the default in 32 bit mode. Modern architectures (Pentium and newer) would prefer long double to be aligned to an 8 or 16 byte boundary. In arrays or structures conforming to the ABI, this would not be possible. So specifying a -m128bit-long-double will align long double to a 16 byte boundary by padding the long double with an additional 32 bit zero.

In the x86-64 compiler, -m128bit-long-double is the default choice as its ABI specifies that long double is to be aligned on 16 byte boundary.

Notice that neither of these options enable any extra precision over the x87 standard of 80 bits for a long double.

Warning: if you override the default value for your target ABI, the structures and arrays containing long double variables will change their size as well as function calling convention for function taking long double will be modified. Hence they will not be binary compatible with arrays or structures in code compiled without that switch.

Message was edited by: etresoft - gave up trying to format it fancy

Using "long double" with gcc

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.