當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Arduino min()用法及代碼示例

[數學]

說明

計算兩個數字的最小值。

用法

min(x, y)

參數

x: 第一個數字。允許的數據類型:任何數據類型。
y: 第二個數字。允許的數據類型:任何數據類型。

返回

兩個數字中較小的一個。

示例代碼

該代碼確保它永遠不會超過 100。

sensVal = min(sensVal, 100);  // assigns sensVal to the smaller of sensVal or 100
                              // ensuring that it never gets above 100.

注意事項和警告

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

由於min()函數的實現方式,請避免使用括號內的其他函數,可能會導致錯誤的結果

min(a++, 100);  // avoid this - yields incorrect results

min(a, 100);
a++;  // use this instead - keep other math outside the function

相關用法


注:本文由純淨天空篩選整理自arduino.cc大神的英文原創作品 min()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。