# Double calculation problem

• 10-03-2002
Coconut
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
• 10-03-2002
Cshot
We're getting lots of these today.
• 10-03-2002
Coconut
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
• 10-03-2002
swoopy
>printf("Final temperature is %lf", final_temp);

Use %Lf for long double.
printf("Final temperature is %Lf", final_temp);
• 10-03-2002
Cshot
You have to specify precision.

Try
printf("Final temperature is %.7f\n", val);
• 10-04-2002
kottapalli
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;
}
• 10-08-2002
swoopy
>Mmm, I wonder if repetition will help...
Certainly worth a shot ...