Thread: printf not working with long type

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    2

    printf not working with long type (resolved)

    Code:
    long stirling1(int n)
    {
          double nd = n;
          
          return ( pow(nd,nd)  * exp(-nd) * sqrt( (2 * PI * nd) ));
    }
    Code:
    long stirling2(int n)
    {
          double nd = n;
          
          return ( pow(nd,nd)  * exp(-nd) * sqrt( (2 * PI * nd) ) * (1 + 1/(12*nd)) );
    }
    Code:
    for(int i = 1; i<=input; i++)
    	      	  printf("%4d %14d %20.10e %20.10e\n", i, factorial(i), stirling1(i), stirling2(i));

    The above code does not print the correct values for stirling1() and stirling2(). I think this has something to do with the printf because when i change the "%20.10e" to "%20d" the numbers are correct in decimal form. I know the calculations are right, but i can't figure out why the printing is wrong.
    Here is the output:
    Code:
    Number    Factorial      Approximation1      Approximation2
    ---------------------------------------------------------------------
       1              1     0.0000000000e+00    4.2419181287e-314
       2              2    2.1219957915e-314    6.3639139197e-314
       3              6    1.0609978957e-313    1.4851897084e-313
       4             24    4.8805903204e-313    5.3047821321e-313
       5            120    2.5039550339e-312    2.5675941725e-312
       6            720    1.5066170119e-311    1.5299568918e-311
       7           5040    1.0567539041e-310    1.0696978709e-310
       8          40320    8.4671876071e-310    8.5560990214e-310
       9         362880    7.6293387888e-309    7.7003195255e-309
      10        3628800    1.2745071711e-307    1.3000610917e-307
      11       39916800    2.7223431127e-297    3.2649323566e-297
    Last edited by that_guy1; 10-15-2005 at 05:41 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You do know that long isn't a floating point type, right? It's integral.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    2
    Thank you very much. I changed the fuction return types to double and everything works fine. I didn't think the storage for double was that big, so I used long. Double is more than enough for what I need.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. Displaying a 'long long' with printf
    By trinitrotoluene in forum C Programming
    Replies: 10
    Last Post: 12-28-2004, 01:32 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM