Thread: Trouble with "int" forcing itself into something I defined as "double"

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    3

    Trouble with "int" forcing itself into something I defined as "double"

    Hello everybody. My first post here so please be gentle.

    I have a problem I don't know how to weed out. I have a simple programme calculating some sequence and I literally have no variables declared as integer. However, when I call a function (again, declarations "double" left and right), it returns as "int".

    Here are the relevant parts:
    Code:
    int main() 
    { double i, add, x, enax;
    
    ....
    
    add = (double) pow((-1), (i)) * pow(x, i) / fact(i); 
    
    ....
    
    }
    
    fact(double a)
    
    {
    
    double j, f; 
    
    for (j=1.0, f=1.0; (j <= a); j++)
    
        { f *= j; }
    
    return f;
    
    }
    The even more rediculous thing is, while factorials are calculated inside the function, "f" is "double" (I checked by putting a printf command). however, the moment it is returned to main(), fact(i) has become "int".

    When I get stubborn, I can't even compile it because, when trying to print out the mid-results, I get:
    Code:
    enax.c:19:7: warning: format ‘%g’ expects argument of type ‘double’, but argument 3 has type ‘int’ [-Wformat=]
     */    printf("potencija %g, faktorijel %g \n", pow(x, i), fact(i));
    This "third argument" is, naturally, fact(i)

    When I take out this control mid-step, I get an error because the integer factorial overflows at i=14 and I want it much, much higher.

    Any ideas?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot to declare the return type of fact as double, so it was assumed to be int. Furthermore, you forgot to forward declare fact before calling it in main. (Alternatively, you can define fact before main.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2016
    Posts
    3
    Fixed. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A "Simple" Array/String Problem. "Help" ASAP needed
    By AirulFmy in forum C Programming
    Replies: 10
    Last Post: 08-19-2015, 04:30 AM
  2. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  3. Replies: 2
    Last Post: 08-19-2012, 06:15 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread