Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long Double in C

Tags:

c

long-double

I've been reading the C Primer Plus book and got to this example

#include <stdio.h>
int main(void)
{
    float aboat = 32000.0;
    double abet = 2.14e9;
    long double dip = 5.32e-5;

    printf("%f can be written %e\n", aboat, aboat);
    printf("%f can be written %e\n", abet, abet);
    printf("%f can be written %e\n", dip, dip);

    return 0;
}

After I ran this on my macbook I was quite shocked at the output:

32000.000000 can be written 3.200000e+04
2140000000.000000 can be written 2.140000e+09
2140000000.000000 can be written 2.140000e+09

So I looked round and found out that the correct format to display long double is to use %Lf. However I still can't understand why I got the double abet value instead of what I got when I ran it on Cygwin, Ubuntu and iDeneb which is roughly

-1950228512509697486020297654959439872418023994430148306244153100897726713609
013030397828640261329800797420159101801613476402327600937901161313172717568.0
00000 can be written 2.725000e+02

Any ideas?

like image 845
reubensammut Avatar asked Nov 22 '09 15:11

reubensammut


People also ask

Can print long double in C?

We can print the long double value using %Lf format specifier.

What is double and long double in C?

The type double provides at least as much precision as float, and the type long double provides at least as much precision as double. The set of values of the type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set of values of the type long double.


1 Answers

Try looking at the varargs calling convention on OSX, that might explain it.

I'm guessing the compiler passes the first long double parameter on the stack (or in an FPU register), and the first double parameter in CPU registers (or on the stack). Either way, they're passed in different places. So when the third call is made, the value from the second call is still lying around (and the callee picks it up). But that is just a guess.

like image 63
Steve Jessop Avatar answered Sep 24 '22 11:09

Steve Jessop