“Max()”的版本间的差异
来自YFRobotwiki
(以“<font color="orange" size="+1">'''描述'''</font> <font color="orange" size="+1">'''语法'''</font> <pre style="color:dimgray"> </pre> <font color="orange"...”为内容创建页面) |
|||
| 第1行: | 第1行: | ||
<font color="orange" size="+1">'''描述'''</font> | <font color="orange" size="+1">'''描述'''</font> | ||
| + | 计算两个数字的最大值。 | ||
| + | <font color="orange" size="+1">'''参数'''</font> | ||
| − | + | * x: 第一个数字,任何数据类型 | |
| + | * y: 第二个数字,任何数据类型 | ||
| − | |||
| − | </ | + | <font color="orange" size="+1">'''返回'''</font> |
| + | 两个数字中的较大的数字。 | ||
| − | |||
| + | <font color="orange" size="+1">'''示例'''</font> | ||
| + | <pre style="color:dimgray"> | ||
| + | sensVal = max(senVal, 20); // assigns sensVal to the larger of sensVal or 20 | ||
| + | // (effectively ensuring that it is at least 20) | ||
| + | </pre> | ||
| − | <font color=" | + | <font color="red" size="">''' 注意'''</font> |
| + | 可能与直觉相反,max()通常用于约束变量范围的下限,而min()用于限制范围的上限。 | ||
| + | <font color="red" size="">'''警告'''</font> | ||
| − | + | 由于实现了max()函数的方式,避免在括号内使用其他函数,否则可能导致错误的结果 | |
<pre style="color:dimgray"> | <pre style="color:dimgray"> | ||
| + | max(a--, 0); // avoid this - yields incorrect results | ||
| + | max(a, 0); | ||
| + | a--; //use this instead - keep other math outside the function | ||
</pre> | </pre> | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
<font color="orange" size="+1">'''扩展阅读'''</font> | <font color="orange" size="+1">'''扩展阅读'''</font> | ||
| − | :- [[ | + | :- [[min()]] |
| − | :- [[ | + | :- [[constrain()]] |
| − | + | ||
| − | + | ||
2017年9月13日 (三) 13:33的最后版本
描述
计算两个数字的最大值。
参数
- x: 第一个数字,任何数据类型
- y: 第二个数字,任何数据类型
返回
两个数字中的较大的数字。
示例
sensVal = max(senVal, 20); // assigns sensVal to the larger of sensVal or 20
// (effectively ensuring that it is at least 20)
注意
可能与直觉相反,max()通常用于约束变量范围的下限,而min()用于限制范围的上限。
警告
由于实现了max()函数的方式,避免在括号内使用其他函数,否则可能导致错误的结果
max(a--, 0); // avoid this - yields incorrect results max(a, 0); a--; //use this instead - keep other math outside the function
扩展阅读
- - min()
- - constrain()
更多建议和问题欢迎反馈至 YFRobot论坛