2. Week 1 Thursday: Integer Operations
≪ 1. Week 1 Tuesday: Errors | Table of Contents | 3. Week 2 Tuesday: Working with Numbers ≫You are likely familiar with the arithmetic operations of +-*/
, representing addition, subtraction, multplication, and division, respectively. One can add, subtract, multiply, and divide integers in C++, alongside a handful of other fun new operations you may or may not have seen before.
Addition, subtraction, and multiplication work exactly how one expects them to, but integer division is somewhat different than usual. Consider the following snippet of code:
Following conventional wisdom and common core elementary math standards, one would expect the output to be something like 2
, 1.428571...
, and 0.71428571...
on three separate lines. However, the actual output is 2
, 1
, and 0
.
Integer Division and the Modulo Operator
Integer division in C++ will always produce an integer and not a decimal. If a division operation would mathematically be a decimal number, C++ will chop off everything past the decimal point and call it a day. This applies to negative numbers as well:
1cout << -9 / 6 << endl; // expected: -1.5, actual: -1
2cout << 9 / -6 << endl; // same as above ^^^
3cout << -21 / -4 << endl; // expected: 5.25, actual: 5
4// etc...
Although frustrating to learn about, this particular behaviour with integer division plays a central role in many algorithms, particularly those that need to chop up lists of data into a set number of chunks and in certain computations. An example is division by 10 — if i
is a C++ integer and one writes the code i = i / 10
, what does this do to i
? When might this be useful?
Integer division was born with a very talented brother named the modulo operator, %
. This is often introduced as the “remainder” operator, and although this is a perfect characterisation of what %
does for positive numbers, it is very misleading and produces confusion for expressions like 21 % -4
. What’s the remainder to 21 / -4
?
Instead, think about how division and multiplication are supposed to cancel each other out. Mathematically, if \(a\) and \(b\) are two numbers and \(b\) is nonzero, we expect
\[(a \div b)\times b = a,\]
no matter what. However, this is not the case in C++ — work out what (21 / 4) * 4
is using integer division. The modulo operator’s purpose is to capture this discrepancy! Specifically, if a
and b
are two integers (negative or not), the quantity a % b
is defined as the number satisfying the equation a = (a / b) * b + a % b
. When a
and b
are both positive, this is exactly the same thing as a remainder; however, when either a
or b
is negative (or both), this alternative form continues to be unambiguous.
Problem 1.
Write a program that expresses 1999473 seconds as days, hours, minutes, and seconds.
Problem 2.
Write a program that expresses 302469 inches as miles, feet, and inches.
WARNING: I had made some serious mistakes when writing my notes for increment and decrement operators. Please see week 2 Thursday’s notes for a more accurate description of how they work. Sorry!