Friday, February 10, 2017

Arithmetic Operators

Part 4 of the Arduino Programming Course
The Arduino can do mathematics for us. In this part of the course, we look at how to do addition, subtraction, multiplication, division, and find a remainder.
Below, five arithmetic operators are described and then all put into a sketch to demonstrate how they work on the Arduino.

Addition

To add numbers on the Arduino, we use the addition operator (+).
The example below shows how to add two numbers together.
int a = 2;
int b = 7;
int sum;

sum = a + b;
In the above code, three variables are defined. Variables a and b are each assigned a value when they are defined.
The sum variable is defined, but not initialized, so contains any random number. We will use this variable to store the result of the addition calculation, so the random value that sum contains will be overwritten when we put the addition result (or sum) into it.
After the statement shown below has been executed, sum will contain the value 9 i.e. the result of the addition of variable a and b.
sum = a + b;
We can also add two constant values and store the result in a variable as shown below.
int sum;

sum = 2 + 10;
The result stored in sum after execution of the addition statement will be 12 in this example.
Constant values and variables can also be added together and the result stored in a variable as shown here.
int a = 3;
int sum;

sum = a + 24;
After execution of the addition, sum will contain 27.
The remaining arithmetic operators can also operate on constant values, variables and a mixture of both.

Subtraction

The subtraction operator subtracts one number from another using the minus sign (-) as the following example shows.
int result;

result = 10 - 2;
The result of this calculation will be 8 and will be stored in the result variable.

Multiplication

Multiplication is done by using the multiplication operator (*).
int result;

result = 4 * 3;
The result of the above calculation will be 12 and will be stored in the result variable.

Division

The division operator (/) is used to perform division in the Arduino.
int result;

result = 12 / 3;
The result of the above calculation is 4.

Integers vs. Floating Point

So far we have only been using integer values to perform arithmetic. If the result of a division is not an integer (or whole number), but contains a fraction part, the fraction part will be discarded if the result is stored in an integer variable.
The following examples will demonstrate what happens when a result with a fractional part is stored in an integer and then a floating point variable.
int result;

result = 5 / 4;
The result will be 1 because the fraction is discarded when the result is stored in the integer variable result.
The same calculation, but this time defining result as a floating point variable (float).
float result;

result = 5.0 / 4.0;
The result now contained in the result variable is 1.25.
When using constant values in calculations that store the result in a floating point variable, we use a decimal point and a zero for whole numbers, i.e. 5.0 instead of 5 on its own.

Remainder (Modulo Division)

The remainder operator (or modulo operator) is used to find the remainder after the division of two numbers. The percentage sign (%) is used as the modulo operator.
int result;

result = 11 % 4;
The result of this calculation will be the remainder of 11 divided by 4 which is 3 (4 goes into 11 twice leaving a remainder of 3).




Arithmetic Sketch

The following sketch called arithmetic demonstrates all of the above arithmetic operators.
Load the sketch to your Arduino and try it out. Also add the examples from above that are not in the sketch, such as mixing constant values and variables.
void setup() {
  int a = 2;
  int b = 7;
  int result;
  float result_fl;
  
  Serial.begin(9600);
  
  Serial.print("Addition (a + b): ");
  result = a + b;
  Serial.println(result);
  
  Serial.print("Subtraction (10 - 2): ");
  result = 10 - 2;
  Serial.println(result);
  
  Serial.print("Multiplication (4 * 3): ");
  result = 4 * 3;
  Serial.println(result);
  
  Serial.print("Int Division (5 / 4): ");
  result = 5 / 4;
  Serial.println(result);
  
  Serial.print("Float Division (5.0 / 4.0): ");
  result_fl = 5.0 / 4.0;
  Serial.println(result_fl);
  
  Serial.print("Remainder (11 % 4): ");
  result = 11 % 4;
  Serial.println(result);
}

void loop() {
}
This video shows the above sketch running.

Comments on the Sketch

print() Function

The sketch contains a mix of print() and println() functions. print() is called when the invisible cursor must stay on the same line so text printed in the println() statement that follows will be printed on the same line as well.

println() Function

println() is used when text must be printed and then the invisible cursor moved to the next line so that the next print() statement will print text on a new line.

Floating Point Variable

A separate result_fl variable was defined to hold the result of the floating point calculation.

Integer Variable

The integer variable result is reused many times during the sketch. The important thing is to display its result before placing the result of the next calculation into it.

Constants and Variables

We looked at variables in the last part of this course. Constants are numbers that remain constant throughout a sketch – i.e. they never change.
Examples of constants are the numbers that are used directly in a calculation and are not assigned to a variable, e.g. sum = 2 + 3 contains the constants 2 and 3.
Constants do not have a name and their values can not be changed when the sketch is running.

Floating Point Arithmetic

Although not shown in the above examples, addition, subtraction and multiplication can also be done on floating point values.

No comments: