Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > What's the deal with "long double"

Reply
Thread Tools

What's the deal with "long double"

 
 
bananaguyc
Guest
Posts: n/a
 
      01-01-2008
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: -69441527659720390616132329106879905271265162747383 26277051364061548998292338911457643245167311365849 94939249352577342742286164740704820047241658318432 03634399732116838814293139990778757446217299595246 28730068989817917060091919713159447848299377718491 87328.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.


 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      01-01-2008
In article <(E-Mail Removed)>,
bananaguyc <(E-Mail Removed)> wrote:
> 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.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
 
Reply With Quote
 
 
 
 
bananaguyc
Guest
Posts: n/a
 
      01-01-2008
On Tue, 01 Jan 2008 00:59:33 +0000, Walter Roberson wrote:

> %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.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-01-2008
bananaguyc wrote:
> On Tue, 01 Jan 2008 00:59:33 +0000, Walter Roberson wrote:
>
>> %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.


The following links will be helpful. I recommend the bz2
compressed version of the C standard, n869_txt.bz2.

Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html> (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf> (C99)
<http://cbfalconer.home.att.net/download/n869_txt.bz2> (C99, txt)
<http://www.dinkumware.com/refxc.html> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-01-2008
bananaguyc wrote:
> 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
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      01-01-2008
CBFalconer wrote, On 01/01/08 01:54:

<snip>

> <http://www.eskimo.com/~scs/C-faq/top.html> (C-faq)


Chuck, you really should update this to http://c-faq.com/
--
Flash Gordon
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-01-2008
Flash Gordon wrote:
> CBFalconer wrote, On 01/01/08 01:54:
>
> <snip>
>
>> <http://www.eskimo.com/~scs/C-faq/top.html> (C-faq)

>
> Chuck, you really should update this to http://c-faq.com/


Done.

--
Merry ChrX-Mozilla-Status: 0009 Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AMD64 or Semperon, deal or no deal? Tad Confused Computer Information 7 04-13-2006 05:43 PM
deal or no deal rbt Python 7 12-28-2005 08:57 PM
2 for 1 deal on 70-270 jb Microsoft Certification 1 04-07-2005 01:53 AM
Newbie Help: How do I deal with variable length vectors? roninn@gmail.com VHDL 5 03-28-2005 12:33 PM
simple programs to deal with data format, data synchronisation Ram VHDL 1 02-24-2005 05:34 PM



Advertisments