15 Sep 1999 -- In lab3 and HW2 (and throughout the
course), we will often need to pay closer attention to the numeric
data types which Visual Basic uses (e.g. Integer, Long, Single,
Double, Currency)
Our textbook does not go into much detail about the types or the range
of numbers which they can store, but it is important to know about or
else we may often see "Overflow Error" as we try to do a calculation
with a value which is too large/small to be represented by a
particular data type.
For example, if a variable or expression is using Integer data, you
can only work with integers in the range -32,768 to 32,767. If you
use a number out of this range, even during an intermediate
calculation, you will get an error when running your program.
The limits for some of the various numeric types are:
Integer | -32,768 to 32,767 |
Long | -2,147,483,648 to 2,147,483,647 |
Single | -3.402823E38 to -1.401298E-45 for negative
values 1.401298E-45 to 3.402823E38 for positive values. |
Double | -1.79769313486232E308 to
-4.94065645841247E-324 for negative values 4.94065645841247E-324 to
1.79769313486232E308 for positive values |
Currency | $-922,337,203,685,477.5808 to
$922,337,203,685,477.5807 |
So when writing your programs you may want to pick the numeric type
based on the values you expect to deal with. Finally, you can always
change the result of one type into a different type during runtime by
using Visual Basic's conversion functions. For example typing
CInt(< blah >) will force < blah > to be interpreted as
an Integer. Similarly, CSng(), CDbl(), CLng(), CCur() force
numbers to be converted to the respective data types.
15 Sep 1999 -- Question 2 on quiz 2 was a bad question! I
will accept either answer (A) or answer (C) as correct.
Please bring your quiz to me if this applies.
For those interested in an optional mini-lesson:
The intent of the question was to test how visual basic deals with
integers during division. Unfortunately, our textbook did not
actually discuss this issue very clearly. Even more unfortunately, I
wrote the problem wrong. The expression "7/4" gets evaluated using
floating point division, and then at the very end when it is stored in
Integer variable X, it is converted to an integer by way of rounding
(and hence X=2 if you type in this code). To get a true integer
division in visual basic you would type the expression "7\4" (note the
slash used is in the other direction). This says to do division but
only return the whole number of the result, and hence X=7\4 sets X to
the value 1. For those really curious, if you want to get the
remainder in such an integer division you can type "7 mod 4" for
example, which would evaluate to 3 in this case, as 7 divide by 4 is
equal to 1 with a remainder of 3.