Integers and Operations on Integers


Integers and real numbers

An integer is a whole number, such as 2 or 14. It can also be negative, such as -3.

Mathematically, integers are special cases of real numbers. But programming languages need to distinguish between them, and there are several places where integers are used but real numbers are not. (Integers are typically used for counting things. If you count how many letters are in a word, the answer cannot be 3.5. It has to be a whole number.)

When you write a number in a program, it is presumed to be a real number if it has a decimal point or an E, and an integer if not. For example, 2.0 is a real number, but 2 is an integer. Even 1E6 is considered a real number (one million), not an integer, since it contains an E. To write one million as an integer, write 1000000.

Computations with real numbers can have roundoff error. For example, if you compute 1.0/3.0, you get approximately 0.333333333333333. That is not exactly one-third, but is close. Even the number 0.1 is not remembered exactly. There is a slight roundoff error.

But integers are exact. There is no roundoff error. So 2 + 3 is exactly 5.


Language differences

Programming languages do not all agree on notation to use. At this point, Cinnameg and Java go their separate ways. We will use Cinnameg notation until later, when we switch to Java.


Integer division

Before you learned about fractions, you learned how to divide integers by getting a quotient and a remainder. Suppose you have 14 apples to distribute equally among 3 people, and you are not allowed to cut an apple. You give each person 4 apples, with 2 apples left over. When you divide 14 by 3, the quotient is 4 and the remainder is 2.

Programming languages provide operations to compute integer quotients and remainders. In Cinnameg, the quotient when you divide x by y is written x `div` y. The strange looking operator `div` is has left-single-quotes on either side of it. You will probably find the left-single-quote character on your keyboard just under the escape key, on the same key as the ~ character. It is critical that you use `div`, not 'div', with apostrophes.

To compute the remainder when you divide x by y in Cinnameg, write x `mod` y. So

   14 `div` 3 = 4
   14 `mod` 3 = 2
The `div` and `mod` operators have the same precedence as * and /, and are done from left to right.

Sometimes the quotient is 0. For example, suppose that you have 12 apples to distribute among 20 people. As before, you are not allowed to cut an apple, and you are required to give the same number of apples to each person. Your only choice is to give each person zero apples, and to have all 12 apples left over. So

  12 `div` 20 = 0
  12 `mod` 20 = 12

(The quotient and remainder operators act a little strangely on negative numbers. You will probably want to stick with nonnegative numbers. Also, you cannot compute x `div` 0 or x `mod` 0, since those expressions require you to divide by 0.)


Uses of quotient and remainder

You can use the remainder operator to do divisibility tests. To decide whether an integer x is divisible by 2 (that is, an even number), compute x `mod` 2 and ask whether the result is 0. We will see how to ask questions, such as whether a number is 0, fairly shortly.

If x is a nonnegative integer then x `mod` 10 is the rightmost digit of x, and x `div` 10 is what you get if you remove the rightmost digit from x. For example,

  2456 `mod` 10 = 6
  2456 `div` 10 = 245


Div and mod only work on integers

You can only use the `div` and `mod` operators on integers. Expression 2.5 `mod` 0.5 is not allowed. Even expression 14.0 `mod` 3.0 is not allowed.


Problems

  1. [solve] How can you tell if a number is even?

  2. [solve] Write an expression that computes the remainder when you divide 123456789 by 13. What is the answer? (Type the expression into the box and push Compute.)

  3. [solve] Is 123456789 divisible by 13?

  4. [solve] Is 123456788 divisible by 13?

  5. [solve] Is (15 `mod` 4 + 1) an allowed expression in Cinnameg? Why or why not? If so, what is its value?

  6. [solve] Is (15.0 `div` 3.0) an allowed expression in Cinnameg? Why or why not?


Summary

Real numbers can have fractional parts but integers cannot. Real numbers are only rememebered approximately, with some roundoff error. Integers are remembered exactly.

Cinnameg and Java keep integers and real numbers separate, and some operations can only be done on integers. For example, you can only compute x `div` y and x `mod` y when x and y are integers.

Expression x `div` y is the integer quotient when you divide x by y. Expression x `mod` y is the integer remainder when you divide x by y.

Use the `mod` operator to check whether one number is divisible by another.


Review

An algorithm is a clear and precise set of instructions telling how to solve a computational problem.

An expression is something that you can evaluate to get a value, by performing operations such as operators (addition, subtraction, multiplication, etc.) and functions.

There are some special rules to remember when you write expressions in a programming language. For example, you need to use * to indicate multiplication.