The only arithmetic symbols that have changed since their inception in the 1960s are the exponentiation and logarithmic symbols. The following table shows these symbols.
Addition | intAnswer = intA + intB; |
Subtraction | decAnswer = decA - decB; |
Multiplication | dblAnswer = dblA * dblB; |
Division | dblAnswer = dblA / dblB; |
Exponentiation | dblAnswer = Math.Pow(dblA, 3.474); // dblA to the 3.474 power |
Logarithms base 10 | dblAnswer = Math.Log10(dblA); |
Logarithms base e | dblAnswer = Math.Log(dblA); |
Parentheses are used to enclose all mathematical expressions:
double dblAnswer = (dblA / dblB) * dblC + dblD;
Note | You would never have a mathematical expression with a variable type declared on the right-hand side of the equation. This can never occur: double dblAnswer6 = (double dblA2 / double dblB2) + 4.2; In this case, both dblA2 and dblB2 must be declared before this statement. Also, both dblA2 and dblB2 must have been filled with numbers before this statement can be executed. The Visual Studio C# compiler checks that the variables are filled before any arithmetic can be accomplished using them. This sequence is acceptable: double dblA2 = 4.0; double dblB2 = 3.5; double dblAnswer6 = (dblA2 / dblB2) + 4.2; // '4.2' automatically becomes a 'double'. |
The conversions between the various arithmetic data types are shown in Chapter 9, Conversions between Data Types.