Arduino> Structure> Arithmetic Operators>
%
[Arithmetic Operators]
Description
Remainder operation calculates the remainder when one integer is divided by another. It is useful for keeping a variable within a particular range (e.g. the size of an array). The % (percent) symbol is used to carry out remainder operation.
Syntax
reaminder = dividend % divisor
Parameters
remainder
: variable. Allowed data types: int, float, double.dividend
: variable or constant. Allowed data types: int.divisor
: non zero variable or constant. Allowed data types: int.Example Code
int x = 0;
x = 7 % 5; // x now contains 2
x = 9 % 5; // x now contains 4
x = 5 % 5; // x now contains 0
x = 4 % 5; // x now contains 4
x = -4 % 5; // x now contains -4
x = 4 % -5; // x now contains 4
/* update one value in an array each time through a loop */
int values[10];
int i = 0;
void setup() {}
void loop() {
values[i] = analogRead(0);
i = (i + 1) % 10; // remainder operator rolls over variable
}
Notes and Warnings
1. The remainder operator does not work on floats.
2. If the first operand is negative, the result is negative (or zero). Therefore, the result of
-------------------------------------------------------------------2. If the first operand is negative, the result is negative (or zero). Therefore, the result of
x % 10
will not always be between 0 and 9 if x
can be negative.*
[Arithmetic Operators]
Description
Multiplication is one of the four primary arithmetic operations. The operator * (asterisk) operates on two operands to produce the product.
Syntax
product = operand1 * operand2;
Parameters
product
: variable. Allowed data types: int, float, double, byte, short, long.operand1
: variable or constant. Allowed data types: int, float, double, byte, short, long.operand2
: variable or constant. Allowed data types: int, float, double, byte, short, long.Example Code
int a = 5;
int b = 10;
int c = 0;
c = a * b; // the variable 'c' gets a value of 50 after this statement is executed
Notes and Warnings
1. The multiplication operation can overflow if the result is bigger than that which can be stored in the data type.
2. If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the calculation.
3. If the operands are of float / double data type and the variable that stores the product is an integer, then only the integral part is stored and the fractional part of the number is lost.
2. If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the calculation.
3. If the operands are of float / double data type and the variable that stores the product is an integer, then only the integral part is stored and the fractional part of the number is lost.
float a = 5.5;
float b = 6.6;
int c = 0;
c = a * b; // the variable 'c' stores a value of 36 only as opposed to the expected product of 36.3}
-------------------------------------------------------------------+
[Arithmetic Operators]
Description
Addition is one of the four primary arithmetic operations. The operator + (plus) operates on two operands to produce the sum.
Syntax
sum = operand1 + operand2;
Parameters
sum
: variable. Allowed data types: int, float, double, byte, short, long.operand1
: variable or constant. Allowed data types: int, float, double, byte, short, long.operand2
: variable or constant. Allowed data types: int, float, double, byte, short, long.Example Code
int a = 5;
int b = 10;
int c = 0;
c = a + b; // the variable 'c' gets a value of 15 after this statement is executed
Notes and Warnings
1. The addition operation can overflow if the result is larger than that which can be stored in the data type (e.g. adding 1 to an integer with the value 32,767 gives -32,768).
2. If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the calculation.
3. If the operands are of float / double data type and the variable that stores the sum is an integer, then only the integral part is stored and the fractional part of the number is lost.
2. If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the calculation.
3. If the operands are of float / double data type and the variable that stores the sum is an integer, then only the integral part is stored and the fractional part of the number is lost.
float a = 5.5;
float b = 6.6;
int c = 0;
c = a + b; // the variable 'c' stores a value of 12 only as opposed to the expected sum of 12.1
--------------------------------------------------------------------
[Arithmetic Operators]
Description
Subtraction is one of the four primary arithmetic operations. The operator - (minus) operates on two operands to produce the difference of the second from the first.
Syntax
difference = operand1 - operand2;
Parameters
difference
: variable. Allowed data types: int, float, double, byte, short, long.operand1
: variable or constant. Allowed data types: int, float, double, byte, short, long.operand2
: variable or constant. Allowed data types: int, float, double, byte, short, long.Example Code
int a = 5;
int b = 10;
int c = 0;
c = a - b; // the variable 'c' gets a value of -5 after this statement is executed
Notes and Warnings
1. The subtraction operation can overflow if the result is smaller than that which can be stored in the data type (e.g. subtracting 1 from an integer with the value -32,768 gives 32,767).
2. If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the calculation.
3. If the operands are of float / double data type and the variable that stores the difference is an integer, then only the integral part is stored and the fractional part of the number is lost.
2. If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the calculation.
3. If the operands are of float / double data type and the variable that stores the difference is an integer, then only the integral part is stored and the fractional part of the number is lost.
float a = 5.5;
float b = 6.6;
int c = 0;
c = a - b; // the variable 'c' stores a value of -1 only as opposed to the expected difference of -1.1
-------------------------------------------------------------------/
[Arithmetic Operators]
Description
Division is one of the four primary arithmetic operations. The operator / (slash) operates on two operands to produce the result.
Syntax
result = numerator / denominator;
Parameters
result
: variable. Allowed data types: int, float, double, byte, short, long.numerator
: variable or constant. Allowed data types: int, float, double, byte, short, long.denominator
: non zero variable or constant. Allowed data types: int, float, double, byte, short, long.Example Code
int a = 50;
int b = 10;
int c = 0;
c = a / b; // the variable 'c' gets a value of 5 after this statement is executed
Notes and Warnings
1. If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the calculation.
2. If the operands are of float / double data type and the variable that stores the result is an integer, then only the integral part is stored and the fractional part of the number is lost.
2. If the operands are of float / double data type and the variable that stores the result is an integer, then only the integral part is stored and the fractional part of the number is lost.
float a = 55.5;
float b = 6.6;
int c = 0;
c = a / b; // the variable 'c' stores a value of 8 only as opposed to the expected result of 8.409
-------------------------------------------------------------------=
[Arithmetic Operators]
Description
The single equal sign = in the C++ programming language is called the assignment operator. It has a different meaning than in algebra class where it indicated an equation or equality. The assignment operator tells the microcontroller to evaluate whatever value or expression is on the right side of the equal sign, and store it in the variable to the left of the equal sign.
Example Code
int sensVal; // declare an integer variable named sensVal
sensVal = analogRead(0); // store the (digitized) input voltage at analog pin 0 in SensVal
Notes and Warnings
1. The variable on the left side of the assignment operator ( = sign ) needs to be able to hold the value stored in it. If it is not large enough to hold a value, the value stored in the variable will be incorrect.
2. Don’t confuse the assignment operator [ = ] (single equal sign) with the comparison operator [ == ] (double equal signs), which evaluates whether two expressions are equal.
-------------------------------------------------------------------2. Don’t confuse the assignment operator [ = ] (single equal sign) with the comparison operator [ == ] (double equal signs), which evaluates whether two expressions are equal.
Post a Comment
Hi Users, if you have any queries then please let me know.