Max()

從 YFRobotwiki
跳到: 導覽搜尋

描述

計算兩個數字的最大值。


參數

  • x: 第一個數字,任何數據類型
  • y: 第二個數字,任何數據類型


返回

兩個數字中的較大的數字。


示例

sensVal = max(senVal, 20); // assigns sensVal to the larger of sensVal or 20
                           // (effectively ensuring that it is at least 20)


注意

可能與直覺相反,max()通常用於約束變量範圍的下限,而min()用於限制範圍的上限。


警告

由於實現了max()函數的方式,避免在括號內使用其他函數,否則可能導致錯誤的結果

max(a--, 0);   // avoid this - yields incorrect results

max(a, 0); 
a--;           //use this instead - keep other math outside the function


擴展閱讀

- min()
- constrain()




返回Arduino語法參考列表

更多建議和問題歡迎反饋至 YFRobot論壇