min(x, y)

描述

比較出兩數中最小者。

參數

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

回傳

兩數中較小者

範例

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

注意

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

警告

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

min(a++, 100);   // 避免這樣做,可能會產生錯誤結果

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

See also

max()
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.