The operators +, -, *, and / respectively evaluate to the sum, difference, product, or quotient (respectively) of the two operands. The operation is conducted using the data type of the operands, so, for example, 9 / 4 gives 2 since 9 and 4 are int variables.
This also means that the operation can overflow if the result is larger than that which can be stored in the data type (e.g. adding 1 to an int with the value 2,147,483,647 gives -2,147,483,648).
If the operands are of different types, the “larger” type is used for the calculation. If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the calculation.
Note
The specifics of these rules are beyond the scope of this documentation; for more information, see The C++ Programming Language, by Bjarne Stroustroup, Appendix C, especially §§C.4-C.6, or this WikiBooks entry on C++ type conversion.
Note
For more information on how computers represent integers, see the Wikipedia page on two’s complement.
result = value1 + value2; result = value1 - value2; result = value1 * value2; result = value1 / value2;
Since the STM32 processor on the Maple is a 32-bit machine, the int type overflows at a much higher value on Maple than on Arduino. In particular, on Maple, ints do not overflow (become negative) until they reach 2,147,483,648; on the Arduino, they overflow at 32,767. Because of this, programs running on Maple are much less likely to run into overflow issues. The following table summarizes the sizes and ranges of integer datatypes on the Maple (the ranges of long long types are approximate):
Datatype | Unsigned range | Signed range | Size (bytes) |
---|---|---|---|
char | 0 — 255 | -128 — 127 | 1 |
short | 0 — 65,535 | -32,768 — 32,767 | 2 |
int | 0 — 4,294,967,295 | -2,147,483,648 — 2,147,483,647 | 4 |
long | 0 — 4,294,967,295 | -2,147,483,648 — 2,147,483,647 | 4 |
long long | 0 — 1.8*1019 (approx.) | -9.2*1018 — 9.2*1018 (approx.) | 8 |
License and Attribution
Portions of this page were adapted from the Arduino Reference Documentation, which is released under a Creative Commons Attribution-ShareAlike 3.0 License.