temp = textBox1.Text.ToString();
double temp2;
temp2 = Convert.ToDouble(temp);
double fhar=(temp2/(5/9))+32;
string fhr = fhar.ToString();
MessageBox.Show(fhr);
Messagebox shows 0 or infinity, depending on what gets in.
Thanks to whoever answer.
EamonnPosted Aug 2, 2009, 2:32 PM
Rafael AnschauPosted Aug 1, 2009, 8:53 PM
EamonnPosted Aug 1, 2009, 8:36 PM
This is where the problem is, 5 & 9 are Int32 literals. Int32/Int32 produces and Int32, so dividing a large Int32 by a smaller Int32 will always produce 0. Dividing any number by zero will produce a Divide By Zero problem which is why you are getting infinity.
So why are you not getting a DivideByZero exception? Well if you were assigning the result to an Int32 then you would get a DivideByZero exception, actually yuo woudl get a compiler error as it woudl evaluate 5/9 as a literal zero. However, when assigning the result to a double or any floating point type, it will not throw an exception and will simply give you an infinity result. I'm not a mathematician but there is probably a good mathematical reason for this behaviour.
You can easily correct this by casting 5 & 9 up to a floating point:
double fhar=(temp2/((double)5/(double)9))+32;
Ideally though, you should assign 5 & 9 to a floating point constant.
e.g.
double const five = 5.00;
double double const nine = 9.00
double fhar=(temp2/(five/nine))+32;