Arduino abs() 绝对值函数使用
✨为什么要单独拿出来讲一下,这么一个函数,为了避免大家入坑!
这里先给出官网的abs()函数的解释链接、
abs()
看似简单的函数哪里来的坑呢?看似简单,其实很容易犯错的abs()
作为一个函数,在它的括号里面是不能进行算术运算,只能纯粹的在括号里面赋值,否则就是返回你意想不到的值
❌错误的示例:
volatile int a,b,c;
c=abs(a-b)
✅正确的示例:
volatile int a,b,c;
c=a-b;
c=abs(c);
- 🔖在使用
abs()
函数的时候,不能一步到位。
📝官网解释:
Notes and Warnings
Because of the way the abs() function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results.
abs(a++); // avoid this - yields incorrect results
// use this instead:
abs(a);
a++; // keep other math outside the function