<p>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.</p>

<p>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.</p>

CC BY-SA 4.0
2020-09-12T12:18:45.320Z
This. A computer is a machine. We don't need metaphors or math concepts, we need to know how the machine actually works. It is not rocket science, a 13 year old could (did, in my case) get it. Venn diagrams and stories would have put me off from computer *science* entirely. Just explain the facts. With the facts, as you say, we can understand many other things about computers immediately, that metaphors and concepts from math would have been no help whatsoever. People really over-think things.
Scott Rowe
CC BY-SA 4.0
2020-09-12T12:23:07.917Z
Maybe the real problem is that computer science instructors do not know as much about *electronics* as I did by the time I was 13, from perusing outdated books about vacuum tubes in the library. It had only 3 books that mentioned transistors, 40 years ago. But I understood how transistors could build a flip-flop, and flip-flops could build a latch, and how latches could implement registers, an ALU and main memory. I didn't have to have encyclopedic knowledge of those things, just an idea. So, instructors: stop diverting to math and teach from the implementation, which is what is real.
Scott Rowe
CC BY-SA 3.0
2017-07-16T21:13:03.620Z
2017-07-16T21:13:03.620Z
Soupy
<p>This might be an interesting way to do it, but here goes.</p>

<p>Give your students a blank sheet of paper. Have them <em>write out</em> "3". Then have them <em>write out</em> $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 <em>type</em>.</p>

<p>If a number has a decimal point, it treats it differently from a number like 1 or 2, and it treats <em>that</em> 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 <code>input()</code> in python and then get a <code>TypeError</code> when you try to add it - instead you must add <code>float(input())</code> or <code>int(input())</code>. It's the computer's way of making life easier for itself, just like we make lives easier for ourselves using exponential notation.</p>

<p>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 <code>x = x**2</code> and $x = x^2$. It's a different context, so it has a different meaning.</p>

CC BY-SA 3.0
2017-07-03T22:19:04.770Z
@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](http://education.lronhubbard.org/page49.htm) come to mind.
Wildcard
CC BY-SA 3.0
2017-07-01T18:06:44.527Z
2017-07-01T22:17:24.690Z
2017-07-01T22:17:24.690Z
auden
<p>If your students are mathematically inclined, you could simply think of the variable type as a constraint on the value contained inside of it.</p>

<p>For example:</p>

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

<p>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:</p>

<p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Venn_A_subset_B.svg/155px-Venn_A_subset_B.svg.png" alt="Image of subset"></p>

<p><sup>Image by Booyabazooka of <a href="https://en.wikipedia.org/wiki/Subset#/media/File:Venn_A_subset_B.svg" rel="nofollow noreferrer">Wikipedia</a> (public domain)</sup></p>

<p>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 <strong>can</strong> be an integer, but an integer type <strong>can only represent an integer</strong>—<em>not all floats are whole numbers, but all whole numbers can be floats</em>.</p>

<p>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 <code>int</code> types can't hold extremely long integers is straightforward.</p>

CC BY-SA 3.0
2017-07-01T18:48:02.847Z
2017-07-01T18:48:02.847Z
Aurora0001
<p>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 <code>3.</code> 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? </p>

CC BY-SA 3.0
2017-07-01T17:32:53.463Z
could you please specify in which languages `3.` is an integer?
ItamarG3
CC BY-SA 3.0
2017-07-01T17:33:53.793Z
In Python for example, `3.` is `float` but is mathematically an integer
Bennett Brown
CC BY-SA 3.0
2017-07-01T19:26:06.503Z
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
<secondary-education><mathematics><data-types>
Teaching "CS integer" vs. "math integer"
# Teaching "CS integer" vs. "math integer"

(Asked by: username_0 on 2017-07-01)

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?

**Tags:** secondary-education, mathematics, data-types

> **Comments:**
> **username_1**: could you please specify in which languages `3.` is an integer?  
> **username_0**: In Python for example, `3.` is `float` but is mathematically an integer  
> **username_2**: 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.  

---

## 3 answers

### Answer by username_5 on 2017-07-01. Score: 7

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.

> **Comments:**
> **username_6**: @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](http://education.lronhubbard.org/page49.htm) come to mind.  

---

### Answer by username_7 on 2017-07-01. Score: 3

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 of subset](https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Venn_A_subset_B.svg/155px-Venn_A_subset_B.svg.png)

Image by Booyabazooka of [Wikipedia](https://en.wikipedia.org/wiki/Subset#/media/File:Venn_A_subset_B.svg) (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 integer**—*not 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.


---

### Answer by username_3 on 2017-07-16. Score: 1

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.

> **Comments:**
> **username_4**: This. A computer is a machine. We don't need metaphors or math concepts, we need to know how the machine actually works. It is not rocket science, a 13 year old could (did, in my case) get it. Venn diagrams and stories would have put me off from computer *science* entirely. Just explain the facts. With the facts, as you say, we can understand many other things about computers immediately, that metaphors and concepts from math would have been no help whatsoever. People really over-think things.  
> **username_4**: Maybe the real problem is that computer science instructors do not know as much about *electronics* as I did by the time I was 13, from perusing outdated books about vacuum tubes in the library. It had only 3 books that mentioned transistors, 40 years ago. But I understood how transistors could build a flip-flop, and flip-flops could build a latch, and how latches could implement registers, an ALU and main memory. I didn't have to have encyclopedic knowledge of those things, just an idea. So, instructors: stop diverting to math and teach from the implementation, which is what is real.  

---

