What's the deal with "long double"

Discussion in 'C Programming' started by bananaguyc, Jan 1, 2008.

  1. bananaguyc

    bananaguyc Guest

    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.
     
    bananaguyc, Jan 1, 2008
    #1
    1. Advertising

  2. In article <>,
    bananaguyc <> 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
     
    Walter Roberson, Jan 1, 2008
    #2
    1. Advertising

  3. bananaguyc

    bananaguyc Guest

    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.
     
    bananaguyc, Jan 1, 2008
    #3
  4. bananaguyc

    CBFalconer Guest

    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
     
    CBFalconer, Jan 1, 2008
    #4
  5. 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
     
    Martin Ambuhl, Jan 1, 2008
    #5
  6. bananaguyc

    Flash Gordon Guest

    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
     
    Flash Gordon, Jan 1, 2008
    #6
  7. bananaguyc

    CBFalconer Guest

    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
     
    CBFalconer, Jan 1, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ferran
    Replies:
    9
    Views:
    3,041
    Kevin Goodsell
    Apr 12, 2004
  2. JKop
    Replies:
    4
    Views:
    16,532
  3. Michael Mair

    Typecast long double->double seems to go wrong

    Michael Mair, May 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    662
    Michael Mair
    May 24, 2004
  4. rbt

    deal or no deal

    rbt, Dec 22, 2005, in forum: Python
    Replies:
    7
    Views:
    561
    Duncan Smith
    Dec 28, 2005
  5. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,205
    Peter Shaggy Haywood
    Sep 20, 2005
Loading...

Share This Page