newb: what's wrong with my code?

This is a discussion on newb: what's wrong with my code? within the C Programming forums, part of the General Programming Boards category; Code: temp_var_a = 1 - (pop_i/growth); temp_var_b = growth * temp_var_a; printf("test temp a %lf \n" , temp_var_a); printf("test temp ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    51

    newb: what's wrong with my code?

    Code:
    temp_var_a = 1 - (pop_i/growth);
    temp_var_b = growth * temp_var_a;
    
    printf("test temp a %lf \n" , temp_var_a);
    printf("test temp b %lf \n" , temp_var_b);
    
    x=exp(temp_var_b);
    
    final_val = pop_i * x;
    
    printf(" final val is %d \n" , final_val);


    I used two temp variables to display the calculations up to that point, so I know it works up to there...

    but the exponent function isn't working
    I'm guessing I'm using the exp function wrong? because the number doesn't come out right....

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    What types are the variables?

    And what do you mean it doesn't come out right? What do you expect? And what do you get? Note that you're losing the decimal precision when you print it as "%d". See http://www.cplusplus.com/reference/c...cmath/exp.html and http://www.cplusplus.com/reference/c...io/printf.html

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And by "losing the decimal precision", zacs could very well mean "completely blowing away the answer", since %d will not print floating-point variables.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Code:
    x=exp(temp_var_b);
    There could be something wrong with the exp function.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Unknowns in your post are variable types of temp_var_a temp_var_b and final_val, code listing of exp() and use of %d when the return value from exp() may well be a float.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > There could be something wrong with the exp function.
    yes... that's assuming that the C standard exp() from math.h wasn't used

    In that case it'd be double, not float.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    thankx guys...

    I put %lf in instead, and it works perfectly....

    but this leads me to another questions I've been pondering about....


    why is it you put in %lf, when you initially declared it as a double?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because that's what %lf means. %d means "integer in decimal format", while %f means "floating-point number" And actually %f works for doubles and floats equally well in printf (for scanf, you need the lf for doubles and the f for floats).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 08:10 AM
  2. Replies: 7
    Last Post: 08-06-2004, 10:14 AM
  3. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 01:51 AM

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