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.