Double calculation problem

This is a discussion on Double calculation problem within the C Programming forums, part of the General Programming Boards category; Hi, I can't get the result right for the float computation. Does anyone help me to fix it. Caution: If ...

  1. #1
    Coconut
    Guest

    Question Double calculation problem

    Hi,
    I can't get the result right for the float computation. Does anyone help me to fix it.
    Caution: If the computation not right, it is kinda
    dangerous to operate the nuclear reactor with this program. :-)

    Code:
    int main()
    {
      long double init_temperature;
      long double temp_detect;
      long double final_temp;
      
      /* Initial temperature */
      init_temperature = 25.0000000;
                                    
      /* Get temperature from sensor */
      temp_detect = 0.0000001;  /* call getTemp()*/
                                    
      final_temp = init_temperature + temp_detect;   
      printf("Final temperature is %lf", final_temp); 
                                    
      if (final_temp > init_temperature)
         shutdown_nuclear_reactor();  /*this func */ 
                                    
      return 0;  
    }
    Coconut

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    We're getting lots of these today.
    Have you read this?
    http://www.cprogramming.com/cboard/s...threadid=25714
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Coconut
    Guest
    I got it, thanx. I should not do comparation
    expression with double or float. But when I used
    printf() to print out the final_temp, it show an
    unexpected output for final_temp. I really dont
    know why?
    Coconut

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >printf("Final temperature is %lf", final_temp);

    Use %Lf for long double.
    printf("Final temperature is %Lf", final_temp);

  5. #5
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    You have to specify precision.

    Try
    printf("Final temperature is %.7f\n", val);
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    2

    try it ...

    int main()
    {
    long double init_temperature = 0;
    long double temp_detect = 0;
    long double final_temp = 0;

    /* Initial temperature */
    init_temperature = 25.0000000;

    /* Get temperature from sensor */
    temp_detect = 0.0000001; /* call getTemp()*/

    final_temp = init_temperature + temp_detect;
    printf("Final temperature is %10.7lf", final_temp);

    if (final_temp > init_temperature)
    shutdown_nuclear_reactor(); /*this func */

    return 0;
    }

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Mmm, I wonder if repetition will help...
    Certainly worth a shot ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Price Calculation Problem using Class
    By Bernard4 in forum C++ Programming
    Replies: 4
    Last Post: 07-23-2008, 12:12 PM
  3. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM
  4. Little Problem on Presentation in the Tower of Hanoi
    By momegao in forum C++ Programming
    Replies: 3
    Last Post: 04-20-2003, 06:22 PM
  5. getline problem
    By scottmanc in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2003, 09:27 PM

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