January 29, 2018
Deadlines: This lab needs to be completed before taking Lab 07.
Dependencies: Part I must be completed before Part II.
For this part, I recommend opening the web page or the numeric version of the document we just studied in class. Note that it contains numerous references at its end.
This part should be carried out without using VS.
Assume we have the following statements:
a / b
b * f
d + f
d + b
a + m
f / m
d * m
Main
method, and run the debugger to find out the answers to question a. For question b., write your own statements to perform the calculations. For instance, if you believe that the result of d + f
is of type int
, write something likefloat
litteral to an int
variable. Check it for yourself by adding the following to your program:You will get an error that reads
f
into the variable a
. To do so, replace the previous statement with the following:Using the debugger, observe the value stored in intVar
. Can you tell if the value stored in f
was rounded or truncated before being stored in the variable intVar
? Conduct further experiments if needed to answer this question.
Add the following in your code:
decimal decVar = 12344321.4999999991M;
double douVar = (double)decVar;
float floVar = (float)douVar;
And observe, using the debugger or printing instructions, the gradual loss of precision.
int
, one of type float
, and one of type double
.int
from the user (assuming your int
variable is named intVar
):Similarly, we can read float
using float.Parse(Console.ReadLine())
, and double using double.Parse(Console.ReadLine())
.
int
, a float
and a double
, store those values in the appropriate variables, and then print them.The following are two independent tasks, to widen your understanding of this class, and to prepare you for the next labs.
float
, double
, and decimal
:Console.Write("With floats:\n\t");
Console.WriteLine(1000000.0f + 1.2f - 1000000.0f);
Console.Write("With double:\n\t");
Console.WriteLine(1000000.0 + 1.2 - 1000000.0);
Console.Write("With decimal:\n\t");
Console.WriteLine(1000000.0m + 1.2m - 1000000.0m);
Can you explain this behaviour?
.ToString()
method introduced in the previous lab (Part III, b.) to convert the int
, float
and double
variables of Part III into string
, and store them in a string
variable. Use concatenation to create a single string containing those three values, and print them with a single Console.WriteLine
instruction.