long double problem...

This is a discussion on long double problem... within the C Programming forums, part of the General Programming Boards category; Hello all, I have just begun learning C and have run into a problem with floating-point numbers. I am using ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    long double problem...

    Hello all, I have just begun learning C and have run into a problem with floating-point numbers. I am using Dev-C++ 4.9.9.2 on Vista. I am working on this example from a book:

    Code:
    /* showf_pt.c -- displays float value in two ways */
    
    #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;
    
    }
    The output the book says I should get is this:

    Code:
    32000.000000 can be written 3.200000e+04
    
    2140000000.000000 can be written 2.140000e+09
    
    0.000053 can be written 5.320000e-05
    But the output that I get is:

    Code:
    32000.000000 can be written 3.200000e+004
    
    2140000000.000000 can be written 2.140000e+009
    
    -1950228512509697500000000000000000000000000000000000000000000000000000000000000
    000000000000000000000000000000000000000000000000000000000000000000000000.000000
    can be written 2.725000e+002
    The first two printf functions come out right, but the third one is obviously wrong. Can anyone help me figure out what is going on here? Thanks!
    Last edited by brianedward; 07-17-2009 at 09:33 AM. Reason: code tags

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    %f and %e is not meant to print long doubles.
    And use code tags next time!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Elysia, thanks for the quick reply, and sorry about those code tags, i went back and put them in. What should i use instead of %f and %e when trying to print long doubles?

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by brianedward View Post
    Elysia, thanks for the quick reply, and sorry about those code tags, i went back and put them in. What should i use instead of %f and %e when trying to print long doubles?
    %lf is used for long double.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    I tried using %lf, but the output was the same as %f. Is there something else that I'm missing?

  6. #6
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    978
    It's %Lf, not %lf. In the latest revision of the C standard, %lf simply means the same thing as %f. In earlier revisions, %lf is invalid.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    I tried the %Lf also, but with the same results. Maybe this will better illustrate the problem i'm having:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        long double dip = 5.32e-5;
        
        printf("%Lf can be written as %e\n", dip, dip);
        
        getchar();
        
        return 0;
    }
    the result is this
    Code:
    -1950228512509697500000000000000000000000000000000000000000000000000000000000000
    000000000000000000000000000000000000000000000000000000000000000000000000.000000
    can be written as 2.725000e+002
    but it should be, or at least what the book says it should be:
    Code:
    0.000053 can be written 5.320000e-05
    any ideas?

  8. #8
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    978
    printf() is not smart. It has no idea what types you're passing to it. You have to tell it, which is what you're doing with %f, %Lf, %e, and so on. %f means you're passing a double, and if you don't pass a double, all bets are off. Similarly for %e. You must instead use %Le, which tells printf() to expect a long double, which is what you're passing.

    Your compiler is not required to tell you (nor can it always tell you, even for those that do warn on improper printf() usage, like gcc) when you're calling printf() incorrectly. It's up to you to get it right, although you might try turning up your compiler's warning level; perhaps it can diagnose some incorrect printf() calls.

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Thanks for the help cas, I changed the printf function to say:
    Code:
    printf("%Lf can be written as %Le\n", dip, dip);
    But I got the exact same result as before. I must have something else wrong. Any more ideas?

  10. #10
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    978
    Broken compiler? If the following code doesn't print what you'd expect, there's a bug somewhere (in the compiler or library, probably):
    Code:
    #include <stdio.h>
    int main(void)
    {
      long double d = 5.32e-5;
      printf("%Lf\n", d);
      return 0;
    }
    Edit: It might also be a mismatch between compiler and library; that is, if your compiler relies on a system library, or a library from another compiler, they might disagree about what, exactly, a long double is. If they are at odds on this issue, then you wouldn't expect correct output.
    Last edited by cas; 07-17-2009 at 11:41 AM. Reason: update

  11. #11
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    You must be right, because I got the same result as before. I'm using Dev-C++ 4.9.9.2, should I just try to re-install it, or should i try a different compiler?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    This is what happened the last time somebody asked this question.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Quote Originally Posted by tabstop View Post
    This is what happened the last time somebody asked this question.
    Oh wow, manual deconstruction. That's a nasty thing to have to do.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by cas View Post
    It's %Lf, not %lf. In the latest revision of the C standard, %lf simply means the same thing as %f. In earlier revisions, %lf is invalid.
    In my case, both %Lf and %lf print the same thing without any warning with %lf. I compiled the OP's code
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        long double dip = 5.32e-5;
        
        printf("%Lf can be written as %e\n", dip, dip);//with %lf also the same result
        
        getchar();
        
        return 0;
    }
    The answer is correct with both of them.
    Code:
    0.000053 can be written 5.320000e-05
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  15. #15
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    978
    Quote Originally Posted by BEN10 View Post
    In my case, both %Lf and %lf print the same thing without any warning with %lf.
    That's one possibility when the behavior is undefined.

    A compiler is not required to diagnose mismatching/incorrect conversion specifiers in calls to printf(). %lf was a common extension in C89 which meant the same as %f, although it's certainly possible that an implementation would have %lf mean the same as %Lf (%lf is required to mean %f in C99). Your system might also treat double and long double as the same thing, which would mean %Lf and %f would work for either.

    For reference, the OP's code gives me:
    0.000053 can be written as -1.950229e+150 (that's with %Lf and %e)
    or
    -19502285125096974860202976549594398724180239944301 48306244153100897726713609013030397828640261329800 79742015910180161347640232760093790116131317271756 8.000000 can be written as 2.725000e+02 (%lf and %e)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Replies: 15
    Last Post: 04-16-2003, 08:06 PM
  3. getline problem
    By scottmanc in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2003, 09:27 PM
  4. need help
    By emperor in forum C Programming
    Replies: 1
    Last Post: 03-04-2002, 11:26 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21