arduino / arduino/ArduinoCore-API

Use of macros for math functions causes unexpected results when argument has side effect

未关闭
#85 6 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看
bug
主要语言
C++
星标
306
派生
150
PR 合并指标
30 天内没有已合并 PR

描述

I find semantically wrong the fact that the following math functions are implemented as macros:

- `abs`
- `constrain`
- `round`
- ~~`min`~~
- ~~`max`~~

... and let me add `bitWrite` to the list.

It is well known that using these function the way the rest of the world does introduces faults, and therefore generates failures.

Writing something like `abs(cos(x) - 123 * sq(sin(x))` would introduce **redundant calculations**, just a crazy delay in this case...

Writing something like `abs(a += b)` or `abs(i++)` just introduces a **fault**... it does something different (wrong in fact).

I assume that using **template** and **inline** functions would solve the issue with no extra executable space taken.

### Additional context

#### Additional reports

- https://github.com/arduino/Arduino/issues/9571
- https://github.com/arduino/ArduinoCore-avr/issues/324

#### Related

- https://github.com/arduino/reference-en/pull/513

贡献指南

这个仓库没有索引到贡献指南

调研方向

首先定位 abs、constrain、round 和 bitWrite 的 Arduino API 定义,然后检查相关报告,以了解预期行为和兼容性方面的担忧。使用建议的函数方式替换有问题的宏行为,并验证带有副作用的参数只求值一次,同时不改变公共 API。

由索引模型根据 Issue 内容生成。

评估

技术栈
cpp
领域
api, embedded-iot
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。