arduino / arduino/ArduinoCore-API
Use of macros for math functions causes unexpected results when argument has side effect
- Dominant language
- C++
- Stars
- 306
- Forks
- 150
- PR merge metrics
- No merged PRs in 30d
Description
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
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by locating the Arduino API definitions for abs, constrain, round, and bitWrite, then inspect the related reports for expected behavior and compatibility concerns. Replace the problematic macro behavior with the proposed function approach and verify that side-effecting arguments are evaluated once without changing the public API.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- api, embedded-iot
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100