Here is a summary of what this page covers:
- What are Arithmetic Expressions?
- What is Integer Division?
- What is Truncation in Programming?
- What is the Mod Operator and How Does it Work?
What are Arithmetic Expressions?
It’s Math time! Arithmetic Expressions can be numeric variables, arithmetic operators, or numeric literals. They simplify to a single value when evaluated (duh!). Here is an example of an arithmetic expression with no variables:
8.675*10*10
This expression equals 867.5. If we substituded a variable of x that equals 10, then we could do:
8.675*x*x
Which also equals 867.5. From the previous section, you are probably familiar with the operators of +, -, * and /.
Let’s go through some logic, take the following expression:
9*(11-3) equals 72
Notice the parentheses in the example help dictate which order to evaluate the expression. However, a program doesn’t always evaluate expressions from left to right. Consider another example:
2 + 3*4
If evaluated from left to right, this would equal (2+3)*4 = 20, instead of 14.
Multiplication and division have a higher order of precedence than addition and subtraction. What this means is that in an arithmetic expression, you should first run through it left to right, only performing the muliplications and divisions. After doing this, process the expression again from left to right, doing all the additions and subtractions.
Consider this expression:
2 + 3*5 - 6/3 * 4/8 + 2*6 - 2*3*4
First, we need to go through from left to right, and perform all of the multiplications and divisions:
2 + 15 - 1 + 12 - 24
Now, we finish by doing all of the additions and subtractions, left to right:
4
If you want your expression to be evaluated in a different order, simply add parentheses, which are the highest precedence when it comes to arithmetic operators.
In order our precedence goes: Parentheses, Multiplication or Division, then finally Addition or Subtraction.
What is Integer Division?
Integer division, much like the title, is dividing two integers against one another.
For example, let’s take some integer code:
int x = 4;
int y = 2;
int z = x / y;
Wow, that’s great. Basically, it’s just division.
However, it’s not all peaches and pie when it comes to division in programming. We are going to answer the question: What happens if there is a remainder?
Let’s take another example:
int x = 8;
int y = 3;
int z = x/y;
What would z‘s value become? If it’s an integer type of variable, then that means there’s no room for decimals. So what happens, is the remainder gets truncated.
What is Truncation in Programming?
Let’s take the actual value of z, which is 2.666666, if we did it on a calculator for example. What integer division would do is truncate that value. It would not round it down, it would not round it up, and it probably would throw some sort of warning dependent on your coding language (unless it’s javascript, where everything is your fault).
So the 2.6666 value would evaluate to just 2
However, if we change the variable types to a type that allows decimals, then we can handle this more appropriately. Take the following example:
double x = 8;
double y = 3;
double z = x/y;
z would now equal the appropriate 2.66666666
Some compilers and languages are able to automatically handle decimal division appropriately, even without specifying types. Such as using 11/4 and 11/4.0 will designate the type of division to use (whether to truncate or not).
However, we can use a special operator, called the mod operator, to handle and check our remainders.
What is the Mod Operator and How Does It Work?
First, the mod operator is more notoriously designated as the percent sign (%)
It is only used for integer-type variables and contains the remainder of a division expression.
Let’s try some examples. Here is truncated integer division, followed by a mod division:
19 / 6 = 3
19 % 6 = 1
14 / 7 = 2
14 % 7 = 0
12 / 5 = 2
12 % 5 = 2
Let’s take the first two lines. 19 / 6 = 3.166666 on my calculator (which is 3 and a remainder of 1), but using integer division, it truncates the remainder which is the 0.1666666 and evaluates to 3. The second line takes only the remainder from the division expression, and returns that.
If you’d like to practice more with the mod operator, try to find a calculator site that can do so, here’s a good one to try:
One of the best things to use a mod operator for, is to see if a number is divisible by something else. If the mod operation evaluates to 0, then you know that the two numbers are divisible by each other. For example:
int x = 10%2 // evaluates to 0, which means 10 is evenly divisible by 2
int y = 3%2 // evaluates to 1, which means 3 is evenly divisible by 2
Well that concludes this page, thanks for reading!