/

[Arithmetic Operators]

描述

Division is one of the four primary arithmetic operations. The operator / (slash) operates on two operands to produce the result.

語法

result = numerator / denominator;

參數

result: variable. Allowed data types: intfloatdoublebyteshortlong.
numerator: variable or constant. Allowed data types: intfloatdoublebyteshortlong.
denominatornon zero variable or constant. Allowed data types: intfloatdoublebyteshortlong.

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

Tips

  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.
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

語法參考主頁面

86Duino 參考的文本是根據 Creative Commons Attribution-ShareAlike 3.0 License,部分文本是從 the Arduino reference 修改的。 參考中的代碼示例已發佈到公共領域。

發表評論

上部へスクロール