arduino - Why does division in #define cause division in code to output a different answer
问题描述
I was getting speed values 4 times less than I calculated and narrowed it down to the division of the inverse of the wheel radius. I originally had WHEEL_D_OVER_TWO
and when doing the reciprocal I was getting an incorrect answer(a factor of 4 difference). I have replicated the problem in the code below.
Hardware is an Arduino UNO.
#define WHEEL_RADIUS 30.0
#define WHEEL_D_OVER_TWO 60.0 / 2.0
void setup() { Serial.begin(115200); }
void loop() {
Serial.println("WHEEL_RADIUS : " + String(WHEEL_RADIUS, 8));
Serial.println("Inverse WHEEL_RADIUS : " + String(1.000 / WHEEL_RADIUS, 8));
Serial.println("WHEEL_D_OVER_TWO : " + String(WHEEL_D_OVER_TWO, 8));
Serial.println("Inverse WHEEL_D_OVER_TWO : " +
String(1.000 / WHEEL_D_OVER_TWO, 8));
delay(1000);
}
The output of the above code is
WHEEL_RADIUS : 30.00000000
Inverse WHEEL_RADIUS : 0.03333334
WHEEL_D_OVER_TWO : 30.00000000
Inverse WHEEL_D_OVER_TWO : 0.00833333
Inverse WHEEL_RADIUS
is correct using a calculator. Most problems I searched for were for integer division which I think I am avoiding here. I know the current workaround but I would like to know what I am fundamentally missing from this example that is giving me different answers.
Thanks in advance.
解决方案
宏只是文本替换。你WHEEL_D_OVER_TWO
的定义为
#define WHEEL_D_OVER_TWO 60.0 / 2.0
意思就是
1.000 / WHEEL_D_OVER_TWO
扩展到
1.000 / 60.0 / 2.0
由于/
C 和 C++ 中的运算符从左到右关联,因此这被解释为
(1.000 / 60.0) / 2.0
显然,这甚至与
1.000 / WHEEL_RADIUS
(这显然是你所期望的)。
这就是为什么当你将宏定义为表达式时,记住给它们加上括号很重要
#define WHEEL_D_OVER_TWO (60.0 / 2.0)
这将使您免于这样的惊喜。
一个更好的主意(至少在 C++ 中)是使用const
变量而不是宏并完全忘记这个问题
const double WHEEL_D_OVER_TWO = 60.0 / 2.0;
推荐阅读
- java - 如何解决使用 for 循环打印表格的 Java 问题?
- html - 窗口浏览器上的身体高度和最小高度扩展
- c++ - 无法实例化抽象类,也无法将参数 1 从“const size_t”转换为“const BDictionary”
&' 错误 - python - 存储最大交易金额并在回溯期内保留第二高交易金额
- flutter - 带有页面视图的 BottomNavigationBar 不会动画到索引页面
- apache-spark - Spark:reduceByKey() 的缓慢实现
- reactjs - 组件正在从另一个组件接收道具,并连接到商店以调用操作
- firebase - Firestore 数组联合:INVALID_ARGUMENT:无法将数组值转换为数组值
- python - Python - 多行命令的语法无效
- java - 清理类路径,将来避免此问题的正确方法是什么?