Use the following code:
int myValue;
myValue = 1 + 2; // addition
myValue = 3 - 2; // subtraction
myValue = 3 * 2; // multiplication
myValue = 3 / 2; // division (the result is 1)
Integer division truncates the fractional remainder in the division example shown in this recipe’s Solution; myValue will equal 1 after the division
int value = 1 + 2 * 3 + 4;
Compound statements, such as the preceding statement, may appear ambiguous, but the precedence (order) of every operator is well-defined. Multiplication and division have higher precedence than addition and subtraction, so the result will be 11. It’s advisable to use parentheses in your code to make the desired calculation precedence clear. int value = 1 + (2 * 3) + 4; produces the same result but is easier to read.
Use parentheses if you need to alter the precedence, as in this example:
int value = ((1 + 2) * 3) + 4;
The result will be 13. The expression in the inner parentheses is calculated first, so 1 gets added to 2, this then gets multiplied by 3, and finally is added to 4, yielding 13.
You’ll need to make sure your result will not exceed the maximum size of the destination variable because the Arduino IDE will not warn you about that, unless you enable warnings in File→Preferences. However, even if you use the correct type, you can still overflow the size of the destination variable. Consider this code:
// 60 seconds in a minute, 60 minutes in an hour, 24 hours in a day
long seconds_per_day = 60 * 60 * 24;
In theory, that should be fine because the result is 86,400, which can fit in a long data type. But the value that’s really stored in seconds_per_day is 20,864. 86,400 is enough to overflow an integer twice (86,400 – 32,768 * 2 = 20,864). The overflow happens because the Arduino IDE’s C compiler sees an arithmetic expression composed of integers, and doesn’t know any better. You must tell the compiler that it should treat the whole expression like a long by appending L to the first value that is evaluated in the expression:long seconds_per_day = 60L * 60 * 24;
long seconds_per_day_plus_one = 1L + 60 * (60 * 24);
If, for some reason, you are using parentheses, remember that innermost parentheses are evaluated first, so this will overflow:
However, this will run correctly:
long seconds_per_day_plus_one = 1 + 60 * (60L * 24);
Complet code Arduino basic math
// Adding, Subtracting, Multiplying, and Dividing
void setup() { Serial.begin(9600); // put your setup code here, to run once: int myValue; myValue = 1 + 2; // addition myValue = 3 - 2; // subtraction myValue = 3 * 2; // multiplication myValue = 3 / 2; // division (the result is 1) myValue * 2; { int value = 1 + 2 * 3 + 4; value * 2; } { int value = ((1 + 2) * 3) + 4; value * 2; }
{ // 60 seconds in a minute, 60 minutes in an hour, 24 hours in a day long seconds_per_day = 60 * 60 * 24; Serial.println(seconds_per_day); } { long seconds_per_day = 60L * 60 * 24; Serial.println(seconds_per_day); } { long seconds_per_day_plus_one = 1L + 60 * (60 * 24); Serial.println(seconds_per_day_plus_one); } { long seconds_per_day_plus_one = 1 + 60 * (60L * 24); Serial.println(seconds_per_day_plus_one); }}
void loop() { // put your main code here, to run repeatedly:
}
Acknowledgment
I took help from Arduino Cookbook Book Arduino basic math tutorial. This cookbook is perfect for anyone who wants to experiment with the popular Arduino microcontroller and programming environment. You’ll find more than 200 tips and techniques for building a variety of objects and prototypes such as IoT solutions, environmental monitors, location and position-aware systems, and products that can respond to touch, sound, heat, and light.
No comments