# Thread: Double calculation problem

1. ## 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. We're getting lots of these today.
Have you read this?
http://www.cprogramming.com/cboard/s...threadid=25714

3. 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. >printf("Final temperature is %lf", final_temp);

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

5. You have to specify precision.

Try
printf("Final temperature is %.7f\n", val);

6. ## 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. >Mmm, I wonder if repetition will help...
Certainly worth a shot ...

Popular pages Recent additions