max(x, y)

描述

比較出兩數中最大者。

參數

x: 第一個數,任意資料型別
y: 第二個數,任意資料型別

回傳

兩數中較大者

範例

sensVal = max(senVal, 20); 
// 在 sensVal 與 20 之間比較出較大者再指派給 sensVal 
// 可用來確定 sensVal 永遠不會比 20 小

注意

以直觀的計數方式來說,max() 比較常用來讓數值低於一個變數範圍,而 min()比較常用來讓數值高於一個變數範圍。

警告

因為 max() 實作方式的關係,盡量避免在括號 () 中使用其他的函式,否則它可能會導致不正確的結果。

max(a--, 0);   // 避免這樣做,可能會產生錯誤結果

a--;           // 改為這樣做
max(a, 0);     // 保持其他數學運算在函式之外

See also

min()
constrain()


語法參考主頁面

本頁由熱血青年 LBU 譯自英文版。

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.