Thread: C Double Float and printf problem

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

    C Double Float and printf problem

    I have been searching the forum for over an hour trying to find a solution to what i'm seeing, as well as the rest of the internet, but theres nothing i can find. I initialize a variable as a double, however when i print it, it limits the value to 16 digits, and fills in the rest with zeros. This reflects the e38 of a float, not the e308 of a double.

    Code:
    #include <stdio.h>
    #include <float.h>
    
    int main()
    {
      double x;
      double y;
      int lim44;
      printf( "Enter the number of cycles: ");
      scanf( "%d", &lim44);
    
      x = 1.0;
      y = 1.0;
    
      do {
        /* "Hello, world!" is printed at least one time
          even though the condition is false*/
        printf( "%.1Lf\n", x );
        x = x+y;
        lim44 = lim44-1;
        printf( "%.1Lf\n", y);
        y = y+x;
        lim44 = lim44-1;
      } while ( lim44 > 0 );
      getchar();
    }
    The Output for sequences 80 to 100:
    Code:
    37889062373143904.0
    61305790721611584.0
    99194853094755488.0
    160500643816367070.0
    259695496911122560.0
    420196140727489660.0
    679891637638612220.0
    1100087778366101900.0
    1779979416004714000.0
    2880067194370816000.0
    4660046610375530500.0
    7540113804746346500.0
    12200160415121877000.0
    19740274219868226000.0
    31940434634990100000.0
    51680708854858326000.0
    83621143489848426000.0
    135301852344706760000.0
    218922995834555200000.0
    354224848179262000000.0

    As seen, it gives up after 16 digits, and with rounding this throws the math off more and more every level. Is there a way to correct this? Or am i missing something stupidly simple?

    Any help will be appreciated.
    Last edited by SlyVixsky; 07-30-2009 at 03:13 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You are lucky you are getting what appears to be valid numbers at all. %Lf is used to print long doubles, not doubles. Change your format specifier to %f, and see if that works for you.

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Try declaring your x and y as long doubles. Thats the highest precision offered in C.
    Spidey out!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    A float has 7 digits, and a double has 16. (Approximately: A float has 24 binary digits, which is about 7.22 decimal digits of accuracy, and a double has 53 binary digits, which is 16 decimal digits.) As to the size, the bit you show appears to have the right order of magnitude if I understand what you are trying to do.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    Quote Originally Posted by bithub View Post
    %Lf is used to print long doubles, not doubles. Change your format specifier to %f, and see if that works for you.
    I tried that to begin with, to no avail. I've read the printf page, but i i think the issue isn't printf but the float/double part. its not using the right limits, and i haven't yet found any reason why.

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    Quote Originally Posted by tabstop View Post
    A float has 7 digits, and a double has 16. (Approximately: A float has 24 binary digits, which is about 7.22 decimal digits of accuracy, and a double has 53 binary digits, which is 16 decimal digits.) As to the size, the bit you show appears to have the right order of magnitude if I understand what you are trying to do.
    Using float or double or long double all releases the same result as i posted.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by SlyVixsky View Post
    I tried that to begin with, to no avail. I've read the printf page, but i i think the issue isn't printf but the float/double part. its not using the right limits, and i haven't yet found any reason why.
    Well, if you mean the limits in float.h, that's true -- you didn't use them anywhere. Let's start here: What do you think you're supposed to be getting?

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    Quote Originally Posted by tabstop View Post
    Well, if you mean the limits in float.h, that's true -- you didn't use them anywhere. Let's start here: What do you think you're supposed to be getting?
    I want to calculate the fibonacci sequence for 'q' iterations, but after 16 digits it doesn't show the numbers, which means the program is only accurate up to about the 83 iteration. I've looked into the #define command for the float.h library, but changing the exponent limit, or number of digits hasn't made any difference.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    This is the last 5 iterations I get using GCC:

    Using floats:
    Code:
    51680688087983194112.0
    83621113846783016960.0
    135301801934766211072.0
    218922915781549228032.0
    354224717716315439104.0
    Using doubles:
    Code:
    51680708854858326016.0
    83621143489848426496.0
    135301852344706760704.0
    218922995834555203584.0
    354224848179261997056.0
    Using long double:
    Code:
    51680708854858323072.0
    83621143489848422976.0
    135301852344706746048.0
    218922995834555169024.0
    354224848179261915072.0
    I'm not sure why you are getting the same values using all 3 types.

    The best question though is why are you using floating point arithmetic? Use a type like long long instead and you will actually get the correct results.

    EDIT: Actually, I may be underestimating how large of numbers you need here. If you need something larger than a 64 bit integer can handle, then you should look into using GMP.
    Last edited by bithub; 07-30-2009 at 03:39 PM.

  10. #10
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    Quote Originally Posted by bithub View Post
    This is the last 5 iterations I get using GCC:

    I'm not sure why you are getting the same values using all 3 types.

    The best question though is why are you using floating point arithmetic? Use a type like long long instead and you will actually get the correct results.
    my result with long
    Code:
    572466946
    -2079590721
    -1507123775
    708252800
    -798870975
    -90618175
    -889489150
    -980107325

  11. #11
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    GMP???

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by SlyVixsky View Post
    I want to calculate the fibonacci sequence for 'q' iterations, but after 16 digits it doesn't show the numbers, which means the program is only accurate up to about the 83 iteration. I've looked into the #define command for the float.h library, but changing the exponent limit, or number of digits hasn't made any difference.
    You can't change the values in float.h; they describe what the hardware does, not dictate to the hardware what it should do.

    If you really need twenty-odd decimal digits, then you need to either (a) make sure you are on a system with a 128-bit long long type, (b) write your own routines for extra-long integers, or (c) use somebody else's bignum library (like the GSL for instance).

  13. #13
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    my result with long
    I said long long, not long.

    GMP???
    It is a library for doing arithmetic with very large numbers.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by SlyVixsky View Post
    my result with long
    Code:
    572466946
    -2079590721
    -1507123775
    708252800
    -798870975
    -90618175
    -889489150
    -980107325
    Note that "long" and "long long" are not the same thing.

    GMP is an option, yes. (I think I erroneously just put GSL in my last reply.)

  15. #15
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    Quote Originally Posted by bithub View Post
    I said long long, not long.
    .
    same result

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  3. Noob: Structure problem
    By Karmachrome in forum C Programming
    Replies: 6
    Last Post: 11-10-2005, 06:13 PM
  4. whats wrong with this?
    By petedee in forum C Programming
    Replies: 32
    Last Post: 01-06-2004, 10:28 PM
  5. error in program????
    By SpEkTrE in forum C Programming
    Replies: 5
    Last Post: 11-24-2003, 06:16 PM