Ch 1. Number Systems

1.1 Mathematical Data Types

In computing and information systems, data must be represented in a form that machines can store, process, and transmit. This begins with understanding mathematical data types, which are the categories of values that define the kind of data being handled. These types are foundational to programming, database design, and digital logic.

An integer is a number with no fractional or decimal part. In mathematics, the set of integers is denoted by [latex]\mathbb{Z}[/latex] and includes values such as [latex]-15[/latex], [latex]-4[/latex], [latex]0[/latex], [latex]9[/latex], and [latex]256[/latex]. Integers are used in computing for counting, indexing, and representing discrete (countable) values. Their range is limited because they are often stored in binary (0 or 1) format.

 

Example 1.1

Computer programs often have loop counters which keep track of how many times a block of code has been run. Integer variables are frequently used in their implementation. For example, consider a program that processes a batch of 100 e-commerce orders. This program may contain a for loop with an integer variable i. The variable i starts at 0 and increments by 1 for each order processed, until it reaches 99. In this example, the integer is used as a loop counter and as an index for each order.

FOR i = 0 to 99 DO

CALL process_order WITH orders[i]

END FOR

In this pseudocode, i is an integer value used as a loop counter. The loop runs from i = 0 to 99 (100 times). For each iteration, it processes the order in the ith position in the list. After each iteration, the counter i is incremented by 1.

Using integers is fundamental in computer programming, as seen in this example that uses loops, array indexing, and program flow control. They are the preferred data type for counting and iteration in programming languages because they are stored in binary, which is efficient and supports fast arithmetic.

 

A rational number is a number that can be written [latex]\frac{p}{q}[/latex], where [latex]p[/latex] and [latex]q[/latex] are integers, for example, [latex]\frac{3}{4}[/latex], [latex]-9[/latex], and [latex]0.4[/latex].

An irrational number is a number that can not be written in the form [latex]\frac{p}{q}[/latex], for example, [latex]\pi[/latex] and [latex]\sqrt{3}[/latex].

The set of real numbers is denoted by [latex]\mathbb{R}[/latex] and includes all rational and irrational numbers.

In computing, rational numbers cannot be stored exactly, so they need to be stored approximately. This can be achieved using fixed-point floating-point numbers.

 

Example 1.2

Consider a Google Nest Thermostat that reports a reading such as [latex]21.5^\circ[/latex]C. Since this value contains a fractional part ([latex]0.5[/latex]), it cannot be stored as an integer and must be stored as a floating-point number. A standard format used in computer systems is the IEEE 754 floating-point format, which uses a 32-bit binary number for single-precision or a 64-bit binary number for double-precision. This format breaks the number into three parts as follows:

sign bit                         exponent                     mantissa

The sign bit is positive or negative, the exponent scales the number, and the mantissa holds the significant digits. For the smart thermostat, the value [latex]21.5[/latex] in 32-bit IEEE 754 format is

0 10000011 01000000000000000000000

The purpose of this binary representation is to allow the smart thermostat to store and process the value efficiently, even though this representation only approximates [latex]21.5[/latex]. While this may not seem like a precise way to store numbers, floating-point numbers allow systems to handle values ranging from [latex]0.000001 (1E-06)[/latex] to [latex]1,000,000 (1E+06)[/latex]. For example, the thermostat may use the following code to monitor room temperatures:

IF temperature < 19.5 THEN

TURN ON HEATING

ELSE IF temperature > 23.5 THEN

TURN ON COOLING

This code will only execute correctly if the temperature data is stored accurately in a floating-point format.

 

A character is a single symbol, such as the letter "A", the digit "9", or the punctuation mark "?". A string is a group of characters, such as "discrete mathematics". Since computers can only work with binary numbers, each character is assigned a number according to standards such as the ASCII and Unicode standards.

 

Example 1.3

In computing, characters are stored as numeric codes using standardized encoding systems. One of the earliest and most widely used systems is the American Standard Code for Information Interchange, or ASCII. In this system, each character is assigned a 7-bit integer value. In ASCII, the capital letters start at decimal value 65. So, when a computer needs to store the character 'A', it stores the binary number 65 = 1000001. Depending on how the value is interpreted by software, different actions are taken for the character 'A', such as sending it to a printer, displaying it on a screen, or adding it to an Internet message.

Characters like 'A', 'B', '1', '@', and ' ' (space) all have specific numeric codes. These codes allow computers to store and manipulate text as sequences of numbers. For example, the word "DATA" is stored as [68, 65, 84, 65] in ASCII.

Operating systems receive these codes and interpret them as the appropriate character. For the character 'A', it may be typed on the keyboard, and a word processor displays it on the screen. In video games, the character 'A' often means move left.

If you need to use characters outside the set of alphanumeric characters, you need to use Unicode. This system increases the number of bits ASCII uses per character to up to 32 bits, allowing for over a million characters, including non-English characters such as '' and emojis such as '😊'. Since Unicode supports ASCII, the letter 'A' still has the code 65, while non-alphanumeric characters have higher codes.

Any textual data, such as names and messages, is a collection of characters and is represented as a string.

Example 1.4

Consider the string "x = Success!" which is a sequence (or array) of 12 characters. In computing, this string would likely be stored as an array of characters. Each character would be encoded in a standard like ASCII or Unicode as follows:

Characters:  x     =     S  u  c  c  e  s  s  !

Each character is stored as a numeric code: 'x' = 120, ' ' = 32, '=' = 61, ' ' = 32, 'S' = 83, 'u' = 117, 'c' = 99, 'c' = 99, 'e' = 101, 's' = 115, 's' = 115, '!' = 33.

In ASCII, each character takes 1 byte, so the entire string requires 12 bytes. In Unicode, these characters also use 1 byte each, since they fall within the ASCII-compatible range.

Strings like "x = Success!" are used in output messages to users, for logging and debugging, for network communication, and for file and data labelling.

In memory, the string might be stored as

[120, 32, 61, 32, 83, 117, 99, 99, 101, 115, 115, 33]

This numeric representation allows computers to manipulate, transmit, and store text efficiently.

 

A Boolean data type has only two possible values: true or false. These are used in logic operations, control flow, and decision-making in programs.

 

Example 1.5

Boolean variables are ideal for representing binary states, which are conditions that are either on or off, yes or no, active or inactive. Consider a website that remembers who is logged in. When a user logs in, the system sets a Boolean variable called isLoggedIn to true. When the user logs out, the variable is set to false.

IF user enters correct username AND password THEN

    SET isLoggedIn TO true

ELSE

    SET isLoggedIn TO false

END IF

This Boolean variable can then be used to control access to protected features:

IF isLoggedIn THEN

    DISPLAY "Welcome back!"

ELSE

    DISPLAY "Please log in to continue."

END IF

Boolean values are highly efficient since they require only 1 bit of memory. They are foundational in decision-making logic, conditional statements, and control flow in programming. In databases, Booleans are used to flag statuses like isActive, isAdmin, or hasPaid.

Think of a light switch. It’s either ON (true) or OFF (false). Similarly, a Boolean variable indicates whether a condition is met.

 

While not strictly mathematical in origin, composite types like tuples and arrays are built from basic data types and are used to group related values. An ordered collection of elements (e.g., (3, "Aiden", true)) is called a tuple. An array is a list of aspects of the same type (e.g., [1, 2, 3, 4]).

If you find yourself in a position where you need to organize and process data efficiently, then these structures are essential.

 

Real World Example 1.1: Online Shopping Cart System

Suppose you are working on an e-commerce platform with a shopping cart system to manage and process customer orders. This system uses various mathematical data types we have discussed, such as integers, floating-point numbers, strings, Booleans, and tuples.

Each item in the cart has a quantity, such as 3 units of a product. This is stored as an integer because it represents a whole number count.

The price of each item (e.g., $19.99) and the total cost (e.g., $59.97) are stored as floating-point numbers to handle decimal values accurately.

Product names like "Wireless Mouse" and customer names like "Aiden Chen" are stored as strings, which are sequences of characters.

Discounts such as free shipping can be assigned a Boolean flag, such as isEligibleForFreeShipping, which might be set to true or false based on the total order value.

When we store items in the shopping cart, we can use a tuple and write it in the form

(Product ID, Product Name, Quantity, PricePerUnit, InStock)

If a customer has placed two wireless mouses (which have the Product ID 1024) in their shopping cart for $19.99 each and the items were in stock, the tuple would be

(1024, “Wireless Mouse”, 2, 19.99, true)

By allowing the shopping cart system to use multiple data types, it can perform a variety of operations, including calculating order totals, applying shipping discounts, and managing inventory.