Skip to main content

Section 2.1 Numbers and Strings

Preview Activity 2.1.1.

Write a sentence answering each question.

(a)

How would you define a “number”?

(b)

Given your definition, are there certain numbers that cannot be understood by a computer?

Activity 2.1.2.

The counting numbers (sometimes written as the lowercase Greek letter omega \(\omega\text{,}\) also sometimes called “whole numbers” or “natural numbers”) are those numbers used to quantify whole items:

\begin{equation*} \{0,1,2,3,\dots,100,101,\dots,8675309,8675310,\dots\} \end{equation*}

For each of the following possible rules for counting numbers, either decide that it should be true, or find an example showing why the rule should be false.

(a)

Adding one to a counting number results in another counting number. For example, \(6\) is a counting number, so \(6+1=7\) is also a counting number.

(b)

Subtracting one from a counting number results in another counting number. For example, \(6\) is a counting number, so \(6-1=5\) is also a counting number.

(c)

Doubling a counting number results in another counting number. For example, \(32\) is a counting number, so \(32\times 2=64\) is also a counting number.

(d)

Halving a counting number results in another counting number. For example, \(32\) is a counting number, so \(32\div 2=16\) is also a counting number.

(e)

Squaring a counting number results in another counting number. For example, \(9\) is a counting number, so \(9^2=81\) is also a counting number.

(f)

Square rooting a counting number results in another counting number. For example, \(9\) is a counting number, so \(\sqrt{9}=3\) is also a counting number.

Activity 2.1.3.

The integers \(\mathbb Z\) include all the counting numbers as well as their negatives: \(\{\dots,-2,-1,0,1,2,\dots\}\text{.}\)

(a)

Explain which of the number rules from 2.1.2 work when “counting number” is replaced with “integer”, and which ones fail.

  • Adding one

  • Subtracting one

  • Doubling

  • Halving

  • Squaring

  • Square rooting

(b)

Choose from the words all/some/no to make the following sentence correct.

All/some/no integers are also counting numbers, and all/some/no counting numbers are also integers.

Activity 2.1.4.

The rational numbers \(\mathbb Q\) include all fractions of integers that don't divide by zero, such as \(\frac{1}{2}\text{,}\) \(\frac{22}{7}\text{,}\) and \(-\frac{1234812348}{42}\text{.}\)

(a)

Explain which of the number rules from 2.1.2 work when “counting number” is replaced with “rational number”, and which ones fail.

  • Adding one

  • Subtracting one

  • Doubling

  • Halving

  • Squaring

  • Square rooting

(b)

Choose from the words all/some/no to make the following sentence correct.

All/some/no integers are also rational numbers, and all/some/no rational numbers are also integers.

Activity 2.1.5.

There are other kinds of real numbers, such as \(e\) and \(\pi\text{,}\) that aren't rational. (The technical definition of the system of real numbers requires experience with calculus, so we won't get into that here.) There are other kinds of numbers as well, such as the complex numbers explorered in 7.

Since SageMath tries to model mathematics as authentically as possible, it will try to recognize when you are working with integers or rational numbers. For example, type(-17/3) display that it is a “sage.rings.rational.Rational

Use the type() tool to help explain why \(-\frac{1234812348}{42}\) is an integer or rational number.

Activity 2.1.6.

Something all these sets of numbers described above is that they are infinite: if you had a finite list of them, take the biggest and add one, and you'll have something you forgot in your list.

While SageMath does its best to hide this limitation from users, there's a limit to how many of these infinitely-many possible numerical values can be stored on a physical computer due to its finite storage space.

To account for this, some lower-level languages offer data types like the short that account for a useful (but finite) range of integers. In particular, shorts are integers that may be stored as a short finite list of the binary digits \(0\) and \(1\text{.}\) (Each binary digit is also known as a bit.)

(a)

Only two numbers may be described using one binary digit: \(0_2\) and \(1_2\text{.}\) Only four numbers may be described using two binary digits: \(00_2\text{,}\) \(01_2\text{,}\) \(10_2\text{,}\) and \(11_2\text{.}\)

Describe all the possible numbers using three binary digits.

\begin{equation*} \square\square\square_2 \end{equation*}

(b)

What do you notice about this pattern? According to this pattern, how many numbers do you expect can be described using four binary digits? Five digits?

(c)

A byte of computer storage is eight binary digits. How many different values of data can be stored in a byte?

(d)

How many different numbers can be stored as a short data type that uses two bytes?

Activity 2.1.7.

Since ultimately data is stored as bits, anything handled by a computer needs to be represented somehow as a (binary) number. For example, a character is a letter or other glyph (such as an emoji 😃) that has a unique representation as a counting number.

(a)

To denote a character in Python or SageMath, surround it with either single quotes 'x' or double quotes "x". Then the code ord('x') (or ord("x")) will reveal the number that is used to represent it in memory (for x, that number is \(120\)), called a Unicode value.

Find the Unicode values for the characters s, S, and 😊. (For the emoji, you may wish to copy-and-paste the character if you're using a standard keyboard.)

(b)

Given a Unicode value, Python and SageMath can return the character it represents by using the chr() tool. For example, print(chr(120)) displays x as expected.

Use chr() to display the charcters with these Unicode values:

  • 89

  • 97

  • 121

  • 13025

(c)

Strings of multiple characters may be used to represent words and sentences. Displaying the string test = "Hello World!" is a common test used in setting up a new programming environment, done with print(test).

The value of a CS variable may be easily inserted into a string by using interpolation. In updated versions of Python 3 and SageMath, this is done by typing an f before the quotation marks: print(f"It's time to say {test} now.") will display It's time to say Hello World! now. (provided test = "Hello World!" was executed previously).

Store the word “math” as a CS variable, then use interpolation to make it appear instead of the blank when running a Code cell with print("My favorite class is ____.").

(d)

Use string interpolation to replace the blank in the line print("The character ___ has Unicode value 42+42.")

Exercises Exercises

1.

Is \(-1.5\) a counting number, integer, and/or rational number?

2.

Is \(+1.5\) a counting number, integer, and/or rational number?

3.

Is \(-15\) a counting number, integer, and/or rational number?

4.

Is \(+15\) a counting number, integer, and/or rational number?

5.

A nibble of computer storage uses four bits (binary digits). How many different values of data can be stored in a nibble?

6.

How many bytes equals twelve nibbles?

7.

A float data type is one used to store fractions using binary numbers. A simple float might use the following two bytes of data...

01101101 11111100

... to represent the following rational number.

\begin{equation*} 01101101_2 \times 2^{11111100_2-256} \end{equation*}

Show how to verify that this float represents the decimal value \(6.8125\text{.}\) (Remember that the value of \(01101101_2\) may be calculated automatically by running int('01101101',2) in either Python or SageMath.)

8.

Importantly, in the float implementation described by the previous exercise, a leading 1 in either byte requires subtracting the value \(256\) from the usual binary value (in order to support negative values).

Use this rule to verify that this implementation might store the decimal \(-0.265625\) as 11101111 11111010.

9.

Use string interpolation along with chr() and ord() to replace the blanks in the following lines of code with sensible output.

print("The Unicode value of the @ character is ___.")
print("The Unicode value of the emoji ___ is 128525.")