+ (Addition)

From Microduino Wiki
Jump to: navigation, search
  • Add, subtract, multiply and divide
  • Description
    • These operators return the sum, difference, product and quotient of two operands. These operations are calculated depending on the type of data operand.
    • For example, the type of 9 and 4 is int, so the outcome of 9 / 4 is 2, which also means that if the result is beyond the range that the type of the data can accommodate, overflow will appear.(For example, 1 plus 32,767 in int, the result is -32,768).
    • If the operands are of different types, the result is of the larger type. If one of the operands is of float type or double, it becomes a floating arithmetic.
  • Example

y = y + 3;
x = x - 7;
i = j * 6;
r = r / 5;

result = value1 + value2;
result = value1 - value2;
result = value1 * value2;
result = value1 / value2;

value1: Any constant or variable  value2: Any constant or variable
  • Note:
    • The default value of the int constant is int, so some calculation between int constants ( in the definition ) will result in overflow, (For example: 60 * 1000 will get a negative result, so if(60*1000 > 0) ,if will get a false value.)
    • When choosing the type of the variables, you must make sure that the scope of the variable typed are big enough, so that they can accommodate the result.
    • You should know on which point that your variable will “turn over”, and you should pay attention the two directions. For example: (0 - 1) or (0 - - 32768)
    • Floating-point numbers can be used in the process of the mathematical scores, but the shortcoming is: The bits length that it will take up is big, and the mathematical speed is slow.
    • Use the type conversion operator, such as (int)myFloat , which can cast a variable to int type.

[Return to Arduino Syntax Manual]