This lab serves multiple goals:
- To introduce you to the concept of overflow and underflow,
- To give an example of the
MaxValue
andMinValue
constants, - To exemplify the care required when performing mathematical calculations with programs,
- (Optional) To illustrate the
String.Format
method.
Overflow
Warm Up
For a general introduction of overflow, please read the relevant section. Do execute the code shared in this section:
Make sure you have a general understanding of what overflowing means before proceeding. If you are unsure, reading about integer overflow on wikipedia can help.
We will now enter a surprising world where
- a number can be equal to itself plus one,
- a number plus one can be less than the number itself, and
- a number multiplied by two and then divided by two is the same as the number multiplied by two.
Now, download, execute the “Overflow” solution, and answer the following questions:
- What is the maximum value that can be stored in an
int
? - What should be the result of adding one to the maximum value that
can be stored in an
int
? - What is the result actually displayed by C#?
Then, answer the same questions for the float
and double
datatypes.
C#‘s Checks
We will now study some of the safeguards against overflowing that are
implemented in C#. Note that some of those checks have been
deactivated by the unchecked
command at the beginning of our project.
Make sure to complete the following section outside of unchecked
’s
scope (i.e., after it).
-
Enter the following statements in your program:
You should receive an error message. Reads it and try to understand what C# is warning you against.
-
Isn’t that weird that C# let go
(even outside an
unchecked
environment, you can try it) not the previous statement? This is because, by default, arithmetic operations onint
are “unchecked”. To check it, useWhat happens when you try to compile and run this program?
-
Note that our program does not give the result of adding one to the maximum value that can be assigned to a
decimal
. Try to display on the screen the result of adding one todecimal.MaxValue
(both inside and outside theunchecked
environment).
Strange Mathematical Properties
Circling back to our prompt in the warm-up section, determine which datatype matches the different overflow behaviors described in the table below, along with the resulting values for each:
Description | Value(s) | Datatype(s) |
---|---|---|
<datatype> .MaxValue == <datatype> .MaxValue + 1 | ||
<datatype> .MaxValue + 1 < <datatype> .MaxValue | ||
<datatype> .MaxValue * 2 / 2 == <datatype> .MaxValue * 2 |
Note that int.MinValue
can similarly produce strange mathematical
properties of the same kind.
As funny or interesting as that strange behavior may seem, overflow errors actually caused death and millions of dollars of losses repeatedly, as you can read for instance in this blog post.
Underflow
Most of what we wrote about overflow is also true of under flow, and you can read about it in the lecture notes. In a nutshell, you can witness it by executing a statement such as
Which should display 1e-34
but actually displays 9.999999E-35
. As
you can see, a rounding error took place because C# did not have enough
“room” to store all the information. Another interesting example is
given by the following loop:
Note that this loop should never exit: no matter how large x
is,
dividing it by two repeatedly should never make it ! After how many
iterations would this loop terminate?
(Optional) String Formatting
As you may have noticed, our program uses the String.Format
method to
nicely display the information. You can read about this method in the
official
documentation.
Compile and execute the following code:
Explain the role of:
- the first digit (either
0
or1
) between the braces, - the second number (
-20
,20
,10
, …) between the braces, - the last character (
C
,P
) between the braces.
Note that the previous statement could have been replaced by
as Console.WriteLine
can process “composite format strings”.