Thread: Long double does not make sense.

  1. #1
    Registered User KAUFMANN's Avatar
    Join Date
    Jan 2011
    Location
    Coimbra, Portugal
    Posts
    31

    Long double does not make sense.

    Hello Community,

    I wrote a small piece of code to print a fairly large number. Something along the lines of 30!++.
    The problem is that what I get instead is a group of random numbers with no meaning, seemingly.
    Am I using the notation wrong here?


    Code:
    #include<stdio.h>
    int main(void){
        int i,lim;
        long double k;
        k=1;
        scanf("%d",&lim);
        for(i=1;i<lim;i++){
                          k*=i;
                          printf("%Ld\n",k);
                          }
        system("pause");
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You need %Lf for long double.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    30! would require 108 bits. That's more than long long.
    Is long double (128-bit quadruple precision) supported?
    "Not all compilers support the quad-precision type. Microsoft Visual C++, for example, makes the long double type synonymous with the double-precision type."

    If your compiler does have it, that has a 112 bit mantissa which is sufficient for exact representation of such a large number.

    What are the corresponding formatter codes for printf? I have no idea.

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    If your number is the factorial of 30, you probably want a long int rather than a long double anyway.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    As I said, 30! exceeds long long. Best you can do there is 12!

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by TheBigH View Post
    If your number is the factorial of 30, you probably want a long int rather than a long double anyway.
    A float at the very least; a long int ain't gonna cut it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Travel Expenses-Weird Result
    By taj777 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2010, 02:23 PM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Errors that make no sense...
    By applescruff in forum C++ Programming
    Replies: 22
    Last Post: 03-04-2005, 05:44 PM
  5. Replies: 15
    Last Post: 04-16-2003, 08:06 PM