Beginning C# – Precision and control

So you’ve learnt about variables, and you’re wondering, “What’s the practical difference between float, double and decimal variable types?” The difference is in precision.

floats represent single precision real numbers, known as such because of the floating point (or decimal point). doubles represent double precision real numbers, taking up two times the storage space of a float (different from representing two times the range of a float). decimals are a bit special, as they can represent up to 28 significant figures. It’s easier to just show you, so here’s the example code (only contents of the Main function).

float lowprecision;
double highprecision;
decimal exactprecision;
lowprecision = 1.61803398874989484820f;
highprecision = 1.61803398874989484820;
exactprecision = 1.61803398874989484820m;

Console.WriteLine(lowprecision);
Console.WriteLine(highprecision);
Console.WriteLine(exactprecision);

if (5 > 3)
{
Console.WriteLine("First condition");
}
else
{
Console.WriteLine("Second condition");
}

// variables can be declared and assigned a value
// on the same line of code.
int truthchecker = 17;
if (truthchecker == 17)
{
Console.WriteLine("The truth is the number 17.");
}

if (truthchecker != 17)
{
Console.WriteLine("The truth is NOT the number 17.");
}

/* The following if-else statement is equivalent to the above
* two if statements.
* This comment also illustrates how to use a multiline comment.
* */
if (truthchecker == 17)
{
Console.WriteLine("2nd check: The truth is the number 17.");
}
else
{
Console.WriteLine("2nd check: The truth is NOT the number 17.");
}

Console.WriteLine("End of program");

I’ve used a prominent mathematical number, the golden ratio, as an assigned value for our float, double and decimal variables. You should have noticed the “f” at the end of golden ratio for the float variable assignment, and the “m” for the decimal variable. These are known as suffixes, and there are other suffixes for other variable types. For now, just remember to append an “f” for floats, and an “m” for decimals.

Output for float, double and decimal
When you print the variables, you get 1.618034, 1.61803398874989 and 1.61803398874989484820 respectively. The difference is due to the size of the variable type, affecting how a variable stores a value. So even though the assigned value is the same, the storage and thus the stored value is different for the three variables.

One practical use of preferring floats over the other two types is their speed. Smaller storage sizes translates to faster calculation speeds, while sacrificing accuracy. This is particularly useful in games, such as calculating 3D positional points where the results don’t have to be accurate as much as being quickly computed. The player isn’t going to notice that a box is 0.000005 units off to the left, because he’s too busy shooting some dastardly evil aliens.

When accuracy is extremely important, such as in financial applications, decimals come into play. There are real numbers that floats and doubles cannot represent exactly. For example, the only money values a float or double can represent exactly are 0.00, 0.25, 0.50 and 0.75 (or any dollar amount with those cent values, like 14.75). Why is this? Short answer is it’ll take too long to explain. Try searching with the words “exponent” and “mantissa”. Anyway, with 28 significant figures for accuracy, decimals are ideal for financial applications.

Decisions, choices and forks
Next we learn a new concept: the if statement, one of the most powerful and most used construct for controlling program logic flow.

if (5 > 3)
{
Console.WriteLine("First condition");
}
else
{
Console.WriteLine("Second condition");
}

Note where the round brackets () and the curly brackets {} are used.
The above code can be read as “If 5 is greater than 3, then print ‘First condition’, otherwise, print ‘Second condition'”.

The if statement can also be used alone without the else part, such as

if (truthchecker == 17)
{
Console.WriteLine("The truth is the number 17.");
}

Note: The double equals mean mathematical equality. A single equal mean assignment.
In English, the above code reads “If the variable ‘truthchecker’ is equal to the number 17, print ‘The truth is the number 17.'” Some programmers prefer to reverse the check, like “17 == truthchecker“, to avoid confusing equality checks and assignments. I find it kinda funny to check in reverse, so take your pick.

Then there’s the converse.

if (truthchecker != 17)
{
Console.WriteLine("The truth is NOT the number 17.");
}

The exclamation mark followed immediately by an equal sign means “not equal to”. So the chunk of code reads “If the variable ‘truthchecker’ is not equal to the number 17, print ‘The truth is NOT the number 17.'”

Multiline commenting
Two consecutive forward slashes // means whatever follows on the same line is a comment, and to be ignored by the computer. What if your comments are too long and go over the next line? You can start another line with // and continue writing comments. Or if you can start your comments with a forward slash and an asterisk /* and end your comments with an asterisk and a forward slash */, which achieves the same effect.

Homework
Wow, that was a long lesson. So homework will be short. Download the source code, and replace the line

if (5 > 3)

with

if ((5 * 4) < (27 - 6))

Change the numbers around and see what happens.