What's the deal with "long double"

B

bananaguyc

Okay, I'm running the following code in GCC 4.1.2:

<stdio.h>
int main(void)
{
long double test = 4.67e-4;
printf("float: %f e-notation: %e\n", test, test);

return 0;
}


I'm expecting something like the following results:
float: 0.000467 e-notation: 4.67000e-05

but instead, I get the following results:

float: -694415276597203906161323291068799052712651627473832627705136406154899829233891145764324516731136584994939249352577342742286164740704820047241658318432036343997321168388142931399907787574462172995952462873006898981791706009191971315944784829937771849187328.000000 e-notation: 4.802997e+149

What exactly is the issue here?

I'm using GCC 4.1.2 on an Intel p3, if that matters.
 
W

Walter Roberson

long double test = 4.67e-4;
printf("float: %f e-notation: %e\n", test, test);

%e and %f are not correct format descriptors for long double.
Try %Le and %Lf (capital L only, not lower-case L).

You are passing in a variable that takes more storage than is
expected according to the format string, so only a part of the
variable storage is (mis-)formatted, leaving the rest of the variable
there to be (mis-)formatted by the next format descriptor.
 
B

bananaguyc

%e and %f are not correct format descriptors for long double.
Try %Le and %Lf (capital L only, not lower-case L).

You are passing in a variable that takes more storage than is
expected according to the format string, so only a part of the
variable storage is (mis-)formatted, leaving the rest of the variable
there to be (mis-)formatted by the next format descriptor.

I see, very informative, thank you.
 
C

CBFalconer

M

Martin Ambuhl

bananaguyc said:
Okay, I'm running the following code in GCC 4.1.2:

/* mha: You really should with the flags for gcc to check the arguments
of printf, which it will do for you. */

#include <stdio.h> /* mha: fixed this line */

int main(void)
{
long double test = 4.67e-4;
#if 0
/* mha: gcc invoked with the proper flags will tell you that your
line below has two incorrect specifiers in it. */
printf("float: %f e-notation: %e\n", test, test);
#endif
/* mha: relacement for the line above */
printf("float: %Lf e-notation: %Le\n", test, test);
return 0;
}
> I'm expecting something like the following results:

Actually, you have no business expecting any particular results
when you use an incorrect specifier with printf. Here is what the
above code produces on one implementation. Except for your error in
the exponent for the e-notation, it looks very close to your
expected
> float: 0.000467 e-notation: 4.67000e-05

[output]
float: 0.000467 e-notation: 4.670000e-04
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top