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


Arduino max()用法及代碼示例

[數學]

說明

計算兩個數字的最大值。

用法

max(x, y)

參數

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

返回

兩個參數值中較大的一個。

示例代碼

該代碼確保 sensVal 至少為 20。

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

注意事項和警告

或許counter-intuitively、max()常用於約束變量範圍的下端,而min()用於約束範圍的上端。

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

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

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

相關用法


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