If your using C Sharp in order to calculate long equations you may have encountered errors in the programs output although nothing is wrong with the equation.

Take for example the following equation to convert the temperature of 87 degrees Fahrenheit to Celsius.

(5/9) * 87 -32 = 30.56

If you were to enter the equation above into excel you would get the response 30.56, however, if you were to code it in C# as follows:

celsius =(5 / 9) * (87.00 – 32.00)

the variable celsius would be 0. I’m not sure why this happens (yet) but to avoid this, only enclose the 87 – 32 in the equation. When you add parentheses it should only be to change the order of operation. If you redundantly enclose something whose order of operation is not affected by the parentheses then that value will turn to zero (0).

for example,

celsius = (87 – 32) * (5 / 9);

will give you zero (0).

celsius = (87 – 32) + (5 / 9);

will give you 55

celsius = (87 – 32) * 5 /(9 + 0);

will give you 30.56. This is becuase the second parentheses is not redundant in terms of affecting the order of operations.

Below is the C# program to address any doubts regarding the error’s origin such as improper variable types, casting or anything else.

using System;

using System.Collections.Generic;

using System.Text;

namespace ca3_3

{

/*

* This program will convert Farenheit to Celsius

* Program by Oscar Valles.

*/

class ex3_3

{

static void Main(string[] args)

{

const double FAHRENHEIT = 87.00;

double celsius;

celsius = (FAHRENHEIT – 32.00) * 5 / 9;

Console.WriteLine(celsius);

Console.WriteLine(“The celsius temp for {0} fahrenheit is {1}”, FAHRENHEIT, celsius);

Console.Read();

}

}

}

Program Code in VS C# 2005 Express (https://oscarvalles.files.wordpress.com/2008/06/vs-csharp-implicit-one.jpg)

Output of program code in Console (https://oscarvalles.files.wordpress.com/2008/06/vs-csharp-implicit-two.jpg)

Note that in integer arithmetic, 5/9 = 0, and 4/3 = 1

This is true, thanks Dave. Although, implicit type coercion does occure in this example. Implicit type coercion is when integral types (such as integers) convert to floating types (such as doubles). Under C# it happens when you mix numeric types in an equation such as int and double. Since the variable FAHRENHEIT is a double and 32.00 is also noted as a double, implicit type coercion occured. If I would have solely had 5/9 in the equation then the answer would be 0 as Dave noted. Good catch.

(5 / 9) * (87.00 – 32) = 0

Implicit type conversion does occur in (87.00 – 32) and in (1st) * (2nd), but where two integers used, such as (5/9), the calculation is done in integer arithmetic. Hence you have (0) * (65.00) = 0.

I’m not disagreeing with what you are saying. I would expect to get 0 as my aswer, but for whatever reason I did not. I used Visual C# 2005 Express edition and the output was 32.XXX. Maybe after the 65.00 was arrived at, instead of truncating the int division it took its actual value and multiplied by the 65.00? I’m not sure but to be on the safe side, I would add decimal places to the int values. Thanks again.