Teaching “CS integer” vs. “math integer”

The concept of an integer data type is not equivalent to the math concept of the set of integers. For example, in many programming languages the expressions 3. and $2^{10^{10000}}$ are math integers but (for different reasons) are not an integer data type. This potentially creates confusion for students in the age range of 8-16 years old who may be learning both the math and CS concepts of integer at once. What are effective ways to have students experience the distinct concepts so they reinforce each other rather than create confusion?

• could you please specify in which languages 3. is an integer? – ItamarG3 Jul 1 '17 at 17:32
• In Python for example, 3. is float but is mathematically an integer – Bennett Brown Jul 1 '17 at 17:33
• In math Integer and Real are infinite sets. int and float are vanishingly small (i.e. finite) subsets of those infinite sets. The language designer makes a choice about which values are most useful to the programmer. – Buffy Jul 1 '17 at 19:26
• Robert M. Pirsig said, "One geometry cannot be more true than another; it can only be more convenient. Geometry is not true, it is advantageous." – user737 Jul 2 '17 at 2:38

This might be an interesting way to do it, but here goes.

Give your students a blank sheet of paper. Have them write out "3". Then have them write out $2^{10^{1000}}$. Most will probably stare at you blankly. A few clever/sassy ones will just write out the exponential notation. Tell them that just as they write out numbers a little differently depending on size, the computer stores numbers a little differently depending on type.

If a number has a decimal point, it treats it differently from a number like 1 or 2, and it treats that differently from a really large number and it treats all of these differently from something like a string. This is the whole reason that you have to convert between types when you ask for a number as input() in python and then get a TypeError when you try to add it - instead you must add float(input()) or int(input()). It's the computer's way of making life easier for itself, just like we make lives easier for ourselves using exponential notation.

Also, by now hopefully they've realized that the world of math doesn't directly translate to some of its uses. Math is a very abstract thing, though it describes many things remarkably well. Pure math is very different from most math they've seen, and I think they understand the difference, for the same reason they understand the difference between x = x**2 and $x = x^2$. It's a different context, so it has a different meaning.

• @nocomprende, actually, the effects of words and meanings can be far more subtle than that. The words "YOU FLUNK THE COURSE," written on the board (even by a non-faculty prankster before class), would have a definite effect on the students. Practically everyone has some degree of irrational identification between the symbols for things and the things themselves. The first few paragraphs of this article come to mind. – Wildcard Jul 3 '17 at 22:19

If your students are mathematically inclined, you could simply think of the variable type as a constraint on the value contained inside of it.

For example:

• int represents $x \in \mathbb{Z}$ (and depending on your type's size, $-2^n \leq x < 2^n$ for a signed integer and $0 \leq x < 2^n$ for an unsigned integer)
• The set of values that may be held by a float is a much larger set than $\mathbb{Z}$, but $\mathbb{Z}$ is a subset of the float set, which is why an integer can be stored in a float, but not vice versa.

In other words, if $\mathbb{F}$ is the set of values representable by a floating point number, $\mathbb{Z} \subseteq \mathbb{F}$ (the integers are a subset of the values represented by floats). Or, if you want an image to show: Image by Booyabazooka of Wikipedia (public domain)

Whether this explanation is useful would depend on your students' proficiency with mathematics and set theory. For younger years, you could simply skip the subset discussion and show a Venn diagram to represent the idea that a float can be an integer, but an integer type can only represent an integernot all floats are whole numbers, but all whole numbers can be floats.

The issue of large integers not being represented by integers in some languages is more difficult to explain without stating the way that integers are stored. In many curricula, binary representation of integers is covered quite early, and if that's the case, explaining why int types can't hold extremely long integers is straightforward.

Show them how integers are actually represented in a computer. This has the benefit of also helping to explain integer underflow and overflow. In addition, it provides the opportunity later on to actually write a program that can convert from one number format to the other.

The two's complement system is not very difficult, and provides a good insight to some of the tricks that computers use to do basic operations. As a CS student, I think that these "go behind the scenes" opportunities help to generate a better appreciation for all the interesting problems students will encounter in the future in CS and Computer Engineering.